/**
* @param string $key
- * @param int $timeout [optional]
+ * @param int $timeout Lock wait timeout [optional]
+ * @param int $expiry Lock expiry [optional]
* @return bool Success
*/
- public function lock( $key, $timeout = 6 ) {
+ public function lock( $key, $timeout = 6, $expiry = 6 ) {
$this->clearLastError();
$timestamp = microtime( true ); // starting UNIX timestamp
- if ( $this->add( "{$key}:lock", 1, $timeout ) ) {
+ if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
return true;
} elseif ( $this->getLastError() ) {
return false;
}
usleep( $sleep ); // back off
$this->clearLastError();
- $locked = $this->add( "{$key}:lock", 1, $timeout );
+ $locked = $this->add( "{$key}:lock", 1, $expiry );
if ( $this->getLastError() ) {
return false;
}
- } while ( !$locked );
+ } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
return $locked;
}
/**
* @param string $key
* @param int $timeout
+ * @param int $expiry
* @return bool
*/
- public function lock( $key, $timeout = 0 ) {
+ public function lock( $key, $timeout = 6, $expiry = 6 ) {
// Lock only the first cache, to avoid deadlocks
if ( isset( $this->caches[0] ) ) {
- return $this->caches[0]->lock( $key, $timeout );
+ return $this->caches[0]->lock( $key, $timeout, $expiry );
} else {
return true;
}