3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
20 * @author Aaron Schulz
23 use Psr\Log\LoggerAwareInterface
;
24 use Psr\Log\LoggerInterface
;
25 use Psr\Log\NullLogger
;
28 * Multi-datacenter aware caching interface
30 * All operations go to the local datacenter cache, except for delete(),
31 * touchCheckKey(), and resetCheckKey(), which broadcast to all datacenters.
33 * This class is intended for caching data from primary stores.
34 * If the get() method does not return a value, then the caller
35 * should query the new value and backfill the cache using set().
36 * The preferred way to do this logic is through getWithSetCallback().
37 * When querying the store on cache miss, the closest DB replica
38 * should be used. Try to avoid heavyweight DB master or quorum reads.
39 * When the source data changes, a purge method should be called.
40 * Since purges are expensive, they should be avoided. One can do so if:
41 * - a) The object cached is immutable; or
42 * - b) Validity is checked against the source after get(); or
43 * - c) Using a modest TTL is reasonably correct and performant
45 * The simplest purge method is delete().
47 * There are two supported ways to handle broadcasted operations:
48 * - a) Configure the 'purge' EventRelayer to point to a valid PubSub endpoint
49 * that has subscribed listeners on the cache servers applying the cache updates.
50 * - b) Ignore the 'purge' EventRelayer configuration (default is NullEventRelayer)
51 * and set up mcrouter as the underlying cache backend, using one of the memcached
52 * BagOStuff classes as 'cache'. Use OperationSelectorRoute in the mcrouter settings
53 * to configure 'set' and 'delete' operations to go to all DCs via AllAsyncRoute and
54 * configure other operations to go to the local DC via PoolRoute (for reference,
55 * see https://github.com/facebook/mcrouter/wiki/List-of-Route-Handles).
57 * Broadcasted operations like delete() and touchCheckKey() are done asynchronously
58 * in all datacenters this way, though the local one should likely be near immediate.
60 * This means that callers in all datacenters may see older values for however many
61 * milliseconds that the purge took to reach that datacenter. As with any cache, this
62 * should not be relied on for cases where reads are used to determine writes to source
63 * (e.g. non-cache) data stores, except when reading immutable data.
65 * All values are wrapped in metadata arrays. Keys use a "WANCache:" prefix
66 * to avoid collisions with keys that are not wrapped as metadata arrays. The
67 * prefixes are as follows:
68 * - a) "WANCache:v" : used for regular value keys
69 * - b) "WANCache:i" : used for temporarily storing values of tombstoned keys
70 * - c) "WANCache:t" : used for storing timestamp "check" keys
71 * - d) "WANCache:m" : used for temporary mutex keys to avoid cache stampedes
76 class WANObjectCache
implements IExpiringStore
, LoggerAwareInterface
{
77 /** @var BagOStuff The local datacenter cache */
79 /** @var HashBagOStuff[] Map of group PHP instance caches */
80 protected $processCaches = [];
81 /** @var string Purge channel name */
82 protected $purgeChannel;
83 /** @var EventRelayer Bus that handles purge broadcasts */
84 protected $purgeRelayer;
85 /** @var LoggerInterface */
88 /** @var int ERR_* constant for the "last error" registry */
89 protected $lastRelayError = self
::ERR_NONE
;
91 /** @var mixed[] Temporary warm-up cache */
92 private $warmupCache = [];
94 /** Max time expected to pass between delete() and DB commit finishing */
95 const MAX_COMMIT_DELAY
= 3;
96 /** Max replication+snapshot lag before applying TTL_LAGGED or disallowing set() */
97 const MAX_READ_LAG
= 7;
98 /** Seconds to tombstone keys on delete() */
99 const HOLDOFF_TTL
= 11; // MAX_COMMIT_DELAY + MAX_READ_LAG + 1
101 /** Seconds to keep dependency purge keys around */
102 const CHECK_KEY_TTL
= self
::TTL_YEAR
;
103 /** Seconds to keep lock keys around */
105 /** Default remaining TTL at which to consider pre-emptive regeneration */
107 /** Default time-since-expiry on a miss that makes a key "hot" */
110 /** Never consider performing "popularity" refreshes until a key reaches this age */
112 /** The time length of the "popularity" refresh window for hot keys */
114 /** Hits/second for a refresh to be expected within the "popularity" window */
115 const HIT_RATE_HIGH
= 1;
116 /** Seconds to ramp up to the "popularity" refresh chance after a key is no longer new */
117 const RAMPUP_TTL
= 30;
119 /** Idiom for getWithSetCallback() callbacks to avoid calling set() */
120 const TTL_UNCACHEABLE
= -1;
121 /** Idiom for getWithSetCallback() callbacks to 'lockTSE' logic */
123 /** Max TTL to store keys when a data sourced is lagged */
124 const TTL_LAGGED
= 30;
125 /** Idiom for delete() for "no hold-off" */
126 const HOLDOFF_NONE
= 0;
127 /** Idiom for getWithSetCallback() for "no minimum required as-of timestamp" */
128 const MIN_TIMESTAMP_NONE
= 0.0;
130 /** Tiny negative float to use when CTL comes up >= 0 due to clock skew */
131 const TINY_NEGATIVE
= -0.000001;
133 /** Cache format version number */
136 const FLD_VERSION
= 0; // key to cache version number
137 const FLD_VALUE
= 1; // key to the cached value
138 const FLD_TTL
= 2; // key to the original TTL
139 const FLD_TIME
= 3; // key to the cache time
140 const FLD_FLAGS
= 4; // key to the flags bitfield
141 const FLD_HOLDOFF
= 5; // key to any hold-off TTL
143 /** @var integer Treat this value as expired-on-arrival */
146 const ERR_NONE
= 0; // no error
147 const ERR_NO_RESPONSE
= 1; // no response
148 const ERR_UNREACHABLE
= 2; // can't connect
149 const ERR_UNEXPECTED
= 3; // response gave some error
150 const ERR_RELAY
= 4; // relay broadcast failed
152 const VALUE_KEY_PREFIX
= 'WANCache:v:';
153 const INTERIM_KEY_PREFIX
= 'WANCache:i:';
154 const TIME_KEY_PREFIX
= 'WANCache:t:';
155 const MUTEX_KEY_PREFIX
= 'WANCache:m:';
157 const PURGE_VAL_PREFIX
= 'PURGED:';
159 const VFLD_DATA
= 'WOC:d'; // key to the value of versioned data
160 const VFLD_VERSION
= 'WOC:v'; // key to the version of the value present
162 const PC_PRIMARY
= 'primary:1000'; // process cache name and max key count
164 const DEFAULT_PURGE_CHANNEL
= 'wancache-purge';
167 * @param array $params
168 * - cache : BagOStuff object for a persistent cache
169 * - channels : Map of (action => channel string). Actions include "purge".
170 * - relayers : Map of (action => EventRelayer object). Actions include "purge".
171 * - logger : LoggerInterface object
173 public function __construct( array $params ) {
174 $this->cache
= $params['cache'];
175 $this->purgeChannel
= isset( $params['channels']['purge'] )
176 ?
$params['channels']['purge']
177 : self
::DEFAULT_PURGE_CHANNEL
;
178 $this->purgeRelayer
= isset( $params['relayers']['purge'] )
179 ?
$params['relayers']['purge']
180 : new EventRelayerNull( [] );
181 $this->setLogger( isset( $params['logger'] ) ?
$params['logger'] : new NullLogger() );
184 public function setLogger( LoggerInterface
$logger ) {
185 $this->logger
= $logger;
189 * Get an instance that wraps EmptyBagOStuff
191 * @return WANObjectCache
193 public static function newEmpty() {
195 'cache' => new EmptyBagOStuff(),
197 'relayer' => new EventRelayerNull( [] )
202 * Fetch the value of a key from cache
204 * If supplied, $curTTL is set to the remaining TTL (current time left):
205 * - a) INF; if $key exists, has no TTL, and is not expired by $checkKeys
206 * - b) float (>=0); if $key exists, has a TTL, and is not expired by $checkKeys
207 * - c) float (<0); if $key is tombstoned, stale, or existing but expired by $checkKeys
208 * - d) null; if $key does not exist and is not tombstoned
210 * If a key is tombstoned, $curTTL will reflect the time since delete().
212 * The timestamp of $key will be checked against the last-purge timestamp
213 * of each of $checkKeys. Those $checkKeys not in cache will have the last-purge
214 * initialized to the current timestamp. If any of $checkKeys have a timestamp
215 * greater than that of $key, then $curTTL will reflect how long ago $key
216 * became invalid. Callers can use $curTTL to know when the value is stale.
217 * The $checkKeys parameter allow mass invalidations by updating a single key:
218 * - a) Each "check" key represents "last purged" of some source data
219 * - b) Callers pass in relevant "check" keys as $checkKeys in get()
220 * - c) When the source data that "check" keys represent changes,
221 * the touchCheckKey() method is called on them
223 * Source data entities might exists in a DB that uses snapshot isolation
224 * (e.g. the default REPEATABLE-READ in innoDB). Even for mutable data, that
225 * isolation can largely be maintained by doing the following:
226 * - a) Calling delete() on entity change *and* creation, before DB commit
227 * - b) Keeping transaction duration shorter than delete() hold-off TTL
229 * However, pre-snapshot values might still be seen if an update was made
230 * in a remote datacenter but the purge from delete() didn't relay yet.
232 * Consider using getWithSetCallback() instead of get() and set() cycles.
233 * That method has cache slam avoiding features for hot/expensive keys.
235 * @param string $key Cache key
236 * @param mixed $curTTL Approximate TTL left on the key if present/tombstoned [returned]
237 * @param array $checkKeys List of "check" keys
238 * @param float &$asOf UNIX timestamp of cached value; null on failure [returned]
239 * @return mixed Value of cache key or false on failure
241 final public function get( $key, &$curTTL = null, array $checkKeys = [], &$asOf = null ) {
244 $values = $this->getMulti( [ $key ], $curTTLs, $checkKeys, $asOfs );
245 $curTTL = isset( $curTTLs[$key] ) ?
$curTTLs[$key] : null;
246 $asOf = isset( $asOfs[$key] ) ?
$asOfs[$key] : null;
248 return isset( $values[$key] ) ?
$values[$key] : false;
252 * Fetch the value of several keys from cache
254 * @see WANObjectCache::get()
256 * @param array $keys List of cache keys
257 * @param array $curTTLs Map of (key => approximate TTL left) for existing keys [returned]
258 * @param array $checkKeys List of check keys to apply to all $keys. May also apply "check"
259 * keys to specific cache keys only by using cache keys as keys in the $checkKeys array.
260 * @param float[] &$asOfs Map of (key => UNIX timestamp of cached value; null on failure)
261 * @return array Map of (key => value) for keys that exist
263 final public function getMulti(
264 array $keys, &$curTTLs = [], array $checkKeys = [], array &$asOfs = []
270 $vPrefixLen = strlen( self
::VALUE_KEY_PREFIX
);
271 $valueKeys = self
::prefixCacheKeys( $keys, self
::VALUE_KEY_PREFIX
);
273 $checkKeysForAll = [];
274 $checkKeysByKey = [];
276 foreach ( $checkKeys as $i => $keys ) {
277 $prefixed = self
::prefixCacheKeys( (array)$keys, self
::TIME_KEY_PREFIX
);
278 $checkKeysFlat = array_merge( $checkKeysFlat, $prefixed );
279 // Is this check keys for a specific cache key, or for all keys being fetched?
280 if ( is_int( $i ) ) {
281 $checkKeysForAll = array_merge( $checkKeysForAll, $prefixed );
283 $checkKeysByKey[$i] = isset( $checkKeysByKey[$i] )
284 ?
array_merge( $checkKeysByKey[$i], $prefixed )
289 // Fetch all of the raw values
290 $keysGet = array_merge( $valueKeys, $checkKeysFlat );
291 if ( $this->warmupCache
) {
292 $wrappedValues = array_intersect_key( $this->warmupCache
, array_flip( $keysGet ) );
293 $keysGet = array_diff( $keysGet, array_keys( $wrappedValues ) ); // keys left to fetch
297 $wrappedValues +
= $this->cache
->getMulti( $keysGet );
298 // Time used to compare/init "check" keys (derived after getMulti() to be pessimistic)
299 $now = microtime( true );
301 // Collect timestamps from all "check" keys
302 $purgeValuesForAll = $this->processCheckKeys( $checkKeysForAll, $wrappedValues, $now );
303 $purgeValuesByKey = [];
304 foreach ( $checkKeysByKey as $cacheKey => $checks ) {
305 $purgeValuesByKey[$cacheKey] =
306 $this->processCheckKeys( $checks, $wrappedValues, $now );
309 // Get the main cache value for each key and validate them
310 foreach ( $valueKeys as $vKey ) {
311 if ( !isset( $wrappedValues[$vKey] ) ) {
312 continue; // not found
315 $key = substr( $vKey, $vPrefixLen ); // unprefix
317 list( $value, $curTTL ) = $this->unwrap( $wrappedValues[$vKey], $now );
318 if ( $value !== false ) {
319 $result[$key] = $value;
321 // Force dependant keys to be invalid for a while after purging
322 // to reduce race conditions involving stale data getting cached
323 $purgeValues = $purgeValuesForAll;
324 if ( isset( $purgeValuesByKey[$key] ) ) {
325 $purgeValues = array_merge( $purgeValues, $purgeValuesByKey[$key] );
327 foreach ( $purgeValues as $purge ) {
328 $safeTimestamp = $purge[self
::FLD_TIME
] +
$purge[self
::FLD_HOLDOFF
];
329 if ( $safeTimestamp >= $wrappedValues[$vKey][self
::FLD_TIME
] ) {
330 // How long ago this value was expired by *this* check key
331 $ago = min( $purge[self
::FLD_TIME
] - $now, self
::TINY_NEGATIVE
);
332 // How long ago this value was expired by *any* known check key
333 $curTTL = min( $curTTL, $ago );
337 $curTTLs[$key] = $curTTL;
338 $asOfs[$key] = ( $value !== false ) ?
$wrappedValues[$vKey][self
::FLD_TIME
] : null;
346 * @param array $timeKeys List of prefixed time check keys
347 * @param array $wrappedValues
349 * @return array List of purge value arrays
351 private function processCheckKeys( array $timeKeys, array $wrappedValues, $now ) {
353 foreach ( $timeKeys as $timeKey ) {
354 $purge = isset( $wrappedValues[$timeKey] )
355 ? self
::parsePurgeValue( $wrappedValues[$timeKey] )
357 if ( $purge === false ) {
358 // Key is not set or invalid; regenerate
359 $newVal = $this->makePurgeValue( $now, self
::HOLDOFF_TTL
);
360 $this->cache
->add( $timeKey, $newVal, self
::CHECK_KEY_TTL
);
361 $purge = self
::parsePurgeValue( $newVal );
363 $purgeValues[] = $purge;
369 * Set the value of a key in cache
371 * Simply calling this method when source data changes is not valid because
372 * the changes do not replicate to the other WAN sites. In that case, delete()
373 * should be used instead. This method is intended for use on cache misses.
375 * If the data was read from a snapshot-isolated transactions (e.g. the default
376 * REPEATABLE-READ in innoDB), use 'since' to avoid the following race condition:
378 * - b) T2 updates a row, calls delete(), and commits
379 * - c) The HOLDOFF_TTL passes, expiring the delete() tombstone
380 * - d) T1 reads the row and calls set() due to a cache miss
381 * - e) Stale value is stuck in cache
383 * Setting 'lag' and 'since' help avoids keys getting stuck in stale states.
387 * $dbr = wfGetDB( DB_REPLICA );
388 * $setOpts = Database::getCacheSetOptions( $dbr );
389 * // Fetch the row from the DB
390 * $row = $dbr->selectRow( ... );
391 * $key = $cache->makeKey( 'building', $buildingId );
392 * $cache->set( $key, $row, $cache::TTL_DAY, $setOpts );
395 * @param string $key Cache key
396 * @param mixed $value
397 * @param integer $ttl Seconds to live. Special values are:
398 * - WANObjectCache::TTL_INDEFINITE: Cache forever
399 * @param array $opts Options map:
400 * - lag : Seconds of replica DB lag. Typically, this is either the replica DB lag
401 * before the data was read or, if applicable, the replica DB lag before
402 * the snapshot-isolated transaction the data was read from started.
403 * Use false to indicate that replication is not running.
405 * - since : UNIX timestamp of the data in $value. Typically, this is either
406 * the current time the data was read or (if applicable) the time when
407 * the snapshot-isolated transaction the data was read from started.
409 * - pending : Whether this data is possibly from an uncommitted write transaction.
410 * Generally, other threads should not see values from the future and
411 * they certainly should not see ones that ended up getting rolled back.
413 * - lockTSE : if excessive replication/snapshot lag is detected, then store the value
414 * with this TTL and flag it as stale. This is only useful if the reads for
415 * this key use getWithSetCallback() with "lockTSE" set.
416 * Default: WANObjectCache::TSE_NONE
417 * - staleTTL : Seconds to keep the key around if it is stale. The get()/getMulti()
418 * methods return such stale values with a $curTTL of 0, and getWithSetCallback()
419 * will call the regeneration callback in such cases, passing in the old value
420 * and its as-of time to the callback. This is useful if adaptiveTTL() is used
421 * on the old value's as-of time when it is verified as still being correct.
423 * @note Options added in 1.28: staleTTL
424 * @return bool Success
426 final public function set( $key, $value, $ttl = 0, array $opts = [] ) {
427 $now = microtime( true );
428 $lockTSE = isset( $opts['lockTSE'] ) ?
$opts['lockTSE'] : self
::TSE_NONE
;
429 $age = isset( $opts['since'] ) ?
max( 0, $now - $opts['since'] ) : 0;
430 $lag = isset( $opts['lag'] ) ?
$opts['lag'] : 0;
431 $staleTTL = isset( $opts['staleTTL'] ) ?
$opts['staleTTL'] : 0;
433 // Do not cache potentially uncommitted data as it might get rolled back
434 if ( !empty( $opts['pending'] ) ) {
435 $this->logger
->info( "Rejected set() for $key due to pending writes." );
437 return true; // no-op the write for being unsafe
440 $wrapExtra = []; // additional wrapped value fields
441 // Check if there's a risk of writing stale data after the purge tombstone expired
442 if ( $lag === false ||
( $lag +
$age ) > self
::MAX_READ_LAG
) {
443 // Case A: read lag with "lockTSE"; save but record value as stale
444 if ( $lockTSE >= 0 ) {
445 $ttl = max( 1, (int)$lockTSE ); // set() expects seconds
446 $wrapExtra[self
::FLD_FLAGS
] = self
::FLG_STALE
; // mark as stale
447 // Case B: any long-running transaction; ignore this set()
448 } elseif ( $age > self
::MAX_READ_LAG
) {
449 $this->logger
->warning( "Rejected set() for $key due to snapshot lag." );
451 return true; // no-op the write for being unsafe
452 // Case C: high replication lag; lower TTL instead of ignoring all set()s
453 } elseif ( $lag === false ||
$lag > self
::MAX_READ_LAG
) {
454 $ttl = $ttl ?
min( $ttl, self
::TTL_LAGGED
) : self
::TTL_LAGGED
;
455 $this->logger
->warning( "Lowered set() TTL for $key due to replication lag." );
456 // Case D: medium length request with medium replication lag; ignore this set()
458 $this->logger
->warning( "Rejected set() for $key due to high read lag." );
460 return true; // no-op the write for being unsafe
464 // Wrap that value with time/TTL/version metadata
465 $wrapped = $this->wrap( $value, $ttl, $now ) +
$wrapExtra;
467 $func = function ( $cache, $key, $cWrapped ) use ( $wrapped ) {
468 return ( is_string( $cWrapped ) )
469 ?
false // key is tombstoned; do nothing
473 return $this->cache
->merge( self
::VALUE_KEY_PREFIX
. $key, $func, $ttl +
$staleTTL, 1 );
477 * Purge a key from all datacenters
479 * This should only be called when the underlying data (being cached)
480 * changes in a significant way. This deletes the key and starts a hold-off
481 * period where the key cannot be written to for a few seconds (HOLDOFF_TTL).
482 * This is done to avoid the following race condition:
483 * - a) Some DB data changes and delete() is called on a corresponding key
484 * - b) A request refills the key with a stale value from a lagged DB
485 * - c) The stale value is stuck there until the key is expired/evicted
487 * This is implemented by storing a special "tombstone" value at the cache
488 * key that this class recognizes; get() calls will return false for the key
489 * and any set() calls will refuse to replace tombstone values at the key.
490 * For this to always avoid stale value writes, the following must hold:
491 * - a) Replication lag is bounded to being less than HOLDOFF_TTL; or
492 * - b) If lag is higher, the DB will have gone into read-only mode already
494 * Note that set() can also be lag-aware and lower the TTL if it's high.
496 * When using potentially long-running ACID transactions, a good pattern is
497 * to use a pre-commit hook to issue the delete. This means that immediately
498 * after commit, callers will see the tombstone in cache upon purge relay.
499 * It also avoids the following race condition:
500 * - a) T1 begins, changes a row, and calls delete()
501 * - b) The HOLDOFF_TTL passes, expiring the delete() tombstone
502 * - c) T2 starts, reads the row and calls set() due to a cache miss
503 * - d) T1 finally commits
504 * - e) Stale value is stuck in cache
508 * $dbw->startAtomic( __METHOD__ ); // start of request
509 * ... <execute some stuff> ...
510 * // Update the row in the DB
511 * $dbw->update( ... );
512 * $key = $cache->makeKey( 'homes', $homeId );
513 * // Purge the corresponding cache entry just before committing
514 * $dbw->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) {
515 * $cache->delete( $key );
517 * ... <execute some stuff> ...
518 * $dbw->endAtomic( __METHOD__ ); // end of request
521 * The $ttl parameter can be used when purging values that have not actually changed
522 * recently. For example, a cleanup script to purge cache entries does not really need
523 * a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge.
524 * Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback().
526 * If called twice on the same key, then the last hold-off TTL takes precedence. For
527 * idempotence, the $ttl should not vary for different delete() calls on the same key.
529 * @param string $key Cache key
530 * @param integer $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL
531 * @return bool True if the item was purged or not found, false on failure
533 final public function delete( $key, $ttl = self
::HOLDOFF_TTL
) {
534 $key = self
::VALUE_KEY_PREFIX
. $key;
537 // Publish the purge to all datacenters
538 $ok = $this->relayDelete( $key );
540 // Publish the purge to all datacenters
541 $ok = $this->relayPurge( $key, $ttl, self
::HOLDOFF_NONE
);
548 * Fetch the value of a timestamp "check" key
550 * The key will be *initialized* to the current time if not set,
551 * so only call this method if this behavior is actually desired
553 * The timestamp can be used to check whether a cached value is valid.
554 * Callers should not assume that this returns the same timestamp in
555 * all datacenters due to relay delays.
557 * The level of staleness can roughly be estimated from this key, but
558 * if the key was evicted from cache, such calculations may show the
559 * time since expiry as ~0 seconds.
561 * Note that "check" keys won't collide with other regular keys.
564 * @return float UNIX timestamp of the check key
566 final public function getCheckKeyTime( $key ) {
567 $key = self
::TIME_KEY_PREFIX
. $key;
569 $purge = self
::parsePurgeValue( $this->cache
->get( $key ) );
570 if ( $purge !== false ) {
571 $time = $purge[self
::FLD_TIME
];
573 // Casting assures identical floats for the next getCheckKeyTime() calls
574 $now = (string)microtime( true );
575 $this->cache
->add( $key,
576 $this->makePurgeValue( $now, self
::HOLDOFF_TTL
),
586 * Purge a "check" key from all datacenters, invalidating keys that use it
588 * This should only be called when the underlying data (being cached)
589 * changes in a significant way, and it is impractical to call delete()
590 * on all keys that should be changed. When get() is called on those
591 * keys, the relevant "check" keys must be supplied for this to work.
593 * The "check" key essentially represents a last-modified field.
594 * When touched, the field will be updated on all cache servers.
595 * Keys using it via get(), getMulti(), or getWithSetCallback() will
596 * be invalidated. It is treated as being HOLDOFF_TTL seconds in the future
597 * by those methods to avoid race conditions where dependent keys get updated
598 * with stale values (e.g. from a DB replica DB).
600 * This is typically useful for keys with hardcoded names or in some cases
601 * dynamically generated names where a low number of combinations exist.
602 * When a few important keys get a large number of hits, a high cache
603 * time is usually desired as well as "lockTSE" logic. The resetCheckKey()
604 * method is less appropriate in such cases since the "time since expiry"
605 * cannot be inferred, causing any get() after the reset to treat the key
606 * as being "hot", resulting in more stale value usage.
608 * Note that "check" keys won't collide with other regular keys.
610 * @see WANObjectCache::get()
611 * @see WANObjectCache::getWithSetCallback()
612 * @see WANObjectCache::resetCheckKey()
614 * @param string $key Cache key
615 * @param int $holdoff HOLDOFF_TTL or HOLDOFF_NONE constant
616 * @return bool True if the item was purged or not found, false on failure
618 final public function touchCheckKey( $key, $holdoff = self
::HOLDOFF_TTL
) {
619 // Publish the purge to all datacenters
620 return $this->relayPurge( self
::TIME_KEY_PREFIX
. $key, self
::CHECK_KEY_TTL
, $holdoff );
624 * Delete a "check" key from all datacenters, invalidating keys that use it
626 * This is similar to touchCheckKey() in that keys using it via get(), getMulti(),
627 * or getWithSetCallback() will be invalidated. The differences are:
628 * - a) The "check" key will be deleted from all caches and lazily
629 * re-initialized when accessed (rather than set everywhere)
630 * - b) Thus, dependent keys will be known to be invalid, but not
631 * for how long (they are treated as "just" purged), which
632 * effects any lockTSE logic in getWithSetCallback()
633 * - c) Since "check" keys are initialized only on the server the key hashes
634 * to, any temporary ejection of that server will cause the value to be
635 * seen as purged as a new server will initialize the "check" key.
637 * The advantage is that this does not place high TTL keys on every cache
638 * server, making it better for code that will cache many different keys
639 * and either does not use lockTSE or uses a low enough TTL anyway.
641 * This is typically useful for keys with dynamically generated names
642 * where a high number of combinations exist.
644 * Note that "check" keys won't collide with other regular keys.
646 * @see WANObjectCache::get()
647 * @see WANObjectCache::getWithSetCallback()
648 * @see WANObjectCache::touchCheckKey()
650 * @param string $key Cache key
651 * @return bool True if the item was purged or not found, false on failure
653 final public function resetCheckKey( $key ) {
654 // Publish the purge to all datacenters
655 return $this->relayDelete( self
::TIME_KEY_PREFIX
. $key );
659 * Method to fetch/regenerate cache keys
661 * On cache miss, the key will be set to the callback result via set()
662 * (unless the callback returns false) and that result will be returned.
663 * The arguments supplied to the callback are:
664 * - $oldValue : current cache value or false if not present
665 * - &$ttl : a reference to the TTL which can be altered
666 * - &$setOpts : a reference to options for set() which can be altered
667 * - $oldAsOf : generation UNIX timestamp of $oldValue or null if not present (since 1.28)
669 * It is strongly recommended to set the 'lag' and 'since' fields to avoid race conditions
670 * that can cause stale values to get stuck at keys. Usually, callbacks ignore the current
671 * value, but it can be used to maintain "most recent X" values that come from time or
672 * sequence based source data, provided that the "as of" id/time is tracked. Note that
673 * preemptive regeneration and $checkKeys can result in a non-false current value.
675 * Usage of $checkKeys is similar to get() and getMulti(). However, rather than the caller
676 * having to inspect a "current time left" variable (e.g. $curTTL, $curTTLs), a cache
677 * regeneration will automatically be triggered using the callback.
679 * The simplest way to avoid stampedes for hot keys is to use
680 * the 'lockTSE' option in $opts. If cache purges are needed, also:
681 * - a) Pass $key into $checkKeys
682 * - b) Use touchCheckKey( $key ) instead of delete( $key )
684 * Example usage (typical key):
686 * $catInfo = $cache->getWithSetCallback(
687 * // Key to store the cached value under
688 * $cache->makeKey( 'cat-attributes', $catId ),
689 * // Time-to-live (in seconds)
690 * $cache::TTL_MINUTE,
691 * // Function that derives the new key value
692 * function ( $oldValue, &$ttl, array &$setOpts ) {
693 * $dbr = wfGetDB( DB_REPLICA );
694 * // Account for any snapshot/replica DB lag
695 * $setOpts += Database::getCacheSetOptions( $dbr );
697 * return $dbr->selectRow( ... );
702 * Example usage (key that is expensive and hot):
704 * $catConfig = $cache->getWithSetCallback(
705 * // Key to store the cached value under
706 * $cache->makeKey( 'site-cat-config' ),
707 * // Time-to-live (in seconds)
709 * // Function that derives the new key value
710 * function ( $oldValue, &$ttl, array &$setOpts ) {
711 * $dbr = wfGetDB( DB_REPLICA );
712 * // Account for any snapshot/replica DB lag
713 * $setOpts += Database::getCacheSetOptions( $dbr );
715 * return CatConfig::newFromRow( $dbr->selectRow( ... ) );
718 * // Calling touchCheckKey() on this key invalidates the cache
719 * 'checkKeys' => [ $cache->makeKey( 'site-cat-config' ) ],
720 * // Try to only let one datacenter thread manage cache updates at a time
722 * // Avoid querying cache servers multiple times in a web request
723 * 'pcTTL' => $cache::TTL_PROC_LONG
728 * Example usage (key with dynamic dependencies):
730 * $catState = $cache->getWithSetCallback(
731 * // Key to store the cached value under
732 * $cache->makeKey( 'cat-state', $cat->getId() ),
733 * // Time-to-live (seconds)
735 * // Function that derives the new key value
736 * function ( $oldValue, &$ttl, array &$setOpts ) {
737 * // Determine new value from the DB
738 * $dbr = wfGetDB( DB_REPLICA );
739 * // Account for any snapshot/replica DB lag
740 * $setOpts += Database::getCacheSetOptions( $dbr );
742 * return CatState::newFromResults( $dbr->select( ... ) );
745 * // The "check" keys that represent things the value depends on;
746 * // Calling touchCheckKey() on any of them invalidates the cache
748 * $cache->makeKey( 'sustenance-bowls', $cat->getRoomId() ),
749 * $cache->makeKey( 'people-present', $cat->getHouseId() ),
750 * $cache->makeKey( 'cat-laws', $cat->getCityId() ),
756 * Example usage (hot key holding most recent 100 events):
758 * $lastCatActions = $cache->getWithSetCallback(
759 * // Key to store the cached value under
760 * $cache->makeKey( 'cat-last-actions', 100 ),
761 * // Time-to-live (in seconds)
763 * // Function that derives the new key value
764 * function ( $oldValue, &$ttl, array &$setOpts ) {
765 * $dbr = wfGetDB( DB_REPLICA );
766 * // Account for any snapshot/replica DB lag
767 * $setOpts += Database::getCacheSetOptions( $dbr );
769 * // Start off with the last cached list
770 * $list = $oldValue ?: [];
771 * // Fetch the last 100 relevant rows in descending order;
772 * // only fetch rows newer than $list[0] to reduce scanning
773 * $rows = iterator_to_array( $dbr->select( ... ) );
774 * // Merge them and get the new "last 100" rows
775 * return array_slice( array_merge( $new, $list ), 0, 100 );
778 * // Try to only let one datacenter thread manage cache updates at a time
780 * // Use a magic value when no cache value is ready rather than stampeding
781 * 'busyValue' => 'computing'
786 * @see WANObjectCache::get()
787 * @see WANObjectCache::set()
789 * @param string $key Cache key
790 * @param integer $ttl Seconds to live for key updates. Special values are:
791 * - WANObjectCache::TTL_INDEFINITE: Cache forever
792 * - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
793 * @param callable $callback Value generation function
794 * @param array $opts Options map:
795 * - checkKeys: List of "check" keys. The key at $key will be seen as invalid when either
796 * touchCheckKey() or resetCheckKey() is called on any of these keys.
798 * - lockTSE: If the key is tombstoned or expired (by checkKeys) less than this many seconds
799 * ago, then try to have a single thread handle cache regeneration at any given time.
800 * Other threads will try to use stale values if possible. If, on miss, the time since
801 * expiration is low, the assumption is that the key is hot and that a stampede is worth
802 * avoiding. Setting this above WANObjectCache::HOLDOFF_TTL makes no difference. The
803 * higher this is set, the higher the worst-case staleness can be.
804 * Use WANObjectCache::TSE_NONE to disable this logic.
805 * Default: WANObjectCache::TSE_NONE.
806 * - busyValue: If no value exists and another thread is currently regenerating it, use this
807 * as a fallback value (or a callback to generate such a value). This assures that cache
808 * stampedes cannot happen if the value falls out of cache. This can be used as insurance
809 * against cache regeneration becoming very slow for some reason (greater than the TTL).
811 * - pcTTL: Process cache the value in this PHP instance for this many seconds. This avoids
812 * network I/O when a key is read several times. This will not cache when the callback
813 * returns false, however. Note that any purges will not be seen while process cached;
814 * since the callback should use replica DBs and they may be lagged or have snapshot
815 * isolation anyway, this should not typically matter.
816 * Default: WANObjectCache::TTL_UNCACHEABLE.
817 * - pcGroup: Process cache group to use instead of the primary one. If set, this must be
818 * of the format ALPHANUMERIC_NAME:MAX_KEY_SIZE, e.g. "mydata:10". Use this for storing
819 * large values, small yet numerous values, or some values with a high cost of eviction.
820 * It is generally preferable to use a class constant when setting this value.
821 * This has no effect unless pcTTL is used.
822 * Default: WANObjectCache::PC_PRIMARY.
823 * - version: Integer version number. This allows for callers to make breaking changes to
824 * how values are stored while maintaining compatability and correct cache purges. New
825 * versions are stored alongside older versions concurrently. Avoid storing class objects
826 * however, as this reduces compatibility (due to serialization).
828 * - minAsOf: Reject values if they were generated before this UNIX timestamp.
829 * This is useful if the source of a key is suspected of having possibly changed
830 * recently, and the caller wants any such changes to be reflected.
831 * Default: WANObjectCache::MIN_TIMESTAMP_NONE.
832 * - hotTTR: Expected time-till-refresh for keys that average ~1 hit/second.
833 * This should be greater than "ageNew". Keys with higher hit rates will regenerate
834 * more often. This is useful when a popular key is changed but the cache purge was
835 * delayed or lost. Seldom used keys are rarely affected by this setting, unless an
836 * extremely low "hotTTR" value is passed in.
837 * Default: WANObjectCache::HOT_TTR.
838 * - lowTTL: Consider pre-emptive updates when the current TTL (seconds) of the key is less
839 * than this. It becomes more likely over time, becoming certain once the key is expired.
840 * Default: WANObjectCache::LOW_TTL.
841 * - ageNew: Consider popularity refreshes only once a key reaches this age in seconds.
842 * Default: WANObjectCache::AGE_NEW.
843 * @return mixed Value found or written to the key
844 * @note Options added in 1.28: version, busyValue, hotTTR, ageNew, pcGroup, minAsOf
845 * @note Callable type hints are not used to avoid class-autoloading
847 final public function getWithSetCallback( $key, $ttl, $callback, array $opts = [] ) {
848 $pcTTL = isset( $opts['pcTTL'] ) ?
$opts['pcTTL'] : self
::TTL_UNCACHEABLE
;
850 // Try the process cache if enabled
852 $group = isset( $opts['pcGroup'] ) ?
$opts['pcGroup'] : self
::PC_PRIMARY
;
853 $procCache = $this->getProcessCache( $group );
854 $value = $procCache->get( $key );
860 if ( $value === false ) {
861 // Fetch the value over the network
862 if ( isset( $opts['version'] ) ) {
863 $version = $opts['version'];
865 $cur = $this->doGetWithSetCallback(
868 function ( $oldValue, &$ttl, &$setOpts, $oldAsOf )
869 use ( $callback, $version ) {
870 if ( is_array( $oldValue )
871 && array_key_exists( self
::VFLD_DATA
, $oldValue )
873 $oldData = $oldValue[self
::VFLD_DATA
];
875 // VFLD_DATA is not set if an old, unversioned, key is present
880 self
::VFLD_DATA
=> $callback( $oldData, $ttl, $setOpts, $oldAsOf ),
881 self
::VFLD_VERSION
=> $version
887 if ( $cur[self
::VFLD_VERSION
] === $version ) {
888 // Value created or existed before with version; use it
889 $value = $cur[self
::VFLD_DATA
];
891 // Value existed before with a different version; use variant key.
892 // Reflect purges to $key by requiring that this key value be newer.
893 $value = $this->doGetWithSetCallback(
894 'cache-variant:' . md5( $key ) . ":$version",
897 // Regenerate value if not newer than $key
898 [ 'version' => null, 'minAsOf' => $asOf ] +
$opts
902 $value = $this->doGetWithSetCallback( $key, $ttl, $callback, $opts );
905 // Update the process cache if enabled
906 if ( $procCache && $value !== false ) {
907 $procCache->set( $key, $value, $pcTTL );
915 * Do the actual I/O for getWithSetCallback() when needed
917 * @see WANObjectCache::getWithSetCallback()
920 * @param integer $ttl
921 * @param callback $callback
922 * @param array $opts Options map for getWithSetCallback()
923 * @param float &$asOf Cache generation timestamp of returned value [returned]
925 * @note Callable type hints are not used to avoid class-autoloading
927 protected function doGetWithSetCallback( $key, $ttl, $callback, array $opts, &$asOf = null ) {
928 $lowTTL = isset( $opts['lowTTL'] ) ?
$opts['lowTTL'] : min( self
::LOW_TTL
, $ttl );
929 $lockTSE = isset( $opts['lockTSE'] ) ?
$opts['lockTSE'] : self
::TSE_NONE
;
930 $checkKeys = isset( $opts['checkKeys'] ) ?
$opts['checkKeys'] : [];
931 $busyValue = isset( $opts['busyValue'] ) ?
$opts['busyValue'] : null;
932 $popWindow = isset( $opts['hotTTR'] ) ?
$opts['hotTTR'] : self
::HOT_TTR
;
933 $ageNew = isset( $opts['ageNew'] ) ?
$opts['ageNew'] : self
::AGE_NEW
;
934 $minTime = isset( $opts['minAsOf'] ) ?
$opts['minAsOf'] : self
::MIN_TIMESTAMP_NONE
;
935 $versioned = isset( $opts['version'] );
937 // Get the current key value
939 $cValue = $this->get( $key, $curTTL, $checkKeys, $asOf ); // current value
940 $value = $cValue; // return value
942 // Determine if a regeneration is desired
943 if ( $value !== false
945 && $this->isValid( $value, $versioned, $asOf, $minTime )
946 && !$this->worthRefreshExpiring( $curTTL, $lowTTL )
947 && !$this->worthRefreshPopular( $asOf, $ageNew, $popWindow )
952 // A deleted key with a negative TTL left must be tombstoned
953 $isTombstone = ( $curTTL !== null && $value === false );
954 // Assume a key is hot if requested soon after invalidation
955 $isHot = ( $curTTL !== null && $curTTL <= 0 && abs( $curTTL ) <= $lockTSE );
956 // Use the mutex if there is no value and a busy fallback is given
957 $checkBusy = ( $busyValue !== null && $value === false );
958 // Decide whether a single thread should handle regenerations.
959 // This avoids stampedes when $checkKeys are bumped and when preemptive
960 // renegerations take too long. It also reduces regenerations while $key
961 // is tombstoned. This balances cache freshness with avoiding DB load.
962 $useMutex = ( $isHot ||
( $isTombstone && $lockTSE > 0 ) ||
$checkBusy );
964 $lockAcquired = false;
966 // Acquire a datacenter-local non-blocking lock
967 if ( $this->cache
->add( self
::MUTEX_KEY_PREFIX
. $key, 1, self
::LOCK_TTL
) ) {
968 // Lock acquired; this thread should update the key
969 $lockAcquired = true;
970 } elseif ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
971 // If it cannot be acquired; then the stale value can be used
974 // Use the INTERIM value for tombstoned keys to reduce regeneration load.
975 // For hot keys, either another thread has the lock or the lock failed;
976 // use the INTERIM value from the last thread that regenerated it.
977 $wrapped = $this->cache
->get( self
::INTERIM_KEY_PREFIX
. $key );
978 list( $value ) = $this->unwrap( $wrapped, microtime( true ) );
979 if ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
980 $asOf = $wrapped[self
::FLD_TIME
];
984 // Use the busy fallback value if nothing else
985 if ( $busyValue !== null ) {
986 return is_callable( $busyValue ) ?
$busyValue() : $busyValue;
991 if ( !is_callable( $callback ) ) {
992 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
995 // Generate the new value from the callback...
997 $value = call_user_func_array( $callback, [ $cValue, &$ttl, &$setOpts, $asOf ] );
998 // When delete() is called, writes are write-holed by the tombstone,
999 // so use a special INTERIM key to pass the new value around threads.
1000 if ( ( $isTombstone && $lockTSE > 0 ) && $value !== false && $ttl >= 0 ) {
1001 $tempTTL = max( 1, (int)$lockTSE ); // set() expects seconds
1002 $newAsOf = microtime( true );
1003 $wrapped = $this->wrap( $value, $tempTTL, $newAsOf );
1004 // Avoid using set() to avoid pointless mcrouter broadcasting
1005 $this->cache
->merge(
1006 self
::INTERIM_KEY_PREFIX
. $key,
1007 function () use ( $wrapped ) {
1015 if ( $value !== false && $ttl >= 0 ) {
1016 // Update the cache; this will fail if the key is tombstoned
1017 $setOpts['lockTSE'] = $lockTSE;
1018 $this->set( $key, $value, $ttl, $setOpts );
1021 if ( $lockAcquired ) {
1022 // Avoid using delete() to avoid pointless mcrouter broadcasting
1023 $this->cache
->changeTTL( self
::MUTEX_KEY_PREFIX
. $key, 1 );
1030 * Method to fetch/regenerate multiple cache keys at once
1032 * This works the same as getWithSetCallback() except:
1033 * - a) The $keys argument expects the result of WANObjectCache::makeMultiKeys()
1034 * - b) The $callback argument expects a callback taking the following arguments:
1035 * - $id: ID of an entity to query
1036 * - $oldValue : the prior cache value or false if none was present
1037 * - &$ttl : a reference to the new value TTL in seconds
1038 * - &$setOpts : a reference to options for set() which can be altered
1039 * - $oldAsOf : generation UNIX timestamp of $oldValue or null if not present
1040 * Aside from the additional $id argument, the other arguments function the same
1041 * way they do in getWithSetCallback().
1042 * - c) The return value is a map of (cache key => value) in the order of $keyedIds
1044 * @see WANObjectCache::getWithSetCallback()
1048 * $rows = $cache->getMultiWithSetCallback(
1049 * // Map of cache keys to entity IDs
1050 * $cache->makeMultiKeys(
1051 * $this->fileVersionIds(),
1052 * function ( $id, WANObjectCache $cache ) {
1053 * return $cache->makeKey( 'file-version', $id );
1056 * // Time-to-live (in seconds)
1058 * // Function that derives the new key value
1059 * return function ( $id, $oldValue, &$ttl, array &$setOpts ) {
1060 * $dbr = wfGetDB( DB_REPLICA );
1061 * // Account for any snapshot/replica DB lag
1062 * $setOpts += Database::getCacheSetOptions( $dbr );
1064 * // Load the row for this file
1065 * $row = $dbr->selectRow( 'file', '*', [ 'id' => $id ], __METHOD__ );
1067 * return $row ? (array)$row : false;
1070 * // Process cache for 30 seconds
1072 * // Use a dedicated 500 item cache (initialized on-the-fly)
1073 * 'pcGroup' => 'file-versions:500'
1076 * $files = array_map( [ __CLASS__, 'newFromRow' ], $rows );
1079 * @param ArrayIterator $keyedIds Result of WANObjectCache::makeMultiKeys()
1080 * @param integer $ttl Seconds to live for key updates
1081 * @param callable $callback Callback the yields entity regeneration callbacks
1082 * @param array $opts Options map
1083 * @return array Map of (cache key => value) in the same order as $keyedIds
1086 final public function getMultiWithSetCallback(
1087 ArrayIterator
$keyedIds, $ttl, callable
$callback, array $opts = []
1089 $keysWarmUp = iterator_to_array( $keyedIds, true );
1090 $checkKeys = isset( $opts['checkKeys'] ) ?
$opts['checkKeys'] : [];
1091 foreach ( $checkKeys as $i => $checkKeyOrKeys ) {
1092 if ( is_int( $i ) ) {
1093 $keysWarmUp[] = $checkKeyOrKeys;
1095 $keysWarmUp = array_merge( $keysWarmUp, $checkKeyOrKeys );
1099 $this->warmupCache
= $this->cache
->getMulti( $keysWarmUp );
1100 $this->warmupCache +
= array_fill_keys( $keysWarmUp, false );
1102 // Wrap $callback to match the getWithSetCallback() format while passing $id to $callback
1104 $func = function ( $oldValue, &$ttl, array $setOpts, $oldAsOf ) use ( $callback, &$id ) {
1105 return $callback( $id, $oldValue, $ttl, $setOpts, $oldAsOf );
1109 foreach ( $keyedIds as $key => $id ) {
1110 $values[$key] = $this->getWithSetCallback( $key, $ttl, $func, $opts );
1113 $this->warmupCache
= [];
1119 * @see BagOStuff::makeKey()
1120 * @param string ... Key component
1124 public function makeKey() {
1125 return call_user_func_array( [ $this->cache
, __FUNCTION__
], func_get_args() );
1129 * @see BagOStuff::makeGlobalKey()
1130 * @param string ... Key component
1134 public function makeGlobalKey() {
1135 return call_user_func_array( [ $this->cache
, __FUNCTION__
], func_get_args() );
1139 * @param array $entities List of entity IDs
1140 * @param callable $keyFunc Callback yielding a key from (entity ID, this WANObjectCache)
1141 * @return ArrayIterator Iterator yielding (cache key => entity ID) in $entities order
1144 public function makeMultiKeys( array $entities, callable
$keyFunc ) {
1146 foreach ( $entities as $entity ) {
1147 $map[$keyFunc( $entity, $this )] = $entity;
1150 return new ArrayIterator( $map );
1154 * Get the "last error" registered; clearLastError() should be called manually
1155 * @return int ERR_* class constant for the "last error" registry
1157 final public function getLastError() {
1158 if ( $this->lastRelayError
) {
1159 // If the cache and the relayer failed, focus on the latter.
1160 // An update not making it to the relayer means it won't show up
1161 // in other DCs (nor will consistent re-hashing see up-to-date values).
1162 // On the other hand, if just the cache update failed, then it should
1163 // eventually be applied by the relayer.
1164 return $this->lastRelayError
;
1167 $code = $this->cache
->getLastError();
1169 case BagOStuff
::ERR_NONE
:
1170 return self
::ERR_NONE
;
1171 case BagOStuff
::ERR_NO_RESPONSE
:
1172 return self
::ERR_NO_RESPONSE
;
1173 case BagOStuff
::ERR_UNREACHABLE
:
1174 return self
::ERR_UNREACHABLE
;
1176 return self
::ERR_UNEXPECTED
;
1181 * Clear the "last error" registry
1183 final public function clearLastError() {
1184 $this->cache
->clearLastError();
1185 $this->lastRelayError
= self
::ERR_NONE
;
1189 * Clear the in-process caches; useful for testing
1193 public function clearProcessCache() {
1194 $this->processCaches
= [];
1198 * @param integer $flag ATTR_* class constant
1199 * @return integer QOS_* class constant
1202 public function getQoS( $flag ) {
1203 return $this->cache
->getQoS( $flag );
1207 * Get a TTL that is higher for objects that have not changed recently
1209 * This is useful for keys that get explicit purges and DB or purge relay
1210 * lag is a potential concern (especially how it interacts with CDN cache)
1214 * // Last-modified time of page
1215 * $mtime = wfTimestamp( TS_UNIX, $page->getTimestamp() );
1216 * // Get adjusted TTL. If $mtime is 3600 seconds ago and $minTTL/$factor left at
1217 * // defaults, then $ttl is 3600 * .2 = 720. If $minTTL was greater than 720, then
1218 * // $ttl would be $minTTL. If $maxTTL was smaller than 720, $ttl would be $maxTTL.
1219 * $ttl = $cache->adaptiveTTL( $mtime, $cache::TTL_DAY );
1222 * @param integer|float $mtime UNIX timestamp
1223 * @param integer $maxTTL Maximum TTL (seconds)
1224 * @param integer $minTTL Minimum TTL (seconds); Default: 30
1225 * @param float $factor Value in the range (0,1); Default: .2
1226 * @return integer Adaptive TTL
1229 public function adaptiveTTL( $mtime, $maxTTL, $minTTL = 30, $factor = .2 ) {
1230 if ( is_float( $mtime ) ||
ctype_digit( $mtime ) ) {
1231 $mtime = (int)$mtime; // handle fractional seconds and string integers
1234 if ( !is_int( $mtime ) ||
$mtime <= 0 ) {
1235 return $minTTL; // no last-modified time provided
1238 $age = time() - $mtime;
1240 return (int)min( $maxTTL, max( $minTTL, $factor * $age ) );
1244 * Do the actual async bus purge of a key
1246 * This must set the key to "PURGED:<UNIX timestamp>:<holdoff>"
1248 * @param string $key Cache key
1249 * @param integer $ttl How long to keep the tombstone [seconds]
1250 * @param integer $holdoff HOLDOFF_* constant controlling how long to ignore sets for this key
1251 * @return bool Success
1253 protected function relayPurge( $key, $ttl, $holdoff ) {
1254 if ( $this->purgeRelayer
instanceof EventRelayerNull
) {
1255 // This handles the mcrouter and the single-DC case
1256 $ok = $this->cache
->set( $key,
1257 $this->makePurgeValue( microtime( true ), self
::HOLDOFF_NONE
),
1261 $event = $this->cache
->modifySimpleRelayEvent( [
1264 'val' => 'PURGED:$UNIXTIME$:' . (int)$holdoff,
1265 'ttl' => max( $ttl, 1 ),
1266 'sbt' => true, // substitute $UNIXTIME$ with actual microtime
1269 $ok = $this->purgeRelayer
->notify( $this->purgeChannel
, $event );
1271 $this->lastRelayError
= self
::ERR_RELAY
;
1279 * Do the actual async bus delete of a key
1281 * @param string $key Cache key
1282 * @return bool Success
1284 protected function relayDelete( $key ) {
1285 if ( $this->purgeRelayer
instanceof EventRelayerNull
) {
1286 // This handles the mcrouter and the single-DC case
1287 $ok = $this->cache
->delete( $key );
1289 $event = $this->cache
->modifySimpleRelayEvent( [
1294 $ok = $this->purgeRelayer
->notify( $this->purgeChannel
, $event );
1296 $this->lastRelayError
= self
::ERR_RELAY
;
1304 * Check if a key should be regenerated (using random probability)
1306 * This returns false if $curTTL >= $lowTTL. Otherwise, the chance
1307 * of returning true increases steadily from 0% to 100% as the $curTTL
1308 * moves from $lowTTL to 0 seconds. This handles widely varying
1309 * levels of cache access traffic.
1311 * @param float $curTTL Approximate TTL left on the key if present
1312 * @param float $lowTTL Consider a refresh when $curTTL is less than this
1315 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1316 if ( $curTTL >= $lowTTL ) {
1318 } elseif ( $curTTL <= 0 ) {
1322 $chance = ( 1 - $curTTL / $lowTTL );
1324 return mt_rand( 1, 1e9
) <= 1e9
* $chance;
1328 * Check if a key is due for randomized regeneration due to its popularity
1330 * This is used so that popular keys can preemptively refresh themselves for higher
1331 * consistency (especially in the case of purge loss/delay). Unpopular keys can remain
1332 * in cache with their high nominal TTL. This means popular keys keep good consistency,
1333 * whether the data changes frequently or not, and long-tail keys get to stay in cache
1334 * and get hits too. Similar to worthRefreshExpiring(), randomization is used.
1336 * @param float $asOf UNIX timestamp of the value
1337 * @param integer $ageNew Age of key when this might recommend refreshing (seconds)
1338 * @param integer $timeTillRefresh Age of key when it should be refreshed if popular (seconds)
1341 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh ) {
1342 $age = microtime( true ) - $asOf;
1343 $timeOld = $age - $ageNew;
1344 if ( $timeOld <= 0 ) {
1348 // Lifecycle is: new, ramp-up refresh chance, full refresh chance
1349 $refreshWindowSec = max( $timeTillRefresh - $ageNew - self
::RAMPUP_TTL
/ 2, 1 );
1350 // P(refresh) * (# hits in $refreshWindowSec) = (expected # of refreshes)
1351 // P(refresh) * ($refreshWindowSec * $popularHitsPerSec) = 1
1352 // P(refresh) = 1/($refreshWindowSec * $popularHitsPerSec)
1353 $chance = 1 / ( self
::HIT_RATE_HIGH
* $refreshWindowSec );
1355 // Ramp up $chance from 0 to its nominal value over RAMPUP_TTL seconds to avoid stampedes
1356 $chance *= ( $timeOld <= self
::RAMPUP_TTL
) ?
$timeOld / self
::RAMPUP_TTL
: 1;
1358 return mt_rand( 1, 1e9
) <= 1e9
* $chance;
1362 * Check whether $value is appropriately versioned and not older than $minTime (if set)
1364 * @param array $value
1365 * @param bool $versioned
1366 * @param float $asOf The time $value was generated
1367 * @param float $minTime The last time the main value was generated (0.0 if unknown)
1370 protected function isValid( $value, $versioned, $asOf, $minTime ) {
1371 if ( $versioned && !isset( $value[self
::VFLD_VERSION
] ) ) {
1373 } elseif ( $minTime > 0 && $asOf < $minTime ) {
1381 * Do not use this method outside WANObjectCache
1383 * @param mixed $value
1384 * @param integer $ttl [0=forever]
1385 * @param float $now Unix Current timestamp just before calling set()
1388 protected function wrap( $value, $ttl, $now ) {
1390 self
::FLD_VERSION
=> self
::VERSION
,
1391 self
::FLD_VALUE
=> $value,
1392 self
::FLD_TTL
=> $ttl,
1393 self
::FLD_TIME
=> $now
1398 * Do not use this method outside WANObjectCache
1400 * @param array|string|bool $wrapped
1401 * @param float $now Unix Current timestamp (preferrably pre-query)
1402 * @return array (mixed; false if absent/invalid, current time left)
1404 protected function unwrap( $wrapped, $now ) {
1405 // Check if the value is a tombstone
1406 $purge = self
::parsePurgeValue( $wrapped );
1407 if ( $purge !== false ) {
1408 // Purged values should always have a negative current $ttl
1409 $curTTL = min( $purge[self
::FLD_TIME
] - $now, self
::TINY_NEGATIVE
);
1410 return [ false, $curTTL ];
1413 if ( !is_array( $wrapped ) // not found
1414 ||
!isset( $wrapped[self
::FLD_VERSION
] ) // wrong format
1415 ||
$wrapped[self
::FLD_VERSION
] !== self
::VERSION
// wrong version
1417 return [ false, null ];
1420 $flags = isset( $wrapped[self
::FLD_FLAGS
] ) ?
$wrapped[self
::FLD_FLAGS
] : 0;
1421 if ( ( $flags & self
::FLG_STALE
) == self
::FLG_STALE
) {
1422 // Treat as expired, with the cache time as the expiration
1423 $age = $now - $wrapped[self
::FLD_TIME
];
1424 $curTTL = min( -$age, self
::TINY_NEGATIVE
);
1425 } elseif ( $wrapped[self
::FLD_TTL
] > 0 ) {
1426 // Get the approximate time left on the key
1427 $age = $now - $wrapped[self
::FLD_TIME
];
1428 $curTTL = max( $wrapped[self
::FLD_TTL
] - $age, 0.0 );
1430 // Key had no TTL, so the time left is unbounded
1434 return [ $wrapped[self
::FLD_VALUE
], $curTTL ];
1438 * @param array $keys
1439 * @param string $prefix
1442 protected static function prefixCacheKeys( array $keys, $prefix ) {
1444 foreach ( $keys as $key ) {
1445 $res[] = $prefix . $key;
1452 * @param string $value Wrapped value like "PURGED:<timestamp>:<holdoff>"
1453 * @return array|bool Array containing a UNIX timestamp (float) and holdoff period (integer),
1454 * or false if value isn't a valid purge value
1456 protected static function parsePurgeValue( $value ) {
1457 if ( !is_string( $value ) ) {
1460 $segments = explode( ':', $value, 3 );
1461 if ( !isset( $segments[0] ) ||
!isset( $segments[1] )
1462 ||
"{$segments[0]}:" !== self
::PURGE_VAL_PREFIX
1466 if ( !isset( $segments[2] ) ) {
1467 // Back-compat with old purge values without holdoff
1468 $segments[2] = self
::HOLDOFF_TTL
;
1471 self
::FLD_TIME
=> (float)$segments[1],
1472 self
::FLD_HOLDOFF
=> (int)$segments[2],
1477 * @param float $timestamp
1478 * @param int $holdoff In seconds
1479 * @return string Wrapped purge value
1481 protected function makePurgeValue( $timestamp, $holdoff ) {
1482 return self
::PURGE_VAL_PREFIX
. (float)$timestamp . ':' . (int)$holdoff;
1486 * @param string $group
1487 * @return HashBagOStuff
1489 protected function getProcessCache( $group ) {
1490 if ( !isset( $this->processCaches
[$group] ) ) {
1491 list( , $n ) = explode( ':', $group );
1492 $this->processCaches
[$group] = new HashBagOStuff( [ 'maxKeys' => (int)$n ] );
1495 return $this->processCaches
[$group];