summaryrefslogtreecommitdiff
path: root/includes/libs/objectcache
diff options
context:
space:
mode:
Diffstat (limited to 'includes/libs/objectcache')
-rw-r--r--includes/libs/objectcache/APCBagOStuff.php71
-rw-r--r--includes/libs/objectcache/BagOStuff.php201
-rw-r--r--includes/libs/objectcache/EmptyBagOStuff.php2
-rw-r--r--includes/libs/objectcache/HashBagOStuff.php14
-rw-r--r--includes/libs/objectcache/ReplicatedBagOStuff.php129
-rw-r--r--includes/libs/objectcache/WANObjectCache.php746
-rw-r--r--includes/libs/objectcache/WinCacheBagOStuff.php33
-rw-r--r--includes/libs/objectcache/XCacheBagOStuff.php23
8 files changed, 1090 insertions, 129 deletions
diff --git a/includes/libs/objectcache/APCBagOStuff.php b/includes/libs/objectcache/APCBagOStuff.php
index eaf11557..35e05e80 100644
--- a/includes/libs/objectcache/APCBagOStuff.php
+++ b/includes/libs/objectcache/APCBagOStuff.php
@@ -27,43 +27,88 @@
* @ingroup Cache
*/
class APCBagOStuff extends BagOStuff {
- public function get( $key, &$casToken = null ) {
- $val = apc_fetch( $key );
+
+ /**
+ * @var bool If true, trust the APC implementation to serialize and
+ * deserialize objects correctly. If false, (de-)serialize in PHP.
+ */
+ protected $nativeSerialize;
+
+ /**
+ * @var string String to append to each APC key. This may be changed
+ * whenever the handling of values is changed, to prevent existing code
+ * from encountering older values which it cannot handle.
+ */
+ const KEY_SUFFIX = ':2';
+
+ /**
+ * Constructor
+ *
+ * Available parameters are:
+ * - nativeSerialize: If true, pass objects to apc_store(), and trust it
+ * to serialize them correctly. If false, serialize
+ * all values in PHP.
+ *
+ * @param array $params
+ */
+ public function __construct( array $params = array() ) {
+ parent::__construct( $params );
+
+ if ( isset( $params['nativeSerialize'] ) ) {
+ $this->nativeSerialize = $params['nativeSerialize'];
+ } elseif ( extension_loaded( 'apcu' ) && ini_get( 'apc.serializer' ) === 'default' ) {
+ // APCu has a memory corruption bug when the serializer is set to 'default'.
+ // See T120267, and upstream bug reports:
+ // - https://github.com/krakjoe/apcu/issues/38
+ // - https://github.com/krakjoe/apcu/issues/35
+ // - https://github.com/krakjoe/apcu/issues/111
+ $this->logger->warning(
+ 'The APCu extension is loaded and the apc.serializer INI setting ' .
+ 'is set to "default". This can cause memory corruption! ' .
+ 'You should change apc.serializer to "php" instead. ' .
+ 'See <https://github.com/krakjoe/apcu/issues/38>.'
+ );
+ $this->nativeSerialize = false;
+ } else {
+ $this->nativeSerialize = true;
+ }
+ }
+
+ public function get( $key, &$casToken = null, $flags = 0 ) {
+ $val = apc_fetch( $key . self::KEY_SUFFIX );
$casToken = $val;
- if ( is_string( $val ) ) {
- if ( $this->isInteger( $val ) ) {
- $val = intval( $val );
- } else {
- $val = unserialize( $val );
- }
+ if ( is_string( $val ) && !$this->nativeSerialize ) {
+ $val = $this->isInteger( $val )
+ ? intval( $val )
+ : unserialize( $val );
}
return $val;
}
public function set( $key, $value, $exptime = 0 ) {
- if ( !$this->isInteger( $value ) ) {
+ if ( !$this->nativeSerialize && !$this->isInteger( $value ) ) {
$value = serialize( $value );
}
- apc_store( $key, $value, $exptime );
+ apc_store( $key . self::KEY_SUFFIX, $value, $exptime );
return true;
}
public function delete( $key ) {
- apc_delete( $key );
+ apc_delete( $key . self::KEY_SUFFIX );
return true;
}
public function incr( $key, $value = 1 ) {
- return apc_inc( $key, $value );
+ return apc_inc( $key . self::KEY_SUFFIX, $value );
}
public function decr( $key, $value = 1 ) {
- return apc_dec( $key, $value );
+ return apc_dec( $key . self::KEY_SUFFIX, $value );
}
}
diff --git a/includes/libs/objectcache/BagOStuff.php b/includes/libs/objectcache/BagOStuff.php
index 0b791e5a..ddbe8eaa 100644
--- a/includes/libs/objectcache/BagOStuff.php
+++ b/includes/libs/objectcache/BagOStuff.php
@@ -1,7 +1,5 @@
<?php
/**
- * Classes to cache objects in PHP accelerators, SQL database or DBA files
- *
* Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
* https://www.mediawiki.org/
*
@@ -37,29 +35,34 @@ use Psr\Log\NullLogger;
* the PHP memcached client.
*
* backends for local hash array and SQL table included:
- * <code>
+ * @code
* $bag = new HashBagOStuff();
* $bag = new SqlBagOStuff(); # connect to db first
- * </code>
+ * @endcode
*
* @ingroup Cache
*/
abstract class BagOStuff implements LoggerAwareInterface {
- private $debugMode = false;
-
+ /** @var array[] Lock tracking */
+ protected $locks = array();
+ /** @var integer */
protected $lastError = self::ERR_NONE;
- /**
- * @var LoggerInterface
- */
+ /** @var LoggerInterface */
protected $logger;
+ /** @var bool */
+ private $debugMode = false;
+
/** Possible values for getLastError() */
const ERR_NONE = 0; // no error
const ERR_NO_RESPONSE = 1; // no response
const ERR_UNREACHABLE = 2; // can't connect
const ERR_UNEXPECTED = 3; // response gave some error
+ /** Bitfield constants for get()/getMulti() */
+ const READ_LATEST = 1; // use latest data for replicated stores
+
public function __construct( array $params = array() ) {
if ( isset( $params['logger'] ) ) {
$this->setLogger( $params['logger'] );
@@ -87,9 +90,10 @@ abstract class BagOStuff implements LoggerAwareInterface {
* Get an item with the given key. Returns false if it does not exist.
* @param string $key
* @param mixed $casToken [optional]
+ * @param integer $flags Bitfield; supports READ_LATEST [optional]
* @return mixed Returns false on failure
*/
- abstract public function get( $key, &$casToken = null );
+ abstract public function get( $key, &$casToken = null, $flags = 0 );
/**
* Set an item.
@@ -109,18 +113,20 @@ abstract class BagOStuff implements LoggerAwareInterface {
/**
* Merge changes into the existing cache value (possibly creating a new one).
- * The callback function returns the new value given the current value (possibly false),
- * and takes the arguments: (this BagOStuff object, cache key, current value).
+ * The callback function returns the new value given the current value
+ * (which will be false if not present), and takes the arguments:
+ * (this BagOStuff, cache key, current value).
*
* @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
+ * @throws InvalidArgumentException
*/
public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
if ( !is_callable( $callback ) ) {
- throw new Exception( "Got invalid callback." );
+ throw new InvalidArgumentException( "Got invalid callback." );
}
return $this->mergeViaLock( $key, $callback, $exptime, $attempts );
@@ -137,11 +143,17 @@ abstract class BagOStuff implements LoggerAwareInterface {
*/
protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
do {
+ $this->clearLastError();
$casToken = null; // passed by reference
$currentValue = $this->get( $key, $casToken );
+ if ( $this->getLastError() ) {
+ return false; // don't spam retries (retry only on races)
+ }
+
// Derive the new value from the old value
$value = call_user_func( $callback, $this, $key, $currentValue );
+ $this->clearLastError();
if ( $value === false ) {
$success = true; // do nothing
} elseif ( $currentValue === false ) {
@@ -151,6 +163,9 @@ abstract class BagOStuff implements LoggerAwareInterface {
// Try to update the key, failing if it gets changed in the meantime
$success = $this->cas( $casToken, $key, $value, $exptime );
}
+ if ( $this->getLastError() ) {
+ return false; // IO error; don't spam retries
+ }
} while ( !$success && --$attempts );
return $success;
@@ -164,6 +179,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
* @param mixed $value
* @param int $exptime Either an interval in seconds or a unix timestamp for expiry
* @return bool Success
+ * @throws Exception
*/
protected function cas( $casToken, $key, $value, $exptime = 0 ) {
throw new Exception( "CAS is not implemented in " . __CLASS__ );
@@ -183,14 +199,18 @@ abstract class BagOStuff implements LoggerAwareInterface {
return false;
}
+ $this->clearLastError();
$currentValue = $this->get( $key );
- // Derive the new value from the old value
- $value = call_user_func( $callback, $this, $key, $currentValue );
-
- if ( $value === false ) {
- $success = true; // do nothing
+ if ( $this->getLastError() ) {
+ $success = false;
} else {
- $success = $this->set( $key, $value, $exptime ); // set the new value
+ // Derive the new value from the old value
+ $value = call_user_func( $callback, $this, $key, $currentValue );
+ if ( $value === false ) {
+ $success = true; // do nothing
+ } else {
+ $success = $this->set( $key, $value, $exptime ); // set the new value
+ }
}
if ( !$this->unlock( $key ) ) {
@@ -202,48 +222,116 @@ abstract class BagOStuff implements LoggerAwareInterface {
}
/**
+ * Acquire an advisory lock on a key string
+ *
+ * Note that if reentry is enabled, duplicate calls ignore $expiry
+ *
* @param string $key
- * @param int $timeout Lock wait timeout [optional]
- * @param int $expiry Lock expiry [optional]
+ * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
+ * @param int $expiry Lock expiry [optional]; 1 day maximum
+ * @param string $rclass Allow reentry if set and the current lock used this value
* @return bool Success
*/
- public function lock( $key, $timeout = 6, $expiry = 6 ) {
+ public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
+ // Avoid deadlocks and allow lock reentry if specified
+ if ( isset( $this->locks[$key] ) ) {
+ if ( $rclass != '' && $this->locks[$key]['class'] === $rclass ) {
+ ++$this->locks[$key]['depth'];
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ $expiry = min( $expiry ?: INF, 86400 );
+
$this->clearLastError();
$timestamp = microtime( true ); // starting UNIX timestamp
if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
- return true;
- } elseif ( $this->getLastError() ) {
- return false;
+ $locked = true;
+ } elseif ( $this->getLastError() || $timeout <= 0 ) {
+ $locked = false; // network partition or non-blocking
+ } else {
+ $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
+ $sleep = 2 * $uRTT; // rough time to do get()+set()
+
+ $attempts = 0; // failed attempts
+ do {
+ if ( ++$attempts >= 3 && $sleep <= 5e5 ) {
+ // Exponentially back off after failed attempts to avoid network spam.
+ // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
+ $sleep *= 2;
+ }
+ usleep( $sleep ); // back off
+ $this->clearLastError();
+ $locked = $this->add( "{$key}:lock", 1, $expiry );
+ if ( $this->getLastError() ) {
+ $locked = false; // network partition
+ break;
+ }
+ } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
}
- $uRTT = ceil( 1e6 * ( microtime( true ) - $timestamp ) ); // estimate RTT (us)
- $sleep = 2 * $uRTT; // rough time to do get()+set()
-
- $locked = false; // lock acquired
- $attempts = 0; // failed attempts
- do {
- if ( ++$attempts >= 3 && $sleep <= 5e5 ) {
- // Exponentially back off after failed attempts to avoid network spam.
- // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
- $sleep *= 2;
- }
- usleep( $sleep ); // back off
- $this->clearLastError();
- $locked = $this->add( "{$key}:lock", 1, $expiry );
- if ( $this->getLastError() ) {
- return false;
- }
- } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
+ if ( $locked ) {
+ $this->locks[$key] = array( 'class' => $rclass, 'depth' => 1 );
+ }
return $locked;
}
/**
+ * Release an advisory lock on a key string
+ *
* @param string $key
* @return bool Success
*/
public function unlock( $key ) {
- return $this->delete( "{$key}:lock" );
+ if ( isset( $this->locks[$key] ) && --$this->locks[$key]['depth'] <= 0 ) {
+ unset( $this->locks[$key] );
+
+ return $this->delete( "{$key}:lock" );
+ }
+
+ return true;
+ }
+
+ /**
+ * Get a lightweight exclusive self-unlocking lock
+ *
+ * Note that the same lock cannot be acquired twice.
+ *
+ * This is useful for task de-duplication or to avoid obtrusive
+ * (though non-corrupting) DB errors like INSERT key conflicts
+ * or deadlocks when using LOCK IN SHARE MODE.
+ *
+ * @param string $key
+ * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
+ * @param int $expiry Lock expiry [optional]; 1 day maximum
+ * @param string $rclass Allow reentry if set and the current lock used this value
+ * @return ScopedCallback|null Returns null on failure
+ * @since 1.26
+ */
+ final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
+ $expiry = min( $expiry ?: INF, 86400 );
+
+ if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
+ return null;
+ }
+
+ $lSince = microtime( true ); // lock timestamp
+ // PHP 5.3: Can't use $this in a closure
+ $that = $this;
+ $logger = $this->logger;
+
+ return new ScopedCallback( function() use ( $that, $logger, $key, $lSince, $expiry ) {
+ $latency = .050; // latency skew (err towards keeping lock present)
+ $age = ( microtime( true ) - $lSince + $latency );
+ if ( ( $age + $latency ) >= $expiry ) {
+ $logger->warning( "Lock for $key held too long ($age sec)." );
+ return; // expired; it's not "safe" to delete the key
+ }
+ $that->unlock( $key );
+ } );
}
/**
@@ -260,14 +348,13 @@ abstract class BagOStuff implements LoggerAwareInterface {
return false;
}
- /* *** Emulated functions *** */
-
/**
* Get an associative array containing the item for each of the keys that have items.
* @param array $keys List of strings
+ * @param integer $flags Bitfield; supports READ_LATEST [optional]
* @return array
*/
- public function getMulti( array $keys ) {
+ public function getMulti( array $keys, $flags = 0 ) {
$res = array();
foreach ( $keys as $key ) {
$val = $this->get( $key );
@@ -334,7 +421,7 @@ abstract class BagOStuff implements LoggerAwareInterface {
* Decrease stored value of $key by $value while preserving its TTL
* @param string $key
* @param int $value
- * @return int
+ * @return int|bool New value or false on failure
*/
public function decr( $key, $value = 1 ) {
return $this->incr( $key, - $value );
@@ -384,6 +471,24 @@ abstract class BagOStuff implements LoggerAwareInterface {
}
/**
+ * Modify a cache update operation array for EventRelayer::notify()
+ *
+ * This is used for relayed writes, e.g. for broadcasting a change
+ * to multiple data-centers. If the array contains a 'val' field
+ * then the command involves setting a key to that value. Note that
+ * for simplicity, 'val' is always a simple scalar value. This method
+ * is used to possibly serialize the value and add any cache-specific
+ * key/values needed for the relayer daemon (e.g. memcached flags).
+ *
+ * @param array $event
+ * @return array
+ * @since 1.26
+ */
+ public function modifySimpleRelayEvent( array $event ) {
+ return $event;
+ }
+
+ /**
* @param string $text
*/
protected function debug( $text ) {
diff --git a/includes/libs/objectcache/EmptyBagOStuff.php b/includes/libs/objectcache/EmptyBagOStuff.php
index 4ccf2707..55e84b05 100644
--- a/includes/libs/objectcache/EmptyBagOStuff.php
+++ b/includes/libs/objectcache/EmptyBagOStuff.php
@@ -27,7 +27,7 @@
* @ingroup Cache
*/
class EmptyBagOStuff extends BagOStuff {
- public function get( $key, &$casToken = null ) {
+ public function get( $key, &$casToken = null, $flags = 0 ) {
return false;
}
diff --git a/includes/libs/objectcache/HashBagOStuff.php b/includes/libs/objectcache/HashBagOStuff.php
index 2c8b05a5..b685e41f 100644
--- a/includes/libs/objectcache/HashBagOStuff.php
+++ b/includes/libs/objectcache/HashBagOStuff.php
@@ -48,7 +48,7 @@ class HashBagOStuff extends BagOStuff {
return true;
}
- public function get( $key, &$casToken = null ) {
+ public function get( $key, &$casToken = null, $flags = 0 ) {
if ( !isset( $this->bag[$key] ) ) {
return false;
}
@@ -68,20 +68,8 @@ class HashBagOStuff extends BagOStuff {
}
function delete( $key ) {
- if ( !isset( $this->bag[$key] ) ) {
- return false;
- }
-
unset( $this->bag[$key] );
return true;
}
-
- public function lock( $key, $timeout = 6, $expiry = 6 ) {
- return true;
- }
-
- function unlock( $key ) {
- return true;
- }
}
diff --git a/includes/libs/objectcache/ReplicatedBagOStuff.php b/includes/libs/objectcache/ReplicatedBagOStuff.php
new file mode 100644
index 00000000..9e80e9fd
--- /dev/null
+++ b/includes/libs/objectcache/ReplicatedBagOStuff.php
@@ -0,0 +1,129 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Cache
+ * @author Aaron Schulz
+ */
+
+/**
+ * A cache class that directs writes to one set of servers and reads to
+ * another. This assumes that the servers used for reads are setup to slave
+ * those that writes go to. This can easily be used with redis for example.
+ *
+ * In the WAN scenario (e.g. multi-datacenter case), this is useful when
+ * writes are rare or they usually take place in the primary datacenter.
+ *
+ * @ingroup Cache
+ * @since 1.26
+ */
+class ReplicatedBagOStuff extends BagOStuff {
+ /** @var BagOStuff */
+ protected $writeStore;
+ /** @var BagOStuff */
+ protected $readStore;
+
+ /**
+ * Constructor. Parameters are:
+ * - writeFactory : ObjectFactory::getObjectFromSpec parameters yeilding BagOStuff.
+ * This object will be used for writes (e.g. the master DB).
+ * - readFactory : ObjectFactory::getObjectFromSpec parameters yeilding BagOStuff.
+ * This object will be used for reads (e.g. a slave DB).
+ *
+ * @param array $params
+ * @throws InvalidArgumentException
+ */
+ public function __construct( $params ) {
+ parent::__construct( $params );
+
+ if ( !isset( $params['writeFactory'] ) ) {
+ throw new InvalidArgumentException(
+ __METHOD__ . ': the "writeFactory" parameter is required' );
+ }
+ if ( !isset( $params['readFactory'] ) ) {
+ throw new InvalidArgumentException(
+ __METHOD__ . ': the "readFactory" parameter is required' );
+ }
+
+ $this->writeStore = ( $params['writeFactory'] instanceof BagOStuff )
+ ? $params['writeFactory']
+ : ObjectFactory::getObjectFromSpec( $params['writeFactory'] );
+ $this->readStore = ( $params['readFactory'] instanceof BagOStuff )
+ ? $params['readFactory']
+ : ObjectFactory::getObjectFromSpec( $params['readFactory'] );
+ }
+
+ public function setDebug( $debug ) {
+ $this->writeStore->setDebug( $debug );
+ $this->readStore->setDebug( $debug );
+ }
+
+ public function get( $key, &$casToken = null, $flags = 0 ) {
+ return ( $flags & self::READ_LATEST )
+ ? $this->writeStore->get( $key, $casToken, $flags )
+ : $this->readStore->get( $key, $casToken, $flags );
+ }
+
+ public function getMulti( array $keys, $flags = 0 ) {
+ return ( $flags & self::READ_LATEST )
+ ? $this->writeStore->getMulti( $keys, $flags )
+ : $this->readStore->getMulti( $keys, $flags );
+ }
+
+ public function set( $key, $value, $exptime = 0 ) {
+ return $this->writeStore->set( $key, $value, $exptime );
+ }
+
+ public function delete( $key ) {
+ return $this->writeStore->delete( $key );
+ }
+
+ public function add( $key, $value, $exptime = 0 ) {
+ return $this->writeStore->add( $key, $value, $exptime );
+ }
+
+ public function incr( $key, $value = 1 ) {
+ return $this->writeStore->incr( $key, $value );
+ }
+
+ public function decr( $key, $value = 1 ) {
+ return $this->writeStore->decr( $key, $value );
+ }
+
+ public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
+ return $this->writeStore->lock( $key, $timeout, $expiry, $rclass );
+ }
+
+ public function unlock( $key ) {
+ return $this->writeStore->unlock( $key );
+ }
+
+ public function merge( $key, $callback, $exptime = 0, $attempts = 10 ) {
+ return $this->writeStore->merge( $key, $callback, $exptime, $attempts );
+ }
+
+ public function getLastError() {
+ return ( $this->writeStore->getLastError() != self::ERR_NONE )
+ ? $this->writeStore->getLastError()
+ : $this->readStore->getLastError();
+ }
+
+ public function clearLastError() {
+ $this->writeStore->clearLastError();
+ $this->readStore->clearLastError();
+ }
+}
diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php
new file mode 100644
index 00000000..2d921a70
--- /dev/null
+++ b/includes/libs/objectcache/WANObjectCache.php
@@ -0,0 +1,746 @@
+<?php
+/**
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
+ * @ingroup Cache
+ * @author Aaron Schulz
+ */
+
+/**
+ * Multi-datacenter aware caching interface
+ *
+ * All operations go to the local cache, except the delete()
+ * and touchCheckKey(), which broadcast to all clusters.
+ * This class is intended for caching data from primary stores.
+ * If the get() method does not return a value, then the caller
+ * should query the new value and backfill the cache using set().
+ * When the source data changes, the delete() method should be called.
+ * Since delete() is expensive, it should be avoided. One can do so if:
+ * - a) The object cached is immutable; or
+ * - b) Validity is checked against the source after get(); or
+ * - c) Using a modest TTL is reasonably correct and performant
+ * Consider using getWithSetCallback() instead of the get()/set() cycle.
+ *
+ * Instances of this class must be configured to point to a valid
+ * PubSub endpoint, and there must be listeners on the cache servers
+ * that subscribe to the endpoint and update the caches.
+ *
+ * Broadcasted operations like delete() and touchCheckKey() are done
+ * synchronously in the local cluster, but are relayed asynchronously.
+ * This means that callers in other datacenters will see older values
+ * for a however many milliseconds the datacenters are apart. As with
+ * any cache, this should not be relied on for cases where reads are
+ * used to determine writes to source (e.g. non-cache) data stores.
+ *
+ * All values are wrapped in metadata arrays. Keys use a "WANCache:" prefix
+ * to avoid collisions with keys that are not wrapped as metadata arrays. The
+ * prefixes are as follows:
+ * - a) "WANCache:v" : used for regular value keys
+ * - b) "WANCache:s" : used for temporarily storing values of tombstoned keys
+ * - c) "WANCache:t" : used for storing timestamp "check" keys
+ *
+ * @ingroup Cache
+ * @since 1.26
+ */
+class WANObjectCache {
+ /** @var BagOStuff The local cluster cache */
+ protected $cache;
+ /** @var string Cache pool name */
+ protected $pool;
+ /** @var EventRelayer */
+ protected $relayer;
+
+ /** @var int */
+ protected $lastRelayError = self::ERR_NONE;
+
+ /** Seconds to tombstone keys on delete() */
+ const HOLDOFF_TTL = 10;
+ /** Seconds to keep dependency purge keys around */
+ const CHECK_KEY_TTL = 31536000; // 1 year
+ /** Seconds to keep lock keys around */
+ const LOCK_TTL = 5;
+ /** Default remaining TTL at which to consider pre-emptive regeneration */
+ const LOW_TTL = 10;
+ /** Default TTL for temporarily caching tombstoned keys */
+ const TEMP_TTL = 5;
+
+ /** Idiom for set()/getWithSetCallback() TTL */
+ const TTL_NONE = 0;
+ /** Idiom for getWithSetCallback() callbacks to avoid calling set() */
+ const TTL_UNCACHEABLE = -1;
+
+ /** Cache format version number */
+ const VERSION = 1;
+
+ /** Fields of value holder arrays */
+ const FLD_VERSION = 0;
+ const FLD_VALUE = 1;
+ const FLD_TTL = 2;
+ const FLD_TIME = 3;
+
+ /** Possible values for getLastError() */
+ const ERR_NONE = 0; // no error
+ const ERR_NO_RESPONSE = 1; // no response
+ const ERR_UNREACHABLE = 2; // can't connect
+ const ERR_UNEXPECTED = 3; // response gave some error
+ const ERR_RELAY = 4; // relay broadcast failed
+
+ const VALUE_KEY_PREFIX = 'WANCache:v:';
+ const STASH_KEY_PREFIX = 'WANCache:s:';
+ const TIME_KEY_PREFIX = 'WANCache:t:';
+
+ const PURGE_VAL_PREFIX = 'PURGED:';
+
+ /**
+ * @param array $params
+ * - cache : BagOStuff object
+ * - pool : pool name
+ * - relayer : EventRelayer object
+ */
+ public function __construct( array $params ) {
+ $this->cache = $params['cache'];
+ $this->pool = $params['pool'];
+ $this->relayer = $params['relayer'];
+ }
+
+ /**
+ * @return WANObjectCache Cache that wraps EmptyBagOStuff
+ */
+ public static function newEmpty() {
+ return new self( array(
+ 'cache' => new EmptyBagOStuff(),
+ 'pool' => 'empty',
+ 'relayer' => new EventRelayerNull( array() )
+ ) );
+ }
+
+ /**
+ * Fetch the value of a key from cache
+ *
+ * If passed in, $curTTL is set to the remaining TTL (current time left):
+ * - a) INF; if the key exists, has no TTL, and is not expired by $checkKeys
+ * - b) float (>=0); if the key exists, has a TTL, and is not expired by $checkKeys
+ * - c) float (<0); if the key is tombstoned or existing but expired by $checkKeys
+ * - d) null; if the key does not exist and is not tombstoned
+ *
+ * If a key is tombstoned, $curTTL will reflect the time since delete().
+ *
+ * The timestamp of $key will be checked against the last-purge timestamp
+ * of each of $checkKeys. Those $checkKeys not in cache will have the last-purge
+ * initialized to the current timestamp. If any of $checkKeys have a timestamp
+ * greater than that of $key, then $curTTL will reflect how long ago $key
+ * became invalid. Callers can use $curTTL to know when the value is stale.
+ * The $checkKeys parameter allow mass invalidations by updating a single key:
+ * - a) Each "check" key represents "last purged" of some source data
+ * - b) Callers pass in relevant "check" keys as $checkKeys in get()
+ * - c) When the source data that "check" keys represent changes,
+ * the touchCheckKey() method is called on them
+ *
+ * For keys that are hot/expensive, consider using getWithSetCallback() instead.
+ *
+ * @param string $key Cache key
+ * @param mixed $curTTL Approximate TTL left on the key if present [returned]
+ * @param array $checkKeys List of "check" keys
+ * @return mixed Value of cache key or false on failure
+ */
+ final public function get( $key, &$curTTL = null, array $checkKeys = array() ) {
+ $curTTLs = array();
+ $values = $this->getMulti( array( $key ), $curTTLs, $checkKeys );
+ $curTTL = isset( $curTTLs[$key] ) ? $curTTLs[$key] : null;
+
+ return isset( $values[$key] ) ? $values[$key] : false;
+ }
+
+ /**
+ * Fetch the value of several keys from cache
+ *
+ * @see WANObjectCache::get()
+ *
+ * @param array $keys List of cache keys
+ * @param array $curTTLs Map of (key => approximate TTL left) for existing keys [returned]
+ * @param array $checkKeys List of "check" keys
+ * @return array Map of (key => value) for keys that exist
+ */
+ final public function getMulti(
+ array $keys, &$curTTLs = array(), array $checkKeys = array()
+ ) {
+ $result = array();
+ $curTTLs = array();
+
+ $vPrefixLen = strlen( self::VALUE_KEY_PREFIX );
+ $valueKeys = self::prefixCacheKeys( $keys, self::VALUE_KEY_PREFIX );
+ $checkKeys = self::prefixCacheKeys( $checkKeys, self::TIME_KEY_PREFIX );
+
+ // Fetch all of the raw values
+ $wrappedValues = $this->cache->getMulti( array_merge( $valueKeys, $checkKeys ) );
+ $now = microtime( true );
+
+ // Get/initialize the timestamp of all the "check" keys
+ $checkKeyTimes = array();
+ foreach ( $checkKeys as $checkKey ) {
+ $timestamp = isset( $wrappedValues[$checkKey] )
+ ? self::parsePurgeValue( $wrappedValues[$checkKey] )
+ : false;
+ if ( !is_float( $timestamp ) ) {
+ // Key is not set or invalid; regenerate
+ $this->cache->add( $checkKey,
+ self::PURGE_VAL_PREFIX . $now, self::CHECK_KEY_TTL );
+ $timestamp = $now;
+ }
+
+ $checkKeyTimes[] = $timestamp;
+ }
+
+ // Get the main cache value for each key and validate them
+ foreach ( $valueKeys as $vKey ) {
+ if ( !isset( $wrappedValues[$vKey] ) ) {
+ continue; // not found
+ }
+
+ $key = substr( $vKey, $vPrefixLen ); // unprefix
+
+ list( $value, $curTTL ) = $this->unwrap( $wrappedValues[$vKey], $now );
+ if ( $value !== false ) {
+ $result[$key] = $value;
+ foreach ( $checkKeyTimes as $checkKeyTime ) {
+ // Force dependant keys to be invalid for a while after purging
+ // to reduce race conditions involving stale data getting cached
+ $safeTimestamp = $checkKeyTime + self::HOLDOFF_TTL;
+ if ( $safeTimestamp >= $wrappedValues[$vKey][self::FLD_TIME] ) {
+ $curTTL = min( $curTTL, $checkKeyTime - $now );
+ }
+ }
+ }
+
+ $curTTLs[$key] = $curTTL;
+ }
+
+ return $result;
+ }
+
+ /**
+ * Set the value of a key from cache
+ *
+ * Simply calling this method when source data changes is not valid because
+ * the changes do not replicate to the other WAN sites. In that case, delete()
+ * should be used instead. This method is intended for use on cache misses.
+ *
+ * @param string $key Cache key
+ * @param mixed $value
+ * @param integer $ttl Seconds to live [0=forever]
+ * @return bool Success
+ */
+ final public function set( $key, $value, $ttl = 0 ) {
+ $key = self::VALUE_KEY_PREFIX . $key;
+ $wrapped = $this->wrap( $value, $ttl );
+
+ $func = function ( $cache, $key, $cWrapped ) use ( $wrapped ) {
+ return ( is_string( $cWrapped ) )
+ ? false // key is tombstoned; do nothing
+ : $wrapped;
+ };
+
+ return $this->cache->merge( $key, $func, $ttl, 1 );
+ }
+
+ /**
+ * Purge a key from all clusters
+ *
+ * This should only be called when the underlying data (being cached)
+ * changes in a significant way. This deletes the key and starts a hold-off
+ * period where the key cannot be written to for a few seconds (HOLDOFF_TTL).
+ * This is done to avoid the following race condition:
+ * a) Some DB data changes and delete() is called on a corresponding key
+ * b) A request refills the key with a stale value from a lagged DB
+ * c) The stale value is stuck there until the key is expired/evicted
+ *
+ * This is implemented by storing a special "tombstone" value at the cache
+ * key that this class recognizes; get() calls will return false for the key
+ * and any set() calls will refuse to replace tombstone values at the key.
+ * For this to always avoid writing stale values, the following must hold:
+ * a) Replication lag is bounded to being less than HOLDOFF_TTL; or
+ * b) If lag is higher, the DB will have gone into read-only mode already
+ *
+ * If called twice on the same key, then the last hold-off TTL takes
+ * precedence. For idempotence, the $ttl should not vary for different
+ * delete() calls on the same key. Also note that lowering $ttl reduces
+ * the effective range of the 'lockTSE' parameter to getWithSetCallback().
+ *
+ * @param string $key Cache key
+ * @param integer $ttl How long to block writes to the key [seconds]
+ * @return bool True if the item was purged or not found, false on failure
+ */
+ final public function delete( $key, $ttl = self::HOLDOFF_TTL ) {
+ $key = self::VALUE_KEY_PREFIX . $key;
+ // Avoid indefinite key salting for sanity
+ $ttl = max( $ttl, 1 );
+ // Update the local cluster immediately
+ $ok = $this->cache->set( $key, self::PURGE_VAL_PREFIX . microtime( true ), $ttl );
+ // Publish the purge to all clusters
+ return $this->relayPurge( $key, $ttl ) && $ok;
+ }
+
+ /**
+ * Fetch the value of a timestamp "check" key
+ *
+ * The key will be *initialized* to the current time if not set,
+ * so only call this method if this behavior is actually desired
+ *
+ * The timestamp can be used to check whether a cached value is valid.
+ * Callers should not assume that this returns the same timestamp in
+ * all datacenters due to relay delays.
+ *
+ * The level of staleness can roughly be estimated from this key, but
+ * if the key was evicted from cache, such calculations may show the
+ * time since expiry as ~0 seconds.
+ *
+ * Note that "check" keys won't collide with other regular keys
+ *
+ * @param string $key
+ * @return float UNIX timestamp of the key
+ */
+ final public function getCheckKeyTime( $key ) {
+ $key = self::TIME_KEY_PREFIX . $key;
+
+ $time = self::parsePurgeValue( $this->cache->get( $key ) );
+ if ( $time === false ) {
+ // Casting assures identical floats for the next getCheckKeyTime() calls
+ $time = (string)microtime( true );
+ $this->cache->add( $key, self::PURGE_VAL_PREFIX . $time, self::CHECK_KEY_TTL );
+ $time = (float)$time;
+ }
+
+ return $time;
+ }
+
+ /**
+ * Purge a "check" key from all clusters, invalidating keys that use it
+ *
+ * This should only be called when the underlying data (being cached)
+ * changes in a significant way, and it is impractical to call delete()
+ * on all keys that should be changed. When get() is called on those
+ * keys, the relevant "check" keys must be supplied for this to work.
+ *
+ * The "check" key essentially represents a last-modified field.
+ * It is set in the future a few seconds when this is called, to
+ * avoid race conditions where dependent keys get updated with a
+ * stale value (e.g. from a DB slave).
+ *
+ * This is typically useful for keys with static names or some cases
+ * dynamically generated names where a low number of combinations exist.
+ * When a few important keys get a large number of hits, a high cache
+ * time is usually desired as well as lockTSE logic. The resetCheckKey()
+ * method is less appropriate in such cases since the "time since expiry"
+ * cannot be inferred.
+ *
+ * Note that "check" keys won't collide with other regular keys
+ *
+ * @see WANObjectCache::get()
+ *
+ * @param string $key Cache key
+ * @return bool True if the item was purged or not found, false on failure
+ */
+ final public function touchCheckKey( $key ) {
+ $key = self::TIME_KEY_PREFIX . $key;
+ // Update the local cluster immediately
+ $ok = $this->cache->set( $key,
+ self::PURGE_VAL_PREFIX . microtime( true ), self::CHECK_KEY_TTL );
+ // Publish the purge to all clusters
+ return $this->relayPurge( $key, self::CHECK_KEY_TTL ) && $ok;
+ }
+
+ /**
+ * Delete a "check" key from all clusters, invalidating keys that use it
+ *
+ * This is similar to touchCheckKey() in that keys using it via
+ * getWithSetCallback() will be invalidated. The differences are:
+ * a) The timestamp will be deleted from all caches and lazily
+ * re-initialized when accessed (rather than set everywhere)
+ * b) Thus, dependent keys will be known to be invalid, but not
+ * for how long (they are treated as "just" purged), which
+ * effects any lockTSE logic in getWithSetCallback()
+ * The advantage is that this does not place high TTL keys on every cache
+ * server, making it better for code that will cache many different keys
+ * and either does not use lockTSE or uses a low enough TTL anyway.
+ *
+ * This is typically useful for keys with dynamically generated names
+ * where a high number of combinations exist.
+ *
+ * Note that "check" keys won't collide with other regular keys
+ *
+ * @see WANObjectCache::touchCheckKey()
+ * @see WANObjectCache::get()
+ *
+ * @param string $key Cache key
+ * @return bool True if the item was purged or not found, false on failure
+ */
+ final public function resetCheckKey( $key ) {
+ $key = self::TIME_KEY_PREFIX . $key;
+ // Update the local cluster immediately
+ $ok = $this->cache->delete( $key );
+ // Publish the purge to all clusters
+ return $this->relayDelete( $key ) && $ok;
+ }
+
+ /**
+ * Method to fetch/regenerate cache keys
+ *
+ * On cache miss, the key will be set to the callback result,
+ * unless the callback returns false. The arguments supplied are:
+ * (current value or false, &$ttl)
+ * The callback function returns the new value given the current
+ * value (false if not present). Preemptive re-caching and $checkKeys
+ * can result in a non-false current value. The TTL of the new value
+ * can be set dynamically by altering $ttl in the callback (by reference).
+ *
+ * Usually, callbacks ignore the current value, but it can be used
+ * to maintain "most recent X" values that come from time or sequence
+ * based source data, provided that the "as of" id/time is tracked.
+ *
+ * Usage of $checkKeys is similar to get()/getMulti(). However,
+ * rather than the caller having to inspect a "current time left"
+ * variable (e.g. $curTTL, $curTTLs), a cache regeneration will be
+ * triggered using the callback.
+ *
+ * The simplest way to avoid stampedes for hot keys is to use
+ * the 'lockTSE' option in $opts. If cache purges are needed, also:
+ * a) Pass $key into $checkKeys
+ * b) Use touchCheckKey( $key ) instead of delete( $key )
+ * Following this pattern lets the old cache be used until a
+ * single thread updates it as needed. Also consider tweaking
+ * the 'lowTTL' parameter.
+ *
+ * Example usage:
+ * @code
+ * $key = wfMemcKey( 'cat-recent-actions', $catId );
+ * // Function that derives the new key value given the old value
+ * $callback = function( $cValue, &$ttl ) { ... };
+ * // Get the key value from cache or from source on cache miss;
+ * // try to only let one cluster thread manage doing cache updates
+ * $opts = array( 'lockTSE' => 5, 'lowTTL' => 10 );
+ * $value = $cache->getWithSetCallback( $key, $callback, 60, array(), $opts );
+ * @endcode
+ *
+ * Example usage:
+ * @code
+ * $key = wfMemcKey( 'cat-state', $catId );
+ * // The "check" keys that represent things the value depends on;
+ * // Calling touchCheckKey() on them invalidates "cat-state"
+ * $checkKeys = array(
+ * wfMemcKey( 'water-bowls', $houseId ),
+ * wfMemcKey( 'food-bowls', $houseId ),
+ * wfMemcKey( 'people-present', $houseId )
+ * );
+ * // Function that derives the new key value
+ * $callback = function() { ... };
+ * // Get the key value from cache or from source on cache miss;
+ * // try to only let one cluster thread manage doing cache updates
+ * $opts = array( 'lockTSE' => 5, 'lowTTL' => 10 );
+ * $value = $cache->getWithSetCallback( $key, $callback, 60, $checkKeys, $opts );
+ * @endcode
+ *
+ * @see WANObjectCache::get()
+ *
+ * @param string $key Cache key
+ * @param integer $ttl Seconds to live for key updates. Special values are:
+ * - WANObjectCache::TTL_NONE : Cache forever
+ * - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
+ * @param callable $callback Value generation function
+ * @param array $opts Options map:
+ * - checkKeys: List of "check" keys.
+ * - lowTTL: Consider pre-emptive updates when the current TTL (sec) of the key is less than
+ * this. It becomes more likely over time, becoming a certainty once the key is expired.
+ * Default: WANObjectCache::LOW_TTL seconds.
+ * - lockTSE: If the key is tombstoned or expired (by checkKeys) less than this many seconds
+ * ago, then try to have a single thread handle cache regeneration at any given time.
+ * Other threads will try to use stale values if possible. If, on miss, the time since
+ * expiration is low, the assumption is that the key is hot and that a stampede is worth
+ * avoiding. Setting this above WANObjectCache::HOLDOFF_TTL makes no difference. The
+ * higher this is set, the higher the worst-case staleness can be.
+ * Use WANObjectCache::TSE_NONE to disable this logic. Default: WANObjectCache::TSE_NONE.
+ * - tempTTL : TTL of the temp key used to cache values while a key is tombstoned.
+ * This avoids excessive regeneration of hot keys on delete() but may
+ * result in stale values.
+ * @return mixed Value to use for the key
+ */
+ final public function getWithSetCallback(
+ $key, $ttl, $callback, array $opts = array(), $oldOpts = array()
+ ) {
+ // Back-compat with 1.26: Swap $ttl and $callback
+ if ( is_int( $callback ) ) {
+ $temp = $ttl;
+ $ttl = $callback;
+ $callback = $temp;
+ }
+ // Back-compat with 1.26: $checkKeys as separate parameter
+ if ( $oldOpts || ( is_array( $opts ) && isset( $opts[0] ) ) ) {
+ $checkKeys = $opts;
+ $opts = $oldOpts;
+ } else {
+ $checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : array();
+ }
+
+ $lowTTL = isset( $opts['lowTTL'] ) ? $opts['lowTTL'] : min( self::LOW_TTL, $ttl );
+ $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : -1;
+ $tempTTL = isset( $opts['tempTTL'] ) ? $opts['tempTTL'] : self::TEMP_TTL;
+
+ // Get the current key value
+ $curTTL = null;
+ $cValue = $this->get( $key, $curTTL, $checkKeys ); // current value
+ $value = $cValue; // return value
+
+ // Determine if a regeneration is desired
+ if ( $value !== false && $curTTL > 0 && !$this->worthRefresh( $curTTL, $lowTTL ) ) {
+ return $value;
+ }
+
+ // A deleted key with a negative TTL left must be tombstoned
+ $isTombstone = ( $curTTL !== null && $value === false );
+ // Assume a key is hot if requested soon after invalidation
+ $isHot = ( $curTTL !== null && $curTTL <= 0 && abs( $curTTL ) <= $lockTSE );
+
+ $lockAcquired = false;
+ if ( $isHot ) {
+ // Acquire a cluster-local non-blocking lock
+ if ( $this->cache->lock( $key, 0, self::LOCK_TTL ) ) {
+ // Lock acquired; this thread should update the key
+ $lockAcquired = true;
+ } elseif ( $value !== false ) {
+ // If it cannot be acquired; then the stale value can be used
+ return $value;
+ }
+ }
+
+ if ( !$lockAcquired && ( $isTombstone || $isHot ) ) {
+ // Use the stash value for tombstoned keys to reduce regeneration load.
+ // For hot keys, either another thread has the lock or the lock failed;
+ // use the stash value from the last thread that regenerated it.
+ $value = $this->cache->get( self::STASH_KEY_PREFIX . $key );
+ if ( $value !== false ) {
+ return $value;
+ }
+ }
+
+ if ( !is_callable( $callback ) ) {
+ throw new InvalidArgumentException( "Invalid cache miss callback provided." );
+ }
+
+ // Generate the new value from the callback...
+ $value = call_user_func_array( $callback, array( $cValue, &$ttl ) );
+ // When delete() is called, writes are write-holed by the tombstone,
+ // so use a special stash key to pass the new value around threads.
+ if ( $value !== false && ( $isHot || $isTombstone ) && $ttl >= 0 ) {
+ $this->cache->set( self::STASH_KEY_PREFIX . $key, $value, $tempTTL );
+ }
+
+ if ( $lockAcquired ) {
+ $this->cache->unlock( $key );
+ }
+
+ if ( $value !== false && $ttl >= 0 ) {
+ // Update the cache; this will fail if the key is tombstoned
+ $this->set( $key, $value, $ttl );
+ }
+
+ return $value;
+ }
+
+ /**
+ * Get the "last error" registered; clearLastError() should be called manually
+ * @return int ERR_* constant for the "last error" registry
+ */
+ final public function getLastError() {
+ if ( $this->lastRelayError ) {
+ // If the cache and the relayer failed, focus on the later.
+ // An update not making it to the relayer means it won't show up
+ // in other DCs (nor will consistent re-hashing see up-to-date values).
+ // On the other hand, if just the cache update failed, then it should
+ // eventually be applied by the relayer.
+ return $this->lastRelayError;
+ }
+
+ $code = $this->cache->getLastError();
+ switch ( $code ) {
+ case BagOStuff::ERR_NONE:
+ return self::ERR_NONE;
+ case BagOStuff::ERR_NO_RESPONSE:
+ return self::ERR_NO_RESPONSE;
+ case BagOStuff::ERR_UNREACHABLE:
+ return self::ERR_UNREACHABLE;
+ default:
+ return self::ERR_UNEXPECTED;
+ }
+ }
+
+ /**
+ * Clear the "last error" registry
+ */
+ final public function clearLastError() {
+ $this->cache->clearLastError();
+ $this->lastRelayError = self::ERR_NONE;
+ }
+
+ /**
+ * Do the actual async bus purge of a key
+ *
+ * This must set the key to "PURGED:<UNIX timestamp>"
+ *
+ * @param string $key Cache key
+ * @param integer $ttl How long to keep the tombstone [seconds]
+ * @return bool Success
+ */
+ protected function relayPurge( $key, $ttl ) {
+ $event = $this->cache->modifySimpleRelayEvent( array(
+ 'cmd' => 'set',
+ 'key' => $key,
+ 'val' => 'PURGED:$UNIXTIME$',
+ 'ttl' => max( $ttl, 1 ),
+ 'sbt' => true, // substitute $UNIXTIME$ with actual microtime
+ ) );
+
+ $ok = $this->relayer->notify( "{$this->pool}:purge", $event );
+ if ( !$ok ) {
+ $this->lastRelayError = self::ERR_RELAY;
+ }
+
+ return $ok;
+ }
+
+ /**
+ * Do the actual async bus delete of a key
+ *
+ * @param string $key Cache key
+ * @return bool Success
+ */
+ protected function relayDelete( $key ) {
+ $event = $this->cache->modifySimpleRelayEvent( array(
+ 'cmd' => 'delete',
+ 'key' => $key,
+ ) );
+
+ $ok = $this->relayer->notify( "{$this->pool}:purge", $event );
+ if ( !$ok ) {
+ $this->lastRelayError = self::ERR_RELAY;
+ }
+
+ return $ok;
+ }
+
+ /**
+ * Check if a key should be regenerated (using random probability)
+ *
+ * This returns false if $curTTL >= $lowTTL. Otherwise, the chance
+ * of returning true increases steadily from 0% to 100% as the $curTTL
+ * moves from $lowTTL to 0 seconds. This handles widely varying
+ * levels of cache access traffic.
+ *
+ * @param float $curTTL Approximate TTL left on the key if present
+ * @param float $lowTTL Consider a refresh when $curTTL is less than this
+ * @return bool
+ */
+ protected function worthRefresh( $curTTL, $lowTTL ) {
+ if ( $curTTL >= $lowTTL ) {
+ return false;
+ } elseif ( $curTTL <= 0 ) {
+ return true;
+ }
+
+ $chance = ( 1 - $curTTL / $lowTTL );
+
+ return mt_rand( 1, 1e9 ) <= 1e9 * $chance;
+ }
+
+ /**
+ * Do not use this method outside WANObjectCache
+ *
+ * @param mixed $value
+ * @param integer $ttl [0=forever]
+ * @return string
+ */
+ protected function wrap( $value, $ttl ) {
+ return array(
+ self::FLD_VERSION => self::VERSION,
+ self::FLD_VALUE => $value,
+ self::FLD_TTL => $ttl,
+ self::FLD_TIME => microtime( true )
+ );
+ }
+
+ /**
+ * Do not use this method outside WANObjectCache
+ *
+ * @param array|string|bool $wrapped
+ * @param float $now Unix Current timestamp (preferrable pre-query)
+ * @return array (mixed; false if absent/invalid, current time left)
+ */
+ protected function unwrap( $wrapped, $now ) {
+ // Check if the value is a tombstone
+ $purgeTimestamp = self::parsePurgeValue( $wrapped );
+ if ( is_float( $purgeTimestamp ) ) {
+ // Purged values should always have a negative current $ttl
+ $curTTL = min( -0.000001, $purgeTimestamp - $now );
+ return array( false, $curTTL );
+ }
+
+ if ( !is_array( $wrapped ) // not found
+ || !isset( $wrapped[self::FLD_VERSION] ) // wrong format
+ || $wrapped[self::FLD_VERSION] !== self::VERSION // wrong version
+ ) {
+ return array( false, null );
+ }
+
+ if ( $wrapped[self::FLD_TTL] > 0 ) {
+ // Get the approximate time left on the key
+ $age = $now - $wrapped[self::FLD_TIME];
+ $curTTL = max( $wrapped[self::FLD_TTL] - $age, 0.0 );
+ } else {
+ // Key had no TTL, so the time left is unbounded
+ $curTTL = INF;
+ }
+
+ return array( $wrapped[self::FLD_VALUE], $curTTL );
+ }
+
+ /**
+ * @param array $keys
+ * @param string $prefix
+ * @return string[]
+ */
+ protected static function prefixCacheKeys( array $keys, $prefix ) {
+ $res = array();
+ foreach ( $keys as $key ) {
+ $res[] = $prefix . $key;
+ }
+
+ return $res;
+ }
+
+ /**
+ * @param string $value String like "PURGED:<timestamp>"
+ * @return float|bool UNIX timestamp or false on failure
+ */
+ protected static function parsePurgeValue( $value ) {
+ $m = array();
+ if ( is_string( $value ) &&
+ preg_match( '/^' . self::PURGE_VAL_PREFIX . '([^:]+)$/', $value, $m )
+ ) {
+ return (float)$m[1];
+ } else {
+ return false;
+ }
+ }
+}
diff --git a/includes/libs/objectcache/WinCacheBagOStuff.php b/includes/libs/objectcache/WinCacheBagOStuff.php
index 53625746..c480aa08 100644
--- a/includes/libs/objectcache/WinCacheBagOStuff.php
+++ b/includes/libs/objectcache/WinCacheBagOStuff.php
@@ -28,15 +28,7 @@
* @ingroup Cache
*/
class WinCacheBagOStuff extends BagOStuff {
-
- /**
- * Get a value from the WinCache object cache
- *
- * @param string $key Cache key
- * @param int $casToken [optional] Cas token
- * @return mixed
- */
- public function get( $key, &$casToken = null ) {
+ public function get( $key, &$casToken = null, $flags = 0 ) {
$val = wincache_ucache_get( $key );
$casToken = $val;
@@ -48,14 +40,6 @@ class WinCacheBagOStuff extends BagOStuff {
return $val;
}
- /**
- * Store a value in the WinCache object cache
- *
- * @param string $key Cache key
- * @param mixed $value Value to store
- * @param int $expire Expiration time
- * @return bool
- */
public function set( $key, $value, $expire = 0 ) {
$result = wincache_ucache_set( $key, serialize( $value ), $expire );
@@ -64,25 +48,10 @@ class WinCacheBagOStuff extends BagOStuff {
return ( is_array( $result ) && $result === array() ) || $result;
}
- /**
- * Store a value in the WinCache object cache, race condition-safe
- *
- * @param int $casToken Cas token
- * @param string $key Cache key
- * @param int $value Object to store
- * @param int $exptime Expiration time
- * @return bool
- */
protected function cas( $casToken, $key, $value, $exptime = 0 ) {
return wincache_ucache_cas( $key, $casToken, serialize( $value ) );
}
- /**
- * Remove a value from the WinCache object cache
- *
- * @param string $key Cache key
- * @return bool
- */
public function delete( $key ) {
wincache_ucache_delete( $key );
diff --git a/includes/libs/objectcache/XCacheBagOStuff.php b/includes/libs/objectcache/XCacheBagOStuff.php
index cfee9236..9dbff6f1 100644
--- a/includes/libs/objectcache/XCacheBagOStuff.php
+++ b/includes/libs/objectcache/XCacheBagOStuff.php
@@ -28,14 +28,7 @@
* @ingroup Cache
*/
class XCacheBagOStuff extends BagOStuff {
- /**
- * Get a value from the XCache object cache
- *
- * @param string $key Cache key
- * @param mixed $casToken Cas token
- * @return mixed
- */
- public function get( $key, &$casToken = null ) {
+ public function get( $key, &$casToken = null, $flags = 0 ) {
$val = xcache_get( $key );
if ( is_string( $val ) ) {
@@ -51,14 +44,6 @@ class XCacheBagOStuff extends BagOStuff {
return $val;
}
- /**
- * Store a value in the XCache object cache
- *
- * @param string $key Cache key
- * @param mixed $value Object to store
- * @param int $expire Expiration time
- * @return bool
- */
public function set( $key, $value, $expire = 0 ) {
if ( !$this->isInteger( $value ) ) {
$value = serialize( $value );
@@ -68,12 +53,6 @@ class XCacheBagOStuff extends BagOStuff {
return true;
}
- /**
- * Remove a value from the XCache object cache
- *
- * @param string $key Cache key
- * @return bool
- */
public function delete( $key ) {
xcache_unset( $key );
return true;