2015-01-27 19:56:44 +00:00
|
|
|
<?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
|
|
|
|
|
*/
|
|
|
|
|
|
2015-10-21 05:55:51 +00:00
|
|
|
use Psr\Log\LoggerAwareInterface;
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* Multi-datacenter aware caching interface
|
|
|
|
|
*
|
2015-09-24 20:47:28 +00:00
|
|
|
* All operations go to the local datacenter cache, except for delete(),
|
2015-09-29 01:22:52 +00:00
|
|
|
* touchCheckKey(), and resetCheckKey(), which broadcast to all datacenters.
|
2015-09-24 20:47:28 +00:00
|
|
|
*
|
2015-01-27 19:56:44 +00:00
|
|
|
* 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().
|
2015-10-10 07:18:00 +00:00
|
|
|
* When querying the store on cache miss, the closest DB replica
|
|
|
|
|
* should be used. Try to avoid heavyweight DB master or quorum reads.
|
2015-09-24 20:47:28 +00:00
|
|
|
* When the source data changes, a purge method should be called.
|
|
|
|
|
* Since purges are expensive, they should be avoided. One can do so if:
|
2015-01-27 19:56:44 +00:00
|
|
|
* - 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
|
2015-10-10 07:18:00 +00:00
|
|
|
*
|
2015-09-24 20:47:28 +00:00
|
|
|
* The simplest purge method is delete().
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
2015-09-29 01:22:52 +00:00
|
|
|
* synchronously in the local datacenter, but are relayed asynchronously.
|
2015-01-27 19:56:44 +00:00
|
|
|
* This means that callers in other datacenters will see older values
|
2015-09-29 01:22:52 +00:00
|
|
|
* for however many milliseconds the datacenters are apart. As with
|
2015-01-27 19:56:44 +00:00
|
|
|
* 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
|
|
|
|
|
*/
|
2015-10-19 17:52:19 +00:00
|
|
|
class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
|
2015-09-29 01:22:52 +00:00
|
|
|
/** @var BagOStuff The local datacenter cache */
|
2015-01-27 19:56:44 +00:00
|
|
|
protected $cache;
|
2015-10-07 08:55:39 +00:00
|
|
|
/** @var HashBagOStuff Script instance PHP cache */
|
|
|
|
|
protected $procCache;
|
2015-01-27 19:56:44 +00:00
|
|
|
/** @var string Cache pool name */
|
|
|
|
|
protected $pool;
|
2015-10-10 07:18:00 +00:00
|
|
|
/** @var EventRelayer Bus that handles purge broadcasts */
|
2015-01-27 19:56:44 +00:00
|
|
|
protected $relayer;
|
2015-10-21 05:55:51 +00:00
|
|
|
/** @var LoggerInterface */
|
|
|
|
|
protected $logger;
|
2015-01-27 19:56:44 +00:00
|
|
|
|
2015-10-10 07:18:00 +00:00
|
|
|
/** @var int ERR_* constant for the "last error" registry */
|
2015-01-27 19:56:44 +00:00
|
|
|
protected $lastRelayError = self::ERR_NONE;
|
|
|
|
|
|
2015-09-24 21:47:30 +00:00
|
|
|
/** Max time expected to pass between delete() and DB commit finishing */
|
2015-10-01 02:40:09 +00:00
|
|
|
const MAX_COMMIT_DELAY = 3;
|
2015-10-25 20:42:21 +00:00
|
|
|
/** Max replication+snapshot lag before applying TTL_LAGGED or disallowing set() */
|
|
|
|
|
const MAX_READ_LAG = 7;
|
2015-01-27 19:56:44 +00:00
|
|
|
/** Seconds to tombstone keys on delete() */
|
2015-10-25 20:42:21 +00:00
|
|
|
const HOLDOFF_TTL = 11; // MAX_COMMIT_DELAY + MAX_READ_LAG + 1
|
2015-09-24 21:47:30 +00:00
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/** Seconds to keep dependency purge keys around */
|
2015-10-19 17:52:19 +00:00
|
|
|
const CHECK_KEY_TTL = self::TTL_YEAR;
|
2015-01-27 19:56:44 +00:00
|
|
|
/** Seconds to keep lock keys around */
|
2015-10-23 02:36:59 +00:00
|
|
|
const LOCK_TTL = 10;
|
2015-09-16 17:05:54 +00:00
|
|
|
/** Default remaining TTL at which to consider pre-emptive regeneration */
|
2015-10-07 06:50:24 +00:00
|
|
|
const LOW_TTL = 30;
|
2015-09-18 19:22:18 +00:00
|
|
|
/** Default time-since-expiry on a miss that makes a key "hot" */
|
|
|
|
|
const LOCK_TSE = 1;
|
2015-01-27 19:56:44 +00:00
|
|
|
|
2015-05-14 03:28:16 +00:00
|
|
|
/** Idiom for getWithSetCallback() callbacks to avoid calling set() */
|
|
|
|
|
const TTL_UNCACHEABLE = -1;
|
2015-09-18 19:22:18 +00:00
|
|
|
/** Idiom for getWithSetCallback() callbacks to 'lockTSE' logic */
|
|
|
|
|
const TSE_NONE = -1;
|
2015-10-01 02:40:09 +00:00
|
|
|
/** Max TTL to store keys when a data sourced is lagged */
|
|
|
|
|
const TTL_LAGGED = 30;
|
2015-11-24 05:41:12 +00:00
|
|
|
/** Idiom for delete() for "no hold-off" */
|
|
|
|
|
const HOLDOFF_NONE = 0;
|
2015-05-13 05:33:57 +00:00
|
|
|
|
2015-10-23 02:36:59 +00:00
|
|
|
/** Tiny negative float to use when CTL comes up >= 0 due to clock skew */
|
|
|
|
|
const TINY_NEGATIVE = -0.000001;
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/** Cache format version number */
|
|
|
|
|
const VERSION = 1;
|
|
|
|
|
|
|
|
|
|
const FLD_VERSION = 0;
|
|
|
|
|
const FLD_VALUE = 1;
|
|
|
|
|
const FLD_TTL = 2;
|
|
|
|
|
const FLD_TIME = 3;
|
2015-10-23 02:36:59 +00:00
|
|
|
const FLD_FLAGS = 4;
|
|
|
|
|
|
|
|
|
|
/** @var integer Treat this value as expired-on-arrival */
|
|
|
|
|
const FLG_STALE = 1;
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
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:';
|
|
|
|
|
|
2015-10-29 21:23:32 +00:00
|
|
|
const MAX_PC_KEYS = 1000; // max keys to keep in process cache
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* @param array $params
|
|
|
|
|
* - cache : BagOStuff object
|
|
|
|
|
* - pool : pool name
|
|
|
|
|
* - relayer : EventRelayer object
|
2015-10-21 05:55:51 +00:00
|
|
|
* - logger : LoggerInterface object
|
2015-01-27 19:56:44 +00:00
|
|
|
*/
|
|
|
|
|
public function __construct( array $params ) {
|
|
|
|
|
$this->cache = $params['cache'];
|
|
|
|
|
$this->pool = $params['pool'];
|
|
|
|
|
$this->relayer = $params['relayer'];
|
2015-10-29 21:23:32 +00:00
|
|
|
$this->procCache = new HashBagOStuff( array( 'maxKeys' => self::MAX_PC_KEYS ) );
|
2015-10-21 05:55:51 +00:00
|
|
|
$this->setLogger( isset( $params['logger'] ) ? $params['logger'] : new NullLogger() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function setLogger( LoggerInterface $logger ) {
|
|
|
|
|
$this->logger = $logger;
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
2015-04-27 21:13:02 +00:00
|
|
|
/**
|
2015-10-10 07:18:00 +00:00
|
|
|
* Get an instance that wraps EmptyBagOStuff
|
|
|
|
|
*
|
|
|
|
|
* @return WANObjectCache
|
2015-04-27 21:13:02 +00:00
|
|
|
*/
|
|
|
|
|
public static function newEmpty() {
|
|
|
|
|
return new self( array(
|
|
|
|
|
'cache' => new EmptyBagOStuff(),
|
|
|
|
|
'pool' => 'empty',
|
|
|
|
|
'relayer' => new EventRelayerNull( array() )
|
|
|
|
|
) );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* Fetch the value of a key from cache
|
|
|
|
|
*
|
2015-10-23 02:36:59 +00:00
|
|
|
* If supplied, $curTTL is set to the remaining TTL (current time left):
|
|
|
|
|
* - a) INF; if $key exists, has no TTL, and is not expired by $checkKeys
|
|
|
|
|
* - b) float (>=0); if $key exists, has a TTL, and is not expired by $checkKeys
|
|
|
|
|
* - c) float (<0); if $key is tombstoned, stale, or existing but expired by $checkKeys
|
|
|
|
|
* - d) null; if $key does not exist and is not tombstoned
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* 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
|
|
|
|
|
*
|
2015-09-22 19:54:24 +00:00
|
|
|
* Source data entities might exists in a DB that uses snapshot isolation
|
|
|
|
|
* (e.g. the default REPEATABLE-READ in innoDB). Even for mutable data, that
|
|
|
|
|
* isolation can largely be maintained by doing the following:
|
|
|
|
|
* - a) Calling delete() on entity change *and* creation, before DB commit
|
|
|
|
|
* - b) Keeping transaction duration shorter than delete() hold-off TTL
|
2015-10-01 08:50:09 +00:00
|
|
|
*
|
2015-09-29 01:22:52 +00:00
|
|
|
* However, pre-snapshot values might still be seen if an update was made
|
|
|
|
|
* in a remote datacenter but the purge from delete() didn't relay yet.
|
2015-09-22 19:54:24 +00:00
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Consider using getWithSetCallback() instead of get() and set() cycles.
|
2015-09-24 20:47:28 +00:00
|
|
|
* That method has cache slam avoiding features for hot/expensive keys.
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* @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]
|
2015-11-16 21:41:10 +00:00
|
|
|
* @param array $checkKeys List of check keys to apply to all $keys. May also apply "check"
|
|
|
|
|
* keys to specific cache keys only by using cache keys as keys in the $checkKeys array.
|
2015-01-27 19:56:44 +00:00
|
|
|
* @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 );
|
2015-11-16 21:41:10 +00:00
|
|
|
|
|
|
|
|
$checksForAll = array();
|
|
|
|
|
$checksByKey = array();
|
|
|
|
|
$checkKeysFlat = array();
|
|
|
|
|
foreach ( $checkKeys as $i => $keys ) {
|
|
|
|
|
$prefixed = self::prefixCacheKeys( (array)$keys, self::TIME_KEY_PREFIX );
|
|
|
|
|
$checkKeysFlat = array_merge( $checkKeysFlat, $prefixed );
|
|
|
|
|
// Is this check keys for a specific cache key, or for all keys being fetched?
|
|
|
|
|
if ( is_int( $i ) ) {
|
|
|
|
|
$checksForAll = array_merge( $checksForAll, $prefixed );
|
|
|
|
|
} else {
|
|
|
|
|
$checksByKey[$i] = isset( $checksByKey[$i] )
|
|
|
|
|
? array_merge( $checksByKey[$i], $prefixed )
|
|
|
|
|
: $prefixed;
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
// Fetch all of the raw values
|
2015-11-16 21:41:10 +00:00
|
|
|
$wrappedValues = $this->cache->getMulti( array_merge( $valueKeys, $checkKeysFlat ) );
|
2015-01-27 19:56:44 +00:00
|
|
|
$now = microtime( true );
|
|
|
|
|
|
2015-11-16 21:41:10 +00:00
|
|
|
// Collect timestamps from all "check" keys
|
|
|
|
|
$checkKeyTimesForAll = $this->processCheckKeys( $checksForAll, $wrappedValues, $now );
|
|
|
|
|
$checkKeyTimesByKey = array();
|
|
|
|
|
foreach ( $checksByKey as $cacheKey => $checks ) {
|
2015-11-24 05:41:12 +00:00
|
|
|
$checkKeyTimesByKey[$cacheKey] =
|
|
|
|
|
$this->processCheckKeys( $checks, $wrappedValues, $now );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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;
|
2015-11-16 21:41:10 +00:00
|
|
|
|
|
|
|
|
// Force dependant keys to be invalid for a while after purging
|
|
|
|
|
// to reduce race conditions involving stale data getting cached
|
|
|
|
|
$checkKeyTimes = $checkKeyTimesForAll;
|
|
|
|
|
if ( isset( $checkKeyTimesByKey[$key] ) ) {
|
|
|
|
|
$checkKeyTimes = array_merge( $checkKeyTimes, $checkKeyTimesByKey[$key] );
|
|
|
|
|
}
|
2015-01-27 19:56:44 +00:00
|
|
|
foreach ( $checkKeyTimes as $checkKeyTime ) {
|
|
|
|
|
$safeTimestamp = $checkKeyTime + self::HOLDOFF_TTL;
|
|
|
|
|
if ( $safeTimestamp >= $wrappedValues[$vKey][self::FLD_TIME] ) {
|
|
|
|
|
$curTTL = min( $curTTL, $checkKeyTime - $now );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$curTTLs[$key] = $curTTL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
|
2015-11-16 21:41:10 +00:00
|
|
|
/**
|
|
|
|
|
* @since 1.27
|
|
|
|
|
* @param array $timeKeys List of prefixed time check keys
|
|
|
|
|
* @param array $wrappedValues
|
|
|
|
|
* @param float $now
|
|
|
|
|
* @return array List of timestamps
|
|
|
|
|
*/
|
|
|
|
|
private function processCheckKeys( array $timeKeys, array $wrappedValues, $now ) {
|
|
|
|
|
$times = array();
|
|
|
|
|
foreach ( $timeKeys as $timeKey ) {
|
|
|
|
|
$timestamp = isset( $wrappedValues[$timeKey] )
|
|
|
|
|
? self::parsePurgeValue( $wrappedValues[$timeKey] )
|
|
|
|
|
: false;
|
|
|
|
|
if ( !is_float( $timestamp ) ) {
|
|
|
|
|
// Key is not set or invalid; regenerate
|
|
|
|
|
$this->cache->add( $timeKey, self::PURGE_VAL_PREFIX . $now, self::CHECK_KEY_TTL );
|
|
|
|
|
$timestamp = $now;
|
|
|
|
|
}
|
|
|
|
|
$times[] = $timestamp;
|
|
|
|
|
}
|
|
|
|
|
return $times;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
2015-10-10 07:18:00 +00:00
|
|
|
* Set the value of a key in cache
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2015-09-24 21:47:30 +00:00
|
|
|
* If the data was read from a snapshot-isolated transactions (e.g. the default
|
|
|
|
|
* REPEATABLE-READ in innoDB), use 'since' to avoid the following race condition:
|
|
|
|
|
* - a) T1 starts
|
|
|
|
|
* - b) T2 updates a row, calls delete(), and commits
|
|
|
|
|
* - c) The HOLDOFF_TTL passes, expiring the delete() tombstone
|
|
|
|
|
* - d) T1 reads the row and calls set() due to a cache miss
|
|
|
|
|
* - e) Stale value is stuck in cache
|
|
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Setting 'lag' and 'since' help avoids keys getting stuck in stale states.
|
2015-10-06 05:06:46 +00:00
|
|
|
*
|
2015-09-24 21:47:30 +00:00
|
|
|
* Example usage:
|
|
|
|
|
* @code
|
|
|
|
|
* $dbr = wfGetDB( DB_SLAVE );
|
2015-10-06 05:06:46 +00:00
|
|
|
* $setOpts = Database::getCacheSetOptions( $dbr );
|
2015-09-24 21:47:30 +00:00
|
|
|
* // Fetch the row from the DB
|
|
|
|
|
* $row = $dbr->selectRow( ... );
|
2015-10-17 22:12:20 +00:00
|
|
|
* $key = $cache->makeKey( 'building', $buildingId );
|
2015-10-19 17:52:19 +00:00
|
|
|
* $cache->set( $key, $row, $cache::TTL_DAY, $setOpts );
|
2015-09-24 21:47:30 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
2015-01-27 19:56:44 +00:00
|
|
|
* @param string $key Cache key
|
|
|
|
|
* @param mixed $value
|
2015-10-15 15:09:48 +00:00
|
|
|
* @param integer $ttl Seconds to live. Special values are:
|
|
|
|
|
* - WANObjectCache::TTL_INDEFINITE: Cache forever
|
2015-09-24 21:47:30 +00:00
|
|
|
* @param array $opts Options map:
|
2015-10-01 02:40:09 +00:00
|
|
|
* - lag : Seconds of slave lag. Typically, this is either the slave lag
|
|
|
|
|
* before the data was read or, if applicable, the slave lag before
|
|
|
|
|
* the snapshot-isolated transaction the data was read from started.
|
2015-10-10 07:18:00 +00:00
|
|
|
* Default: 0 seconds
|
2015-09-24 21:47:30 +00:00
|
|
|
* - since : UNIX timestamp of the data in $value. Typically, this is either
|
|
|
|
|
* the current time the data was read or (if applicable) the time when
|
|
|
|
|
* the snapshot-isolated transaction the data was read from started.
|
2015-10-10 07:18:00 +00:00
|
|
|
* Default: 0 seconds
|
2015-10-21 05:55:51 +00:00
|
|
|
* - pending : Whether this data is possibly from an uncommitted write transaction.
|
|
|
|
|
* Generally, other threads should not see values from the future and
|
|
|
|
|
* they certainly should not see ones that ended up getting rolled back.
|
|
|
|
|
* Default: false
|
2015-10-23 02:36:59 +00:00
|
|
|
* - lockTSE : if excessive replication/snapshot lag is detected, then store the value
|
|
|
|
|
* with this TTL and flag it as stale. This is only useful if the reads for
|
|
|
|
|
* this key use getWithSetCallback() with "lockTSE" set.
|
2015-10-10 07:18:00 +00:00
|
|
|
* Default: WANObjectCache::TSE_NONE
|
2015-01-27 19:56:44 +00:00
|
|
|
* @return bool Success
|
|
|
|
|
*/
|
2015-09-24 21:47:30 +00:00
|
|
|
final public function set( $key, $value, $ttl = 0, array $opts = array() ) {
|
|
|
|
|
$lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
|
|
|
|
|
$age = isset( $opts['since'] ) ? max( 0, microtime( true ) - $opts['since'] ) : 0;
|
2015-10-01 02:40:09 +00:00
|
|
|
$lag = isset( $opts['lag'] ) ? $opts['lag'] : 0;
|
2015-10-23 02:36:59 +00:00
|
|
|
|
|
|
|
|
// Do not cache potentially uncommitted data as it might get rolled back
|
2015-10-21 05:55:51 +00:00
|
|
|
if ( !empty( $opts['pending'] ) ) {
|
|
|
|
|
$this->logger->info( "Rejected set() for $key due to pending writes." );
|
|
|
|
|
|
|
|
|
|
return true; // no-op the write for being unsafe
|
|
|
|
|
}
|
2015-10-23 02:36:59 +00:00
|
|
|
|
|
|
|
|
$wrapExtra = array(); // additional wrapped value fields
|
2015-10-25 20:42:21 +00:00
|
|
|
// Check if there's a risk of writing stale data after the purge tombstone expired
|
|
|
|
|
if ( ( $lag + $age ) > self::MAX_READ_LAG ) {
|
2015-10-23 02:36:59 +00:00
|
|
|
// Case A: read lag with "lockTSE"; save but record value as stale
|
2015-09-24 21:47:30 +00:00
|
|
|
if ( $lockTSE >= 0 ) {
|
2015-10-23 02:36:59 +00:00
|
|
|
$ttl = max( 1, (int)$lockTSE ); // set() expects seconds
|
|
|
|
|
$wrapExtra[self::FLD_FLAGS] = self::FLG_STALE; // mark as stale
|
|
|
|
|
// Case B: any long-running transaction; ignore this set()
|
|
|
|
|
} elseif ( $age > self::MAX_READ_LAG ) {
|
2015-10-25 20:42:21 +00:00
|
|
|
$this->logger->warning( "Rejected set() for $key due to snapshot lag." );
|
|
|
|
|
|
|
|
|
|
return true; // no-op the write for being unsafe
|
2015-10-23 02:36:59 +00:00
|
|
|
// Case C: high replication lag; lower TTL instead of ignoring all set()s
|
2015-10-25 20:42:21 +00:00
|
|
|
} elseif ( $lag > self::MAX_READ_LAG ) {
|
|
|
|
|
$ttl = $ttl ? min( $ttl, self::TTL_LAGGED ) : self::TTL_LAGGED;
|
|
|
|
|
$this->logger->warning( "Lowered set() TTL for $key due to replication lag." );
|
2015-10-23 02:36:59 +00:00
|
|
|
// Case D: medium length request with medium replication lag; ignore this set()
|
2015-10-25 20:42:21 +00:00
|
|
|
} else {
|
|
|
|
|
$this->logger->warning( "Rejected set() for $key due to high read lag." );
|
|
|
|
|
|
|
|
|
|
return true; // no-op the write for being unsafe
|
|
|
|
|
}
|
2015-09-24 21:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
2015-10-23 02:36:59 +00:00
|
|
|
// Wrap that value with time/TTL/version metadata
|
|
|
|
|
$wrapped = $this->wrap( $value, $ttl ) + $wrapExtra;
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
$func = function ( $cache, $key, $cWrapped ) use ( $wrapped ) {
|
|
|
|
|
return ( is_string( $cWrapped ) )
|
|
|
|
|
? false // key is tombstoned; do nothing
|
|
|
|
|
: $wrapped;
|
|
|
|
|
};
|
|
|
|
|
|
2015-09-24 21:47:30 +00:00
|
|
|
return $this->cache->merge( self::VALUE_KEY_PREFIX . $key, $func, $ttl, 1 );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 01:22:52 +00:00
|
|
|
* Purge a key from all datacenters
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
2015-09-17 18:00:23 +00:00
|
|
|
* 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:
|
2015-09-22 20:37:19 +00:00
|
|
|
* - 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
|
2015-08-31 20:26:22 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
2015-10-10 07:18:00 +00:00
|
|
|
* For this to always avoid stale value writes, the following must hold:
|
2015-09-22 20:37:19 +00:00
|
|
|
* - 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
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Note that set() can also be lag-aware and lower the TTL if it's high.
|
|
|
|
|
*
|
2015-09-24 22:25:44 +00:00
|
|
|
* When using potentially long-running ACID transactions, a good pattern is
|
|
|
|
|
* to use a pre-commit hook to issue the delete. This means that immediately
|
|
|
|
|
* after commit, callers will see the tombstone in cache in the local datacenter
|
|
|
|
|
* and in the others upon relay. It also avoids the following race condition:
|
|
|
|
|
* - a) T1 begins, changes a row, and calls delete()
|
|
|
|
|
* - b) The HOLDOFF_TTL passes, expiring the delete() tombstone
|
|
|
|
|
* - c) T2 starts, reads the row and calls set() due to a cache miss
|
|
|
|
|
* - d) T1 finally commits
|
|
|
|
|
* - e) Stale value is stuck in cache
|
|
|
|
|
*
|
|
|
|
|
* Example usage:
|
|
|
|
|
* @code
|
|
|
|
|
* $dbw->begin(); // start of request
|
|
|
|
|
* ... <execute some stuff> ...
|
|
|
|
|
* // Update the row in the DB
|
|
|
|
|
* $dbw->update( ... );
|
2015-10-17 22:12:20 +00:00
|
|
|
* $key = $cache->makeKey( 'homes', $homeId );
|
2015-09-24 22:25:44 +00:00
|
|
|
* // Purge the corresponding cache entry just before committing
|
|
|
|
|
* $dbw->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) {
|
|
|
|
|
* $cache->delete( $key );
|
|
|
|
|
* } );
|
|
|
|
|
* ... <execute some stuff> ...
|
|
|
|
|
* $dbw->commit(); // end of request
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
2015-10-28 04:52:30 +00:00
|
|
|
* The $ttl parameter can be used when purging values that have not actually changed
|
|
|
|
|
* recently. For example, a cleanup script to purge cache entries does not really need
|
2015-11-24 05:41:12 +00:00
|
|
|
* a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge.
|
2015-10-28 04:52:30 +00:00
|
|
|
* Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback().
|
|
|
|
|
*
|
|
|
|
|
* 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.
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* @param string $key Cache key
|
2015-10-28 04:52:30 +00:00
|
|
|
* @param integer $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL
|
2015-01-27 19:56:44 +00:00
|
|
|
* @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;
|
2015-11-24 05:41:12 +00:00
|
|
|
|
|
|
|
|
if ( $ttl <= 0 ) {
|
|
|
|
|
// Update the local datacenter immediately
|
|
|
|
|
$ok = $this->cache->delete( $key );
|
|
|
|
|
// Publish the purge to all datacenters
|
|
|
|
|
$ok = $this->relayDelete( $key ) && $ok;
|
|
|
|
|
} else {
|
|
|
|
|
// Update the local datacenter immediately
|
|
|
|
|
$ok = $this->cache->set( $key, self::PURGE_VAL_PREFIX . microtime( true ), $ttl );
|
|
|
|
|
// Publish the purge to all datacenters
|
|
|
|
|
$ok = $this->relayPurge( $key, $ttl ) && $ok;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ok;
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fetch the value of a timestamp "check" key
|
|
|
|
|
*
|
2015-05-22 05:46:25 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Note that "check" keys won't collide with other regular keys.
|
2015-05-12 02:15:35 +00:00
|
|
|
*
|
2015-01-27 19:56:44 +00:00
|
|
|
* @param string $key
|
2015-05-22 05:46:25 +00:00
|
|
|
* @return float UNIX timestamp of the key
|
2015-01-27 19:56:44 +00:00
|
|
|
*/
|
|
|
|
|
final public function getCheckKeyTime( $key ) {
|
2015-05-22 05:46:25 +00:00
|
|
|
$key = self::TIME_KEY_PREFIX . $key;
|
|
|
|
|
|
|
|
|
|
$time = self::parsePurgeValue( $this->cache->get( $key ) );
|
|
|
|
|
if ( $time === false ) {
|
2015-05-28 17:04:19 +00:00
|
|
|
// Casting assures identical floats for the next getCheckKeyTime() calls
|
|
|
|
|
$time = (string)microtime( true );
|
2015-05-22 05:46:25 +00:00
|
|
|
$this->cache->add( $key, self::PURGE_VAL_PREFIX . $time, self::CHECK_KEY_TTL );
|
2015-05-28 17:04:19 +00:00
|
|
|
$time = (float)$time;
|
2015-05-22 05:46:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $time;
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-09-29 01:22:52 +00:00
|
|
|
* Purge a "check" key from all datacenters, invalidating keys that use it
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* 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.
|
2015-10-10 07:18:00 +00:00
|
|
|
* When touched, keys using it via get(), getMulti(), or getWithSetCallback()
|
|
|
|
|
* will be invalidated. It is treated as being HOLDOFF_TTL seconds in the future
|
|
|
|
|
* by those methods to avoid race conditions where dependent keys get updated
|
|
|
|
|
* with stale values (e.g. from a DB slave).
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* This is typically useful for keys with hardcoded names or in some cases
|
2015-05-28 17:04:19 +00:00
|
|
|
* dynamically generated names where a low number of combinations exist.
|
|
|
|
|
* When a few important keys get a large number of hits, a high cache
|
2015-10-10 07:18:00 +00:00
|
|
|
* time is usually desired as well as "lockTSE" logic. The resetCheckKey()
|
2015-05-28 17:04:19 +00:00
|
|
|
* method is less appropriate in such cases since the "time since expiry"
|
|
|
|
|
* cannot be inferred.
|
|
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Note that "check" keys won't collide with other regular keys.
|
2015-05-12 02:15:35 +00:00
|
|
|
*
|
2015-01-27 19:56:44 +00:00
|
|
|
* @see WANObjectCache::get()
|
2015-10-10 07:18:00 +00:00
|
|
|
* @see WANObjectCache::getWithSetCallback()
|
|
|
|
|
* @see WANObjectCache::resetCheckKey()
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* @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;
|
2015-09-29 01:22:52 +00:00
|
|
|
// Update the local datacenter immediately
|
2015-01-27 19:56:44 +00:00
|
|
|
$ok = $this->cache->set( $key,
|
|
|
|
|
self::PURGE_VAL_PREFIX . microtime( true ), self::CHECK_KEY_TTL );
|
2015-09-29 01:22:52 +00:00
|
|
|
// Publish the purge to all datacenters
|
2015-01-27 19:56:44 +00:00
|
|
|
return $this->relayPurge( $key, self::CHECK_KEY_TTL ) && $ok;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 17:04:19 +00:00
|
|
|
/**
|
2015-09-29 01:22:52 +00:00
|
|
|
* Delete a "check" key from all datacenters, invalidating keys that use it
|
2015-05-28 17:04:19 +00:00
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* This is similar to touchCheckKey() in that keys using it via get(), getMulti(),
|
|
|
|
|
* or getWithSetCallback() will be invalidated. The differences are:
|
2015-09-22 20:37:19 +00:00
|
|
|
* - a) The timestamp will be deleted from all caches and lazily
|
2015-09-24 20:47:28 +00:00
|
|
|
* re-initialized when accessed (rather than set everywhere)
|
2015-09-22 20:37:19 +00:00
|
|
|
* - b) Thus, dependent keys will be known to be invalid, but not
|
2015-09-24 20:47:28 +00:00
|
|
|
* for how long (they are treated as "just" purged), which
|
|
|
|
|
* effects any lockTSE logic in getWithSetCallback()
|
2015-10-01 08:50:09 +00:00
|
|
|
*
|
2015-05-28 17:04:19 +00:00
|
|
|
* 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.
|
|
|
|
|
*
|
2015-10-10 07:18:00 +00:00
|
|
|
* Note that "check" keys won't collide with other regular keys.
|
2015-05-28 17:04:19 +00:00
|
|
|
*
|
|
|
|
|
* @see WANObjectCache::get()
|
2015-10-10 07:18:00 +00:00
|
|
|
* @see WANObjectCache::getWithSetCallback()
|
|
|
|
|
* @see WANObjectCache::touchCheckKey()
|
2015-05-28 17:04:19 +00:00
|
|
|
*
|
|
|
|
|
* @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;
|
2015-09-29 01:22:52 +00:00
|
|
|
// Update the local datacenter immediately
|
2015-05-28 17:04:19 +00:00
|
|
|
$ok = $this->cache->delete( $key );
|
2015-09-29 01:22:52 +00:00
|
|
|
// Publish the purge to all datacenters
|
2015-05-28 17:04:19 +00:00
|
|
|
return $this->relayDelete( $key ) && $ok;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* Method to fetch/regenerate cache keys
|
|
|
|
|
*
|
2015-09-24 21:47:30 +00:00
|
|
|
* On cache miss, the key will be set to the callback result via set()
|
2015-10-10 07:18:00 +00:00
|
|
|
* (unless the callback returns false) and that result will be returned.
|
|
|
|
|
* The arguments supplied to the callback are:
|
|
|
|
|
* - $oldValue : current cache value or false if not present
|
|
|
|
|
* - &$ttl : a reference to the TTL which can be altered
|
|
|
|
|
* - &$setOpts : a reference to options for set() which can be altered
|
|
|
|
|
*
|
|
|
|
|
* It is strongly recommended to set the 'lag' and 'since' fields to avoid race conditions
|
|
|
|
|
* that can cause stale values to get stuck at keys. 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. Note that
|
|
|
|
|
* preemptive regeneration and $checkKeys can result in a non-false current value.
|
|
|
|
|
*
|
|
|
|
|
* Usage of $checkKeys is similar to get() and getMulti(). However, rather than the caller
|
|
|
|
|
* having to inspect a "current time left" variable (e.g. $curTTL, $curTTLs), a cache
|
|
|
|
|
* regeneration will automatically be triggered using the callback.
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* The simplest way to avoid stampedes for hot keys is to use
|
|
|
|
|
* the 'lockTSE' option in $opts. If cache purges are needed, also:
|
2015-09-22 19:54:24 +00:00
|
|
|
* - a) Pass $key into $checkKeys
|
|
|
|
|
* - b) Use touchCheckKey( $key ) instead of delete( $key )
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
2015-09-30 00:25:38 +00:00
|
|
|
* Example usage (typical key):
|
2015-04-29 04:49:40 +00:00
|
|
|
* @code
|
2015-09-30 00:25:38 +00:00
|
|
|
* $catInfo = $cache->getWithSetCallback(
|
|
|
|
|
* // Key to store the cached value under
|
2015-10-17 22:12:20 +00:00
|
|
|
* $cache->makeKey( 'cat-attributes', $catId ),
|
2015-10-19 17:52:19 +00:00
|
|
|
* // Time-to-live (in seconds)
|
|
|
|
|
* $cache::TTL_MINUTE,
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Function that derives the new key value
|
2015-10-01 02:40:09 +00:00
|
|
|
* function ( $oldValue, &$ttl, array &$setOpts ) {
|
2015-09-30 00:25:38 +00:00
|
|
|
* $dbr = wfGetDB( DB_SLAVE );
|
2015-10-01 02:40:09 +00:00
|
|
|
* // Account for any snapshot/slave lag
|
2015-10-06 05:06:46 +00:00
|
|
|
* $setOpts += Database::getCacheSetOptions( $dbr );
|
2015-09-30 00:25:38 +00:00
|
|
|
*
|
2015-10-01 02:40:09 +00:00
|
|
|
* return $dbr->selectRow( ... );
|
2015-10-08 04:52:43 +00:00
|
|
|
* }
|
2015-09-30 00:25:38 +00:00
|
|
|
* );
|
2015-04-29 04:49:40 +00:00
|
|
|
* @endcode
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
2015-09-30 00:25:38 +00:00
|
|
|
* Example usage (key that is expensive and hot):
|
|
|
|
|
* @code
|
|
|
|
|
* $catConfig = $cache->getWithSetCallback(
|
|
|
|
|
* // Key to store the cached value under
|
2015-10-17 22:12:20 +00:00
|
|
|
* $cache->makeKey( 'site-cat-config' ),
|
2015-10-19 17:52:19 +00:00
|
|
|
* // Time-to-live (in seconds)
|
|
|
|
|
* $cache::TTL_DAY,
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Function that derives the new key value
|
2015-10-01 02:40:09 +00:00
|
|
|
* function ( $oldValue, &$ttl, array &$setOpts ) {
|
2015-09-30 00:25:38 +00:00
|
|
|
* $dbr = wfGetDB( DB_SLAVE );
|
2015-10-01 02:40:09 +00:00
|
|
|
* // Account for any snapshot/slave lag
|
2015-10-06 05:06:46 +00:00
|
|
|
* $setOpts += Database::getCacheSetOptions( $dbr );
|
2015-09-30 00:25:38 +00:00
|
|
|
*
|
2015-10-01 02:40:09 +00:00
|
|
|
* return CatConfig::newFromRow( $dbr->selectRow( ... ) );
|
2015-10-08 04:52:43 +00:00
|
|
|
* },
|
|
|
|
|
* array(
|
2015-10-08 22:49:57 +00:00
|
|
|
* // Calling touchCheckKey() on this key invalidates the cache
|
2015-10-17 22:12:20 +00:00
|
|
|
* 'checkKeys' => array( $cache->makeKey( 'site-cat-config' ) ),
|
2015-10-08 04:52:43 +00:00
|
|
|
* // Try to only let one datacenter thread manage cache updates at a time
|
|
|
|
|
* 'lockTSE' => 30
|
|
|
|
|
* )
|
2015-09-30 00:25:38 +00:00
|
|
|
* );
|
|
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* Example usage (key with dynamic dependencies):
|
2015-04-29 04:49:40 +00:00
|
|
|
* @code
|
2015-09-30 00:25:38 +00:00
|
|
|
* $catState = $cache->getWithSetCallback(
|
|
|
|
|
* // Key to store the cached value under
|
2015-10-17 22:12:20 +00:00
|
|
|
* $cache->makeKey( 'cat-state', $cat->getId() ),
|
2015-10-08 04:52:43 +00:00
|
|
|
* // Time-to-live (seconds)
|
2015-10-28 04:52:30 +00:00
|
|
|
* $cache::TTL_HOUR,
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Function that derives the new key value
|
2015-10-01 02:40:09 +00:00
|
|
|
* function ( $oldValue, &$ttl, array &$setOpts ) {
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Determine new value from the DB
|
|
|
|
|
* $dbr = wfGetDB( DB_SLAVE );
|
2015-10-01 02:40:09 +00:00
|
|
|
* // Account for any snapshot/slave lag
|
2015-10-06 05:06:46 +00:00
|
|
|
* $setOpts += Database::getCacheSetOptions( $dbr );
|
2015-09-30 00:25:38 +00:00
|
|
|
*
|
2015-10-01 02:40:09 +00:00
|
|
|
* return CatState::newFromResults( $dbr->select( ... ) );
|
2015-10-08 04:52:43 +00:00
|
|
|
* },
|
|
|
|
|
* array(
|
2015-10-08 22:49:57 +00:00
|
|
|
* // The "check" keys that represent things the value depends on;
|
|
|
|
|
* // Calling touchCheckKey() on any of them invalidates the cache
|
2015-10-08 04:52:43 +00:00
|
|
|
* 'checkKeys' => array(
|
2015-10-17 22:12:20 +00:00
|
|
|
* $cache->makeKey( 'sustenance-bowls', $cat->getRoomId() ),
|
|
|
|
|
* $cache->makeKey( 'people-present', $cat->getHouseId() ),
|
|
|
|
|
* $cache->makeKey( 'cat-laws', $cat->getCityId() ),
|
2015-10-08 04:52:43 +00:00
|
|
|
* )
|
2015-09-30 00:25:38 +00:00
|
|
|
* )
|
2015-01-27 19:56:44 +00:00
|
|
|
* );
|
2015-09-30 00:25:38 +00:00
|
|
|
* @endcode
|
|
|
|
|
*
|
|
|
|
|
* Example usage (hot key holding most recent 100 events):
|
|
|
|
|
* @code
|
|
|
|
|
* $lastCatActions = $cache->getWithSetCallback(
|
|
|
|
|
* // Key to store the cached value under
|
2015-10-17 22:12:20 +00:00
|
|
|
* $cache->makeKey( 'cat-last-actions', 100 ),
|
2015-10-19 17:52:19 +00:00
|
|
|
* // Time-to-live (in seconds)
|
2015-10-08 04:52:43 +00:00
|
|
|
* 10,
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Function that derives the new key value
|
2015-10-01 02:40:09 +00:00
|
|
|
* function ( $oldValue, &$ttl, array &$setOpts ) {
|
2015-09-30 00:25:38 +00:00
|
|
|
* $dbr = wfGetDB( DB_SLAVE );
|
2015-10-01 02:40:09 +00:00
|
|
|
* // Account for any snapshot/slave lag
|
2015-10-06 05:06:46 +00:00
|
|
|
* $setOpts += Database::getCacheSetOptions( $dbr );
|
2015-10-01 02:40:09 +00:00
|
|
|
*
|
2015-09-30 00:25:38 +00:00
|
|
|
* // Start off with the last cached list
|
2015-09-30 04:28:30 +00:00
|
|
|
* $list = $oldValue ?: array();
|
|
|
|
|
* // Fetch the last 100 relevant rows in descending order;
|
|
|
|
|
* // only fetch rows newer than $list[0] to reduce scanning
|
2015-09-30 00:25:38 +00:00
|
|
|
* $rows = iterator_to_array( $dbr->select( ... ) );
|
|
|
|
|
* // Merge them and get the new "last 100" rows
|
2015-10-01 02:40:09 +00:00
|
|
|
* return array_slice( array_merge( $new, $list ), 0, 100 );
|
2015-09-30 00:25:38 +00:00
|
|
|
* },
|
|
|
|
|
* // Try to only let one datacenter thread manage cache updates at a time
|
|
|
|
|
* array( 'lockTSE' => 30 )
|
2015-09-30 04:28:30 +00:00
|
|
|
* );
|
2015-04-29 04:49:40 +00:00
|
|
|
* @endcode
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* @see WANObjectCache::get()
|
2015-09-24 21:47:30 +00:00
|
|
|
* @see WANObjectCache::set()
|
2015-01-27 19:56:44 +00:00
|
|
|
*
|
|
|
|
|
* @param string $key Cache key
|
2015-05-14 03:28:16 +00:00
|
|
|
* @param integer $ttl Seconds to live for key updates. Special values are:
|
2015-10-13 15:15:36 +00:00
|
|
|
* - WANObjectCache::TTL_INDEFINITE: Cache forever
|
2015-10-07 23:21:11 +00:00
|
|
|
* - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
|
|
|
|
|
* @param callable $callback Value generation function
|
2015-01-27 19:56:44 +00:00
|
|
|
* @param array $opts Options map:
|
2015-10-10 07:18:00 +00:00
|
|
|
* - checkKeys: List of "check" keys. The key at $key will be seen as invalid when either
|
|
|
|
|
* touchCheckKey() or resetCheckKey() is called on any of these keys.
|
2015-10-07 23:21:11 +00:00
|
|
|
* - 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.
|
2015-10-07 08:55:39 +00:00
|
|
|
* Use WANObjectCache::TSE_NONE to disable this logic.
|
|
|
|
|
* Default: WANObjectCache::TSE_NONE.
|
|
|
|
|
* - pcTTL : process cache the value in this PHP instance with this TTL. This avoids
|
|
|
|
|
* network I/O when a key is read several times. This will not cache if the callback
|
|
|
|
|
* returns false however. Note that any purges will not be seen while process cached;
|
|
|
|
|
* since the callback should use slave DBs and they may be lagged or have snapshot
|
2015-10-10 07:18:00 +00:00
|
|
|
* isolation anyway, this should not typically matter.
|
2015-10-07 08:55:39 +00:00
|
|
|
* Default: WANObjectCache::TTL_UNCACHEABLE.
|
2015-01-27 19:56:44 +00:00
|
|
|
* @return mixed Value to use for the key
|
|
|
|
|
*/
|
2015-10-17 22:45:08 +00:00
|
|
|
final public function getWithSetCallback( $key, $ttl, $callback, array $opts = array() ) {
|
2015-10-07 08:55:39 +00:00
|
|
|
$pcTTL = isset( $opts['pcTTL'] ) ? $opts['pcTTL'] : self::TTL_UNCACHEABLE;
|
|
|
|
|
|
|
|
|
|
// Try the process cache if enabled
|
|
|
|
|
$value = ( $pcTTL >= 0 ) ? $this->procCache->get( $key ) : false;
|
|
|
|
|
|
|
|
|
|
if ( $value === false ) {
|
|
|
|
|
// Fetch the value over the network
|
2015-10-17 22:45:08 +00:00
|
|
|
$value = $this->doGetWithSetCallback( $key, $ttl, $callback, $opts );
|
2015-10-07 08:55:39 +00:00
|
|
|
// Update the process cache if enabled
|
|
|
|
|
if ( $pcTTL >= 0 && $value !== false ) {
|
|
|
|
|
$this->procCache->set( $key, $value, $pcTTL );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-10-10 07:18:00 +00:00
|
|
|
* Do the actual I/O for getWithSetCallback() when needed
|
|
|
|
|
*
|
2015-10-07 08:55:39 +00:00
|
|
|
* @see WANObjectCache::getWithSetCallback()
|
|
|
|
|
*
|
|
|
|
|
* @param string $key
|
|
|
|
|
* @param integer $ttl
|
|
|
|
|
* @param callback $callback
|
|
|
|
|
* @param array $opts
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2015-10-17 22:45:08 +00:00
|
|
|
protected function doGetWithSetCallback( $key, $ttl, $callback, array $opts ) {
|
2015-09-16 17:05:54 +00:00
|
|
|
$lowTTL = isset( $opts['lowTTL'] ) ? $opts['lowTTL'] : min( self::LOW_TTL, $ttl );
|
2015-09-18 19:22:18 +00:00
|
|
|
$lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
|
2015-10-17 22:45:08 +00:00
|
|
|
$checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : array();
|
2015-01-27 19:56:44 +00:00
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 18:00:23 +00:00
|
|
|
// A deleted key with a negative TTL left must be tombstoned
|
2015-05-15 05:52:35 +00:00
|
|
|
$isTombstone = ( $curTTL !== null && $value === false );
|
2015-01-27 19:56:44 +00:00
|
|
|
// Assume a key is hot if requested soon after invalidation
|
|
|
|
|
$isHot = ( $curTTL !== null && $curTTL <= 0 && abs( $curTTL ) <= $lockTSE );
|
2015-09-18 19:22:18 +00:00
|
|
|
// Decide whether a single thread should handle regenerations.
|
|
|
|
|
// This avoids stampedes when $checkKeys are bumped and when preemptive
|
|
|
|
|
// renegerations take too long. It also reduces regenerations while $key
|
|
|
|
|
// is tombstoned. This balances cache freshness with avoiding DB load.
|
|
|
|
|
$useMutex = ( $isHot || ( $isTombstone && $lockTSE > 0 ) );
|
2015-01-27 19:56:44 +00:00
|
|
|
|
2015-09-17 18:00:23 +00:00
|
|
|
$lockAcquired = false;
|
2015-09-18 19:22:18 +00:00
|
|
|
if ( $useMutex ) {
|
2015-09-29 01:22:52 +00:00
|
|
|
// Acquire a datacenter-local non-blocking lock
|
2015-01-27 19:56:44 +00:00
|
|
|
if ( $this->cache->lock( $key, 0, self::LOCK_TTL ) ) {
|
|
|
|
|
// Lock acquired; this thread should update the key
|
2015-09-17 18:00:23 +00:00
|
|
|
$lockAcquired = true;
|
2015-01-27 19:56:44 +00:00
|
|
|
} elseif ( $value !== false ) {
|
|
|
|
|
// If it cannot be acquired; then the stale value can be used
|
|
|
|
|
return $value;
|
2015-09-18 19:22:18 +00:00
|
|
|
} else {
|
|
|
|
|
// 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;
|
|
|
|
|
}
|
2015-05-15 05:52:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !is_callable( $callback ) ) {
|
|
|
|
|
throw new InvalidArgumentException( "Invalid cache miss callback provided." );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
// Generate the new value from the callback...
|
2015-09-24 21:47:30 +00:00
|
|
|
$setOpts = array();
|
|
|
|
|
$value = call_user_func_array( $callback, array( $cValue, &$ttl, &$setOpts ) );
|
2015-01-27 19:56:44 +00:00
|
|
|
// When delete() is called, writes are write-holed by the tombstone,
|
|
|
|
|
// so use a special stash key to pass the new value around threads.
|
2015-09-18 19:22:18 +00:00
|
|
|
if ( $useMutex && $value !== false && $ttl >= 0 ) {
|
|
|
|
|
$tempTTL = max( 1, (int)$lockTSE ); // set() expects seconds
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->cache->set( self::STASH_KEY_PREFIX . $key, $value, $tempTTL );
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-17 18:00:23 +00:00
|
|
|
if ( $lockAcquired ) {
|
2015-01-27 19:56:44 +00:00
|
|
|
$this->cache->unlock( $key );
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-14 03:28:16 +00:00
|
|
|
if ( $value !== false && $ttl >= 0 ) {
|
2015-01-27 19:56:44 +00:00
|
|
|
// Update the cache; this will fail if the key is tombstoned
|
2015-09-24 21:47:30 +00:00
|
|
|
$setOpts['lockTSE'] = $lockTSE;
|
|
|
|
|
$this->set( $key, $value, $ttl, $setOpts );
|
2015-01-27 19:56:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-17 22:12:20 +00:00
|
|
|
/**
|
|
|
|
|
* @see BagOStuff::makeKey()
|
|
|
|
|
* @param string ... Key component
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public function makeKey() {
|
|
|
|
|
return call_user_func_array( array( $this->cache, __FUNCTION__ ), func_get_args() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @see BagOStuff::makeGlobalKey()
|
|
|
|
|
* @param string ... Key component
|
|
|
|
|
* @return string
|
|
|
|
|
* @since 1.27
|
|
|
|
|
*/
|
|
|
|
|
public function makeGlobalKey() {
|
|
|
|
|
return call_user_func_array( array( $this->cache, __FUNCTION__ ), func_get_args() );
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-28 17:04:19 +00:00
|
|
|
/**
|
|
|
|
|
* 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;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-27 19:56:44 +00:00
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2015-08-24 11:15:28 +00:00
|
|
|
* @param float $curTTL Approximate TTL left on the key if present
|
2015-01-27 19:56:44 +00:00
|
|
|
* @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]
|
2015-10-23 02:36:59 +00:00
|
|
|
* @return array
|
2015-01-27 19:56:44 +00:00
|
|
|
*/
|
|
|
|
|
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
|
2015-10-23 02:36:59 +00:00
|
|
|
$curTTL = min( $purgeTimestamp - $now, self::TINY_NEGATIVE );
|
2015-01-27 19:56:44 +00:00
|
|
|
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 );
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 02:36:59 +00:00
|
|
|
$flags = isset( $wrapped[self::FLD_FLAGS] ) ? $wrapped[self::FLD_FLAGS] : 0;
|
|
|
|
|
if ( ( $flags & self::FLG_STALE ) == self::FLG_STALE ) {
|
|
|
|
|
// Treat as expired, with the cache time as the expiration
|
|
|
|
|
$age = $now - $wrapped[self::FLD_TIME];
|
|
|
|
|
$curTTL = min( -$age, self::TINY_NEGATIVE );
|
|
|
|
|
} elseif ( $wrapped[self::FLD_TTL] > 0 ) {
|
2015-01-27 19:56:44 +00:00
|
|
|
// 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
|
2015-04-27 20:03:50 +00:00
|
|
|
* @return string[]
|
2015-01-27 19:56:44 +00:00
|
|
|
*/
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|