* merge() using the locking version by default. The callers that implement cas()
override merge() to use the cas-based version. Those that do not no longer
need to define the unused dummy method.
* Removed some redundant doc blocks.
Change-Id: I49f7dd5432efa8d76f4758c273f0859376ddafb7
* @ingroup Cache
*/
class APCBagOStuff extends BagOStuff {
- /**
- * @param string $key
- * @param int $casToken [optional]
- * @return mixed
- */
public function get( $key, &$casToken = null ) {
$val = apc_fetch( $key );
return $val;
}
- /**
- * @param string $key
- * @param mixed $value
- * @param int $exptime
- * @return bool
- */
public function set( $key, $value, $exptime = 0 ) {
if ( !$this->isInteger( $value ) ) {
$value = serialize( $value );
return true;
}
- /**
- * @param mixed $casToken
- * @param string $key
- * @param mixed $value
- * @param int $exptime
- * @return bool
- * @throws MWException
- */
- protected function cas( $casToken, $key, $value, $exptime = 0 ) {
- // APC's CAS functions only work on integers
- throw new MWException( "CAS is not implemented in " . __CLASS__ );
- }
-
- /**
- * @param string $key
- * @return bool
- */
public function delete( $key ) {
apc_delete( $key );
return true;
}
- /**
- * @param string $key
- * @param callable $callback Callback method to be executed
- * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
- * @param int $attempts The amount of times to attempt a merge in case of failure
- * @return bool Success
- */
- public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
- return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
- }
-
public function incr( $key, $value = 1 ) {
return apc_inc( $key, $value );
}
throw new Exception( "Got invalid callback." );
}
- return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+ return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
}
/**
}
/**
- * Check and set an item.
+ * Check and set an item
+ *
* @param mixed $casToken
* @param string $key
* @param mixed $value
* @param int $exptime Either an interval in seconds or a unix timestamp for expiry
* @return bool Success
*/
- abstract protected function cas( $casToken, $key, $value, $exptime = 0 );
+ protected function cas( $casToken, $key, $value, $exptime = 0 ) {
+ throw new MWException( "CAS is not implemented in " . __CLASS__ );
+ }
/**
* @see BagOStuff::merge()
* @ingroup Cache
*/
class EmptyBagOStuff extends BagOStuff {
-
- /**
- * @param string $key
- * @param mixed $casToken [optional]
- * @return bool
- */
public function get( $key, &$casToken = null ) {
return false;
}
- /**
- * @param string $key
- * @param mixed $value
- * @param int $exp
- * @return bool
- */
public function set( $key, $value, $exp = 0 ) {
return true;
}
- /**
- * @param mixed $casToken
- * @param string $key
- * @param mixed $value
- * @param int $exp
- * @return bool
- */
- protected function cas( $casToken, $key, $value, $exp = 0 ) {
- return true;
- }
-
- /**
- * @param string $key
- * @return bool
- */
public function delete( $key ) {
return true;
}
- /**
- * @param string $key
- * @param callable $callback Callback method to be executed
- * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
- * @param int $attempts The amount of times to attempt a merge in case of failure
- * @return bool Success
- */
public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
- return true;
+ return true; // faster
}
}
$this->bag = array();
}
- /**
- * @param string $key
- * @return bool
- */
protected function expire( $key ) {
$et = $this->bag[$key][1];
return true;
}
- /**
- * @param string $key
- * @param mixed $casToken [optional]
- * @return bool|mixed
- */
public function get( $key, &$casToken = null ) {
if ( !isset( $this->bag[$key] ) ) {
return false;
return $this->bag[$key][0];
}
- /**
- * @param string $key
- * @param mixed $value
- * @param int $exptime
- * @return bool
- */
public function set( $key, $value, $exptime = 0 ) {
$this->bag[$key] = array( $value, $this->convertExpiry( $exptime ) );
return true;
}
- /**
- * @param mixed $casToken
- * @param string $key
- * @param mixed $value
- * @param int $exptime
- * @return bool
- */
- protected function cas( $casToken, $key, $value, $exptime = 0 ) {
- if ( $this->get( $key ) === $casToken ) {
- return $this->set( $key, $value, $exptime );
- }
-
- return false;
- }
-
- /**
- * @param string $key
- * @return bool
- */
function delete( $key ) {
if ( !isset( $this->bag[$key] ) ) {
return false;
return true;
}
+
+ function lock( $key ) {
+ return true;
+ }
+
+ function unlock( $key ) {
+ return true;
+ }
}
$this->fixExpiry( $exptime ) );
}
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ if ( !is_callable( $callback ) ) {
+ throw new Exception( "Got invalid callback." );
+ }
+
+ return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+ }
+
/**
* Get the underlying client object. This is provided for debugging
* purposes.
return false;
}
- /**
- * @param mixed $casToken
- * @param string $key
- * @param mixed $value
- * @param mixed $exptime
- * @return bool
- * @throws MWException
- */
- protected function cas( $casToken, $key, $value, $exptime = 0 ) {
- throw new MWException( "CAS is not implemented in " . __CLASS__ );
- }
-
/**
* @param string $key
* @param mixed $value
$this->logRequest( 'incr', $key, $server, $result );
return $result;
}
+
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ if ( !is_callable( $callback ) ) {
+ throw new Exception( "Got invalid callback." );
+ }
+
+ return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+ }
+
/**
* @param mixed $data
* @return string
return $newValue;
}
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ if ( !is_callable( $callback ) ) {
+ throw new Exception( "Got invalid callback." );
+ }
+
+ return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+ }
+
/**
* @param DatabaseBase $db
* @param string $exptime
return true;
}
+
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ if ( !is_callable( $callback ) ) {
+ throw new Exception( "Got invalid callback." );
+ }
+
+ return $this->mergeViaCas( $key, $callback, $exptime, $attempts );
+ }
}
return true;
}
- /**
- * @param mixed $casToken
- * @param string $key
- * @param mixed $value
- * @param int $exptime
- * @return bool
- * @throws MWException
- */
- protected function cas( $casToken, $key, $value, $exptime = 0 ) {
- // Can't find any documentation on xcache cas
- throw new MWException( "CAS is not implemented in " . __CLASS__ );
- }
-
/**
* Remove a value from the XCache object cache
*
return true;
}
- /**
- * Merge an item.
- * XCache does not seem to support any way of performing CAS - this however will
- * provide a way to perform CAS-like functionality.
- *
- * @param string $key
- * @param callable $callback Callback method to be executed
- * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
- * @param int $attempts The amount of times to attempt a merge in case of failure
- * @return bool Success
- */
- public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
- return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
- }
-
public function incr( $key, $value = 1 ) {
return xcache_inc( $key, $value );
}