*
* @ingroup Cache
*/
-abstract class BagOStuff implements LoggerAwareInterface {
+abstract class BagOStuff implements IExpiringStore, LoggerAwareInterface {
/** @var array[] Lock tracking */
protected $locks = array();
/** Bitfield constants for get()/getMulti() */
const READ_LATEST = 1; // use latest data for replicated stores
const READ_VERIFIED = 2; // promise that caller can tell when keys are stale
+ /** Bitfield constants for set()/merge() */
+ const WRITE_SYNC = 1; // synchronously write to all locations for replicated stores
public function __construct( array $params = array() ) {
if ( isset( $params['logger'] ) ) {
abstract protected function doGet( $key, $flags = 0 );
/**
- * @note: This method is only needed if cas() is not is used for merge()
+ * @note: This method is only needed if merge() uses mergeViaCas()
*
* @param string $key
* @param mixed $casToken
* @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
* @return mixed Returns false on failure and if the item does not exist
+ * @throws Exception
*/
protected function getWithToken( $key, &$casToken, $flags = 0 ) {
throw new Exception( __METHOD__ . ' not implemented.' );
* @param string $key
* @param mixed $value
* @param int $exptime Either an interval in seconds or a unix timestamp for expiry
+ * @param int $flags Bitfield of BagOStuff::WRITE_* constants
* @return bool Success
*/
- abstract public function set( $key, $value, $exptime = 0 );
+ abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
/**
* Delete an item
* @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
+ * @param int $flags Bitfield of BagOStuff::WRITE_* constants
* @return bool Success
* @throws InvalidArgumentException
*/
- public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
if ( !is_callable( $callback ) ) {
throw new InvalidArgumentException( "Got invalid callback." );
}
- return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
+ return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
}
/**
do {
$this->clearLastError();
$casToken = null; // passed by reference
- $currentValue = $this->getWithToken( $key, $casToken, BagOStuff::READ_LATEST );
+ $currentValue = $this->getWithToken( $key, $casToken, self::READ_LATEST );
if ( $this->getLastError() ) {
return false; // don't spam retries (retry only on races)
}
* @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
+ * @param int $flags Bitfield of BagOStuff::WRITE_* constants
* @return bool Success
*/
- protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
if ( !$this->lock( $key, 6 ) ) {
return false;
}
$this->clearLastError();
- $currentValue = $this->get( $key, BagOStuff::READ_LATEST );
+ $currentValue = $this->get( $key, self::READ_LATEST );
if ( $this->getLastError() ) {
$success = false;
} else {
if ( $value === false ) {
$success = true; // do nothing
} else {
- $success = $this->set( $key, $value, $exptime ); // set the new value
+ $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
}
}
}
}
- $expiry = min( $expiry ?: INF, 86400 );
+ $expiry = min( $expiry ?: INF, self::TTL_DAY );
$this->clearLastError();
$timestamp = microtime( true ); // starting UNIX timestamp
} elseif ( $this->getLastError() || $timeout <= 0 ) {
$locked = false; // network partition or non-blocking
} else {
- $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
+ // Estimate the RTT (us); use 1ms minimum for sanity
+ $uRTT = max( 1e3, ceil( 1e6 * ( microtime( true ) - $timestamp ) ) );
$sleep = 2 * $uRTT; // rough time to do get()+set()
$attempts = 0; // failed attempts
* @since 1.26
*/
final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
- $expiry = min( $expiry ?: INF, 86400 );
+ $expiry = min( $expiry ?: INF, self::TTL_DAY );
if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
return null;
/**
* Increase stored value of $key by $value while preserving its TTL
*
- * This will create the key with value $init and TTL $ttl if not present
+ * This will create the key with value $init and TTL $ttl instead if not present
*
* @param string $key
* @param int $ttl
* @param int $value
* @param int $init
- * @return bool
+ * @return int|bool New value or false on failure
* @since 1.24
*/
public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
- return $this->incr( $key, $value ) ||
- $this->add( $key, (int)$init, $ttl ) || $this->incr( $key, $value );
+ $newValue = $this->incr( $key, $value );
+ if ( $newValue === false ) {
+ // No key set; initialize
+ $newValue = $this->add( $key, (int)$init, $ttl ) ? $init : false;
+ }
+ if ( $newValue === false ) {
+ // Raced out initializing; increment
+ $newValue = $this->incr( $key, $value );
+ }
+
+ return $newValue;
}
/**
* @return int
*/
protected function convertExpiry( $exptime ) {
- if ( ( $exptime != 0 ) && ( $exptime < 86400 * 3650 /* 10 years */ ) ) {
+ if ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) ) {
return time() + $exptime;
} else {
return $exptime;
* @return int
*/
protected function convertToRelative( $exptime ) {
- if ( $exptime >= 86400 * 3650 /* 10 years */ ) {
+ if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
$exptime -= time();
if ( $exptime <= 0 ) {
$exptime = 1;
* @return string
*/
public function makeKeyInternal( $keyspace, $args ) {
- $key = $keyspace . ':' . implode( ':', $args );
+ $key = $keyspace;
+ foreach ( $args as $arg ) {
+ $arg = str_replace( ':', '%3A', $arg );
+ $key = $key . ':' . $arg;
+ }
return strtr( $key, ' ', '_' );
}
* Make a global cache key.
*
* @since 1.27
- * @param string $args,...
+ * @param string ... Key component (variadic)
* @return string
*/
public function makeGlobalKey() {
* Make a cache key, scoped to this instance's keyspace.
*
* @since 1.27
- * @param string $args,...
+ * @param string ... Key component (variadic)
* @return string
*/
public function makeKey() {