b9753d3bb28f4922b76ccff98eac9ec6e3777dbd
[lhc/web/wiklou.git] / includes / libs / objectcache / WANObjectCache.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Cache
20 * @author Aaron Schulz
21 */
22
23 use Psr\Log\LoggerAwareInterface;
24 use Psr\Log\LoggerInterface;
25 use Psr\Log\NullLogger;
26
27 /**
28 * Multi-datacenter aware caching interface
29 *
30 * All operations go to the local datacenter cache, except for delete(),
31 * touchCheckKey(), and resetCheckKey(), which broadcast to all datacenters.
32 *
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
44 *
45 * The simplest purge method is delete().
46 *
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).
56 *
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.
59 *
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.
64 *
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
72 *
73 * @ingroup Cache
74 * @since 1.26
75 */
76 class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
77 /** @var BagOStuff The local datacenter cache */
78 protected $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 */
86 protected $logger;
87
88 /** @var int ERR_* constant for the "last error" registry */
89 protected $lastRelayError = self::ERR_NONE;
90
91 /** @var integer Callback stack depth for getWithSetCallback() */
92 private $callbackDepth = 0;
93 /** @var mixed[] Temporary warm-up cache */
94 private $warmupCache = [];
95
96 /** @var callable Callback used in generating default options in getWithSetCallback() */
97 private $sowSetOptsCallback;
98 /** @var callable Callback used in generating default options in getWithSetCallback() */
99 private $reapSetOptsCallback;
100
101 /** Max time expected to pass between delete() and DB commit finishing */
102 const MAX_COMMIT_DELAY = 3;
103 /** Max replication+snapshot lag before applying TTL_LAGGED or disallowing set() */
104 const MAX_READ_LAG = 7;
105 /** Seconds to tombstone keys on delete() */
106 const HOLDOFF_TTL = 11; // MAX_COMMIT_DELAY + MAX_READ_LAG + 1
107
108 /** Seconds to keep dependency purge keys around */
109 const CHECK_KEY_TTL = self::TTL_YEAR;
110 /** Seconds to keep lock keys around */
111 const LOCK_TTL = 10;
112 /** Default remaining TTL at which to consider pre-emptive regeneration */
113 const LOW_TTL = 30;
114 /** Default time-since-expiry on a miss that makes a key "hot" */
115 const LOCK_TSE = 1;
116
117 /** Never consider performing "popularity" refreshes until a key reaches this age */
118 const AGE_NEW = 60;
119 /** The time length of the "popularity" refresh window for hot keys */
120 const HOT_TTR = 900;
121 /** Hits/second for a refresh to be expected within the "popularity" window */
122 const HIT_RATE_HIGH = 1;
123 /** Seconds to ramp up to the "popularity" refresh chance after a key is no longer new */
124 const RAMPUP_TTL = 30;
125
126 /** Idiom for getWithSetCallback() callbacks to avoid calling set() */
127 const TTL_UNCACHEABLE = -1;
128 /** Idiom for getWithSetCallback() callbacks to 'lockTSE' logic */
129 const TSE_NONE = -1;
130 /** Max TTL to store keys when a data sourced is lagged */
131 const TTL_LAGGED = 30;
132 /** Idiom for delete() for "no hold-off" */
133 const HOLDOFF_NONE = 0;
134 /** Idiom for getWithSetCallback() for "no minimum required as-of timestamp" */
135 const MIN_TIMESTAMP_NONE = 0.0;
136
137 /** Tiny negative float to use when CTL comes up >= 0 due to clock skew */
138 const TINY_NEGATIVE = -0.000001;
139
140 /** Cache format version number */
141 const VERSION = 1;
142
143 const FLD_VERSION = 0; // key to cache version number
144 const FLD_VALUE = 1; // key to the cached value
145 const FLD_TTL = 2; // key to the original TTL
146 const FLD_TIME = 3; // key to the cache time
147 const FLD_FLAGS = 4; // key to the flags bitfield
148 const FLD_HOLDOFF = 5; // key to any hold-off TTL
149
150 /** @var integer Treat this value as expired-on-arrival */
151 const FLG_STALE = 1;
152
153 const ERR_NONE = 0; // no error
154 const ERR_NO_RESPONSE = 1; // no response
155 const ERR_UNREACHABLE = 2; // can't connect
156 const ERR_UNEXPECTED = 3; // response gave some error
157 const ERR_RELAY = 4; // relay broadcast failed
158
159 const VALUE_KEY_PREFIX = 'WANCache:v:';
160 const INTERIM_KEY_PREFIX = 'WANCache:i:';
161 const TIME_KEY_PREFIX = 'WANCache:t:';
162 const MUTEX_KEY_PREFIX = 'WANCache:m:';
163
164 const PURGE_VAL_PREFIX = 'PURGED:';
165
166 const VFLD_DATA = 'WOC:d'; // key to the value of versioned data
167 const VFLD_VERSION = 'WOC:v'; // key to the version of the value present
168
169 const PC_PRIMARY = 'primary:1000'; // process cache name and max key count
170
171 const DEFAULT_PURGE_CHANNEL = 'wancache-purge';
172
173 /**
174 * @param array $params
175 * - cache : BagOStuff object for a persistent cache
176 * - channels : Map of (action => channel string). Actions include "purge".
177 * - relayers : Map of (action => EventRelayer object). Actions include "purge".
178 * - logger : LoggerInterface object
179 */
180 public function __construct( array $params ) {
181 $this->cache = $params['cache'];
182 $this->purgeChannel = isset( $params['channels']['purge'] )
183 ? $params['channels']['purge']
184 : self::DEFAULT_PURGE_CHANNEL;
185 $this->purgeRelayer = isset( $params['relayers']['purge'] )
186 ? $params['relayers']['purge']
187 : new EventRelayerNull( [] );
188 $this->setLogger( isset( $params['logger'] ) ? $params['logger'] : new NullLogger() );
189 $this->sowSetOptsCallback = function () {
190 return null; // no-op
191 };
192 $this->reapSetOptsCallback = function () {
193 return []; // no-op
194 };
195 }
196
197 public function setLogger( LoggerInterface $logger ) {
198 $this->logger = $logger;
199 }
200
201 /**
202 * Get an instance that wraps EmptyBagOStuff
203 *
204 * @return WANObjectCache
205 */
206 public static function newEmpty() {
207 return new self( [
208 'cache' => new EmptyBagOStuff(),
209 'pool' => 'empty',
210 'relayer' => new EventRelayerNull( [] )
211 ] );
212 }
213
214 /**
215 * Fetch the value of a key from cache
216 *
217 * If supplied, $curTTL is set to the remaining TTL (current time left):
218 * - a) INF; if $key exists, has no TTL, and is not expired by $checkKeys
219 * - b) float (>=0); if $key exists, has a TTL, and is not expired by $checkKeys
220 * - c) float (<0); if $key is tombstoned, stale, or existing but expired by $checkKeys
221 * - d) null; if $key does not exist and is not tombstoned
222 *
223 * If a key is tombstoned, $curTTL will reflect the time since delete().
224 *
225 * The timestamp of $key will be checked against the last-purge timestamp
226 * of each of $checkKeys. Those $checkKeys not in cache will have the last-purge
227 * initialized to the current timestamp. If any of $checkKeys have a timestamp
228 * greater than that of $key, then $curTTL will reflect how long ago $key
229 * became invalid. Callers can use $curTTL to know when the value is stale.
230 * The $checkKeys parameter allow mass invalidations by updating a single key:
231 * - a) Each "check" key represents "last purged" of some source data
232 * - b) Callers pass in relevant "check" keys as $checkKeys in get()
233 * - c) When the source data that "check" keys represent changes,
234 * the touchCheckKey() method is called on them
235 *
236 * Source data entities might exists in a DB that uses snapshot isolation
237 * (e.g. the default REPEATABLE-READ in innoDB). Even for mutable data, that
238 * isolation can largely be maintained by doing the following:
239 * - a) Calling delete() on entity change *and* creation, before DB commit
240 * - b) Keeping transaction duration shorter than delete() hold-off TTL
241 *
242 * However, pre-snapshot values might still be seen if an update was made
243 * in a remote datacenter but the purge from delete() didn't relay yet.
244 *
245 * Consider using getWithSetCallback() instead of get() and set() cycles.
246 * That method has cache slam avoiding features for hot/expensive keys.
247 *
248 * @param string $key Cache key
249 * @param mixed $curTTL Approximate TTL left on the key if present/tombstoned [returned]
250 * @param array $checkKeys List of "check" keys
251 * @param float &$asOf UNIX timestamp of cached value; null on failure [returned]
252 * @return mixed Value of cache key or false on failure
253 */
254 final public function get( $key, &$curTTL = null, array $checkKeys = [], &$asOf = null ) {
255 $curTTLs = [];
256 $asOfs = [];
257 $values = $this->getMulti( [ $key ], $curTTLs, $checkKeys, $asOfs );
258 $curTTL = isset( $curTTLs[$key] ) ? $curTTLs[$key] : null;
259 $asOf = isset( $asOfs[$key] ) ? $asOfs[$key] : null;
260
261 return isset( $values[$key] ) ? $values[$key] : false;
262 }
263
264 /**
265 * Fetch the value of several keys from cache
266 *
267 * @see WANObjectCache::get()
268 *
269 * @param array $keys List of cache keys
270 * @param array $curTTLs Map of (key => approximate TTL left) for existing keys [returned]
271 * @param array $checkKeys List of check keys to apply to all $keys. May also apply "check"
272 * keys to specific cache keys only by using cache keys as keys in the $checkKeys array.
273 * @param float[] &$asOfs Map of (key => UNIX timestamp of cached value; null on failure)
274 * @return array Map of (key => value) for keys that exist
275 */
276 final public function getMulti(
277 array $keys, &$curTTLs = [], array $checkKeys = [], array &$asOfs = []
278 ) {
279 $result = [];
280 $curTTLs = [];
281 $asOfs = [];
282
283 $vPrefixLen = strlen( self::VALUE_KEY_PREFIX );
284 $valueKeys = self::prefixCacheKeys( $keys, self::VALUE_KEY_PREFIX );
285
286 $checkKeysForAll = [];
287 $checkKeysByKey = [];
288 $checkKeysFlat = [];
289 foreach ( $checkKeys as $i => $keys ) {
290 $prefixed = self::prefixCacheKeys( (array)$keys, self::TIME_KEY_PREFIX );
291 $checkKeysFlat = array_merge( $checkKeysFlat, $prefixed );
292 // Is this check keys for a specific cache key, or for all keys being fetched?
293 if ( is_int( $i ) ) {
294 $checkKeysForAll = array_merge( $checkKeysForAll, $prefixed );
295 } else {
296 $checkKeysByKey[$i] = isset( $checkKeysByKey[$i] )
297 ? array_merge( $checkKeysByKey[$i], $prefixed )
298 : $prefixed;
299 }
300 }
301
302 // Fetch all of the raw values
303 $keysGet = array_merge( $valueKeys, $checkKeysFlat );
304 if ( $this->warmupCache ) {
305 $wrappedValues = array_intersect_key( $this->warmupCache, array_flip( $keysGet ) );
306 $keysGet = array_diff( $keysGet, array_keys( $wrappedValues ) ); // keys left to fetch
307 } else {
308 $wrappedValues = [];
309 }
310 $wrappedValues += $this->cache->getMulti( $keysGet );
311 // Time used to compare/init "check" keys (derived after getMulti() to be pessimistic)
312 $now = microtime( true );
313
314 // Collect timestamps from all "check" keys
315 $purgeValuesForAll = $this->processCheckKeys( $checkKeysForAll, $wrappedValues, $now );
316 $purgeValuesByKey = [];
317 foreach ( $checkKeysByKey as $cacheKey => $checks ) {
318 $purgeValuesByKey[$cacheKey] =
319 $this->processCheckKeys( $checks, $wrappedValues, $now );
320 }
321
322 // Get the main cache value for each key and validate them
323 foreach ( $valueKeys as $vKey ) {
324 if ( !isset( $wrappedValues[$vKey] ) ) {
325 continue; // not found
326 }
327
328 $key = substr( $vKey, $vPrefixLen ); // unprefix
329
330 list( $value, $curTTL ) = $this->unwrap( $wrappedValues[$vKey], $now );
331 if ( $value !== false ) {
332 $result[$key] = $value;
333
334 // Force dependant keys to be invalid for a while after purging
335 // to reduce race conditions involving stale data getting cached
336 $purgeValues = $purgeValuesForAll;
337 if ( isset( $purgeValuesByKey[$key] ) ) {
338 $purgeValues = array_merge( $purgeValues, $purgeValuesByKey[$key] );
339 }
340 foreach ( $purgeValues as $purge ) {
341 $safeTimestamp = $purge[self::FLD_TIME] + $purge[self::FLD_HOLDOFF];
342 if ( $safeTimestamp >= $wrappedValues[$vKey][self::FLD_TIME] ) {
343 // How long ago this value was expired by *this* check key
344 $ago = min( $purge[self::FLD_TIME] - $now, self::TINY_NEGATIVE );
345 // How long ago this value was expired by *any* known check key
346 $curTTL = min( $curTTL, $ago );
347 }
348 }
349 }
350 $curTTLs[$key] = $curTTL;
351 $asOfs[$key] = ( $value !== false ) ? $wrappedValues[$vKey][self::FLD_TIME] : null;
352 }
353
354 return $result;
355 }
356
357 /**
358 * @since 1.27
359 * @param array $timeKeys List of prefixed time check keys
360 * @param array $wrappedValues
361 * @param float $now
362 * @return array List of purge value arrays
363 */
364 private function processCheckKeys( array $timeKeys, array $wrappedValues, $now ) {
365 $purgeValues = [];
366 foreach ( $timeKeys as $timeKey ) {
367 $purge = isset( $wrappedValues[$timeKey] )
368 ? self::parsePurgeValue( $wrappedValues[$timeKey] )
369 : false;
370 if ( $purge === false ) {
371 // Key is not set or invalid; regenerate
372 $newVal = $this->makePurgeValue( $now, self::HOLDOFF_TTL );
373 $this->cache->add( $timeKey, $newVal, self::CHECK_KEY_TTL );
374 $purge = self::parsePurgeValue( $newVal );
375 }
376 $purgeValues[] = $purge;
377 }
378 return $purgeValues;
379 }
380
381 /**
382 * Set the value of a key in cache
383 *
384 * Simply calling this method when source data changes is not valid because
385 * the changes do not replicate to the other WAN sites. In that case, delete()
386 * should be used instead. This method is intended for use on cache misses.
387 *
388 * If the data was read from a snapshot-isolated transactions (e.g. the default
389 * REPEATABLE-READ in innoDB), use 'since' to avoid the following race condition:
390 * - a) T1 starts
391 * - b) T2 updates a row, calls delete(), and commits
392 * - c) The HOLDOFF_TTL passes, expiring the delete() tombstone
393 * - d) T1 reads the row and calls set() due to a cache miss
394 * - e) Stale value is stuck in cache
395 *
396 * Setting 'lag' and 'since' help avoids keys getting stuck in stale states.
397 *
398 * Example usage:
399 * @code
400 * $dbr = wfGetDB( DB_REPLICA );
401 * $setOpts = Database::getCacheSetOptions( $dbr );
402 * // Fetch the row from the DB
403 * $row = $dbr->selectRow( ... );
404 * $key = $cache->makeKey( 'building', $buildingId );
405 * $cache->set( $key, $row, $cache::TTL_DAY, $setOpts );
406 * @endcode
407 *
408 * @param string $key Cache key
409 * @param mixed $value
410 * @param integer $ttl Seconds to live. Special values are:
411 * - WANObjectCache::TTL_INDEFINITE: Cache forever
412 * @param array $opts Options map:
413 * - lag : Seconds of replica DB lag. Typically, this is either the replica DB lag
414 * before the data was read or, if applicable, the replica DB lag before
415 * the snapshot-isolated transaction the data was read from started.
416 * Use false to indicate that replication is not running.
417 * Default: 0 seconds
418 * - since : UNIX timestamp of the data in $value. Typically, this is either
419 * the current time the data was read or (if applicable) the time when
420 * the snapshot-isolated transaction the data was read from started.
421 * Default: 0 seconds
422 * - pending : Whether this data is possibly from an uncommitted write transaction.
423 * Generally, other threads should not see values from the future and
424 * they certainly should not see ones that ended up getting rolled back.
425 * Default: false
426 * - lockTSE : if excessive replication/snapshot lag is detected, then store the value
427 * with this TTL and flag it as stale. This is only useful if the reads for
428 * this key use getWithSetCallback() with "lockTSE" set.
429 * Default: WANObjectCache::TSE_NONE
430 * - staleTTL : Seconds to keep the key around if it is stale. The get()/getMulti()
431 * methods return such stale values with a $curTTL of 0, and getWithSetCallback()
432 * will call the regeneration callback in such cases, passing in the old value
433 * and its as-of time to the callback. This is useful if adaptiveTTL() is used
434 * on the old value's as-of time when it is verified as still being correct.
435 * Default: 0.
436 * @note Options added in 1.28: staleTTL
437 * @return bool Success
438 */
439 final public function set( $key, $value, $ttl = 0, array $opts = [] ) {
440 $now = microtime( true );
441 $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
442 $age = isset( $opts['since'] ) ? max( 0, $now - $opts['since'] ) : 0;
443 $lag = isset( $opts['lag'] ) ? $opts['lag'] : 0;
444 $staleTTL = isset( $opts['staleTTL'] ) ? $opts['staleTTL'] : 0;
445
446 // Do not cache potentially uncommitted data as it might get rolled back
447 if ( !empty( $opts['pending'] ) ) {
448 $this->logger->info( "Rejected set() for $key due to pending writes." );
449
450 return true; // no-op the write for being unsafe
451 }
452
453 $wrapExtra = []; // additional wrapped value fields
454 // Check if there's a risk of writing stale data after the purge tombstone expired
455 if ( $lag === false || ( $lag + $age ) > self::MAX_READ_LAG ) {
456 // Case A: read lag with "lockTSE"; save but record value as stale
457 if ( $lockTSE >= 0 ) {
458 $ttl = max( 1, (int)$lockTSE ); // set() expects seconds
459 $wrapExtra[self::FLD_FLAGS] = self::FLG_STALE; // mark as stale
460 // Case B: any long-running transaction; ignore this set()
461 } elseif ( $age > self::MAX_READ_LAG ) {
462 $this->logger->warning( "Rejected set() for $key due to snapshot lag." );
463
464 return true; // no-op the write for being unsafe
465 // Case C: high replication lag; lower TTL instead of ignoring all set()s
466 } elseif ( $lag === false || $lag > self::MAX_READ_LAG ) {
467 $ttl = $ttl ? min( $ttl, self::TTL_LAGGED ) : self::TTL_LAGGED;
468 $this->logger->warning( "Lowered set() TTL for $key due to replication lag." );
469 // Case D: medium length request with medium replication lag; ignore this set()
470 } else {
471 $this->logger->warning( "Rejected set() for $key due to high read lag." );
472
473 return true; // no-op the write for being unsafe
474 }
475 }
476
477 // Wrap that value with time/TTL/version metadata
478 $wrapped = $this->wrap( $value, $ttl, $now ) + $wrapExtra;
479
480 $func = function ( $cache, $key, $cWrapped ) use ( $wrapped ) {
481 return ( is_string( $cWrapped ) )
482 ? false // key is tombstoned; do nothing
483 : $wrapped;
484 };
485
486 return $this->cache->merge( self::VALUE_KEY_PREFIX . $key, $func, $ttl + $staleTTL, 1 );
487 }
488
489 /**
490 * Purge a key from all datacenters
491 *
492 * This should only be called when the underlying data (being cached)
493 * changes in a significant way. This deletes the key and starts a hold-off
494 * period where the key cannot be written to for a few seconds (HOLDOFF_TTL).
495 * This is done to avoid the following race condition:
496 * - a) Some DB data changes and delete() is called on a corresponding key
497 * - b) A request refills the key with a stale value from a lagged DB
498 * - c) The stale value is stuck there until the key is expired/evicted
499 *
500 * This is implemented by storing a special "tombstone" value at the cache
501 * key that this class recognizes; get() calls will return false for the key
502 * and any set() calls will refuse to replace tombstone values at the key.
503 * For this to always avoid stale value writes, the following must hold:
504 * - a) Replication lag is bounded to being less than HOLDOFF_TTL; or
505 * - b) If lag is higher, the DB will have gone into read-only mode already
506 *
507 * Note that set() can also be lag-aware and lower the TTL if it's high.
508 *
509 * When using potentially long-running ACID transactions, a good pattern is
510 * to use a pre-commit hook to issue the delete. This means that immediately
511 * after commit, callers will see the tombstone in cache upon purge relay.
512 * It also avoids the following race condition:
513 * - a) T1 begins, changes a row, and calls delete()
514 * - b) The HOLDOFF_TTL passes, expiring the delete() tombstone
515 * - c) T2 starts, reads the row and calls set() due to a cache miss
516 * - d) T1 finally commits
517 * - e) Stale value is stuck in cache
518 *
519 * Example usage:
520 * @code
521 * $dbw->startAtomic( __METHOD__ ); // start of request
522 * ... <execute some stuff> ...
523 * // Update the row in the DB
524 * $dbw->update( ... );
525 * $key = $cache->makeKey( 'homes', $homeId );
526 * // Purge the corresponding cache entry just before committing
527 * $dbw->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) {
528 * $cache->delete( $key );
529 * } );
530 * ... <execute some stuff> ...
531 * $dbw->endAtomic( __METHOD__ ); // end of request
532 * @endcode
533 *
534 * The $ttl parameter can be used when purging values that have not actually changed
535 * recently. For example, a cleanup script to purge cache entries does not really need
536 * a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge.
537 * Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback().
538 *
539 * If called twice on the same key, then the last hold-off TTL takes precedence. For
540 * idempotence, the $ttl should not vary for different delete() calls on the same key.
541 *
542 * @param string $key Cache key
543 * @param integer $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL
544 * @return bool True if the item was purged or not found, false on failure
545 */
546 final public function delete( $key, $ttl = self::HOLDOFF_TTL ) {
547 $key = self::VALUE_KEY_PREFIX . $key;
548
549 if ( $ttl <= 0 ) {
550 // Publish the purge to all datacenters
551 $ok = $this->relayDelete( $key );
552 } else {
553 // Publish the purge to all datacenters
554 $ok = $this->relayPurge( $key, $ttl, self::HOLDOFF_NONE );
555 }
556
557 return $ok;
558 }
559
560 /**
561 * Fetch the value of a timestamp "check" key
562 *
563 * The key will be *initialized* to the current time if not set,
564 * so only call this method if this behavior is actually desired
565 *
566 * The timestamp can be used to check whether a cached value is valid.
567 * Callers should not assume that this returns the same timestamp in
568 * all datacenters due to relay delays.
569 *
570 * The level of staleness can roughly be estimated from this key, but
571 * if the key was evicted from cache, such calculations may show the
572 * time since expiry as ~0 seconds.
573 *
574 * Note that "check" keys won't collide with other regular keys.
575 *
576 * @param string $key
577 * @return float UNIX timestamp of the check key
578 */
579 final public function getCheckKeyTime( $key ) {
580 $key = self::TIME_KEY_PREFIX . $key;
581
582 $purge = self::parsePurgeValue( $this->cache->get( $key ) );
583 if ( $purge !== false ) {
584 $time = $purge[self::FLD_TIME];
585 } else {
586 // Casting assures identical floats for the next getCheckKeyTime() calls
587 $now = (string)microtime( true );
588 $this->cache->add( $key,
589 $this->makePurgeValue( $now, self::HOLDOFF_TTL ),
590 self::CHECK_KEY_TTL
591 );
592 $time = (float)$now;
593 }
594
595 return $time;
596 }
597
598 /**
599 * Purge a "check" key from all datacenters, invalidating keys that use it
600 *
601 * This should only be called when the underlying data (being cached)
602 * changes in a significant way, and it is impractical to call delete()
603 * on all keys that should be changed. When get() is called on those
604 * keys, the relevant "check" keys must be supplied for this to work.
605 *
606 * The "check" key essentially represents a last-modified field.
607 * When touched, the field will be updated on all cache servers.
608 * Keys using it via get(), getMulti(), or getWithSetCallback() will
609 * be invalidated. It is treated as being HOLDOFF_TTL seconds in the future
610 * by those methods to avoid race conditions where dependent keys get updated
611 * with stale values (e.g. from a DB replica DB).
612 *
613 * This is typically useful for keys with hardcoded names or in some cases
614 * dynamically generated names where a low number of combinations exist.
615 * When a few important keys get a large number of hits, a high cache
616 * time is usually desired as well as "lockTSE" logic. The resetCheckKey()
617 * method is less appropriate in such cases since the "time since expiry"
618 * cannot be inferred, causing any get() after the reset to treat the key
619 * as being "hot", resulting in more stale value usage.
620 *
621 * Note that "check" keys won't collide with other regular keys.
622 *
623 * @see WANObjectCache::get()
624 * @see WANObjectCache::getWithSetCallback()
625 * @see WANObjectCache::resetCheckKey()
626 *
627 * @param string $key Cache key
628 * @param int $holdoff HOLDOFF_TTL or HOLDOFF_NONE constant
629 * @return bool True if the item was purged or not found, false on failure
630 */
631 final public function touchCheckKey( $key, $holdoff = self::HOLDOFF_TTL ) {
632 // Publish the purge to all datacenters
633 return $this->relayPurge( self::TIME_KEY_PREFIX . $key, self::CHECK_KEY_TTL, $holdoff );
634 }
635
636 /**
637 * Delete a "check" key from all datacenters, invalidating keys that use it
638 *
639 * This is similar to touchCheckKey() in that keys using it via get(), getMulti(),
640 * or getWithSetCallback() will be invalidated. The differences are:
641 * - a) The "check" key will be deleted from all caches and lazily
642 * re-initialized when accessed (rather than set everywhere)
643 * - b) Thus, dependent keys will be known to be invalid, but not
644 * for how long (they are treated as "just" purged), which
645 * effects any lockTSE logic in getWithSetCallback()
646 * - c) Since "check" keys are initialized only on the server the key hashes
647 * to, any temporary ejection of that server will cause the value to be
648 * seen as purged as a new server will initialize the "check" key.
649 *
650 * The advantage is that this does not place high TTL keys on every cache
651 * server, making it better for code that will cache many different keys
652 * and either does not use lockTSE or uses a low enough TTL anyway.
653 *
654 * This is typically useful for keys with dynamically generated names
655 * where a high number of combinations exist.
656 *
657 * Note that "check" keys won't collide with other regular keys.
658 *
659 * @see WANObjectCache::get()
660 * @see WANObjectCache::getWithSetCallback()
661 * @see WANObjectCache::touchCheckKey()
662 *
663 * @param string $key Cache key
664 * @return bool True if the item was purged or not found, false on failure
665 */
666 final public function resetCheckKey( $key ) {
667 // Publish the purge to all datacenters
668 return $this->relayDelete( self::TIME_KEY_PREFIX . $key );
669 }
670
671 /**
672 * Method to fetch/regenerate cache keys
673 *
674 * On cache miss, the key will be set to the callback result via set()
675 * (unless the callback returns false) and that result will be returned.
676 * The arguments supplied to the callback are:
677 * - $oldValue : current cache value or false if not present
678 * - &$ttl : a reference to the TTL which can be altered
679 * - &$setOpts : a reference to options for set() which can be altered
680 * - $oldAsOf : generation UNIX timestamp of $oldValue or null if not present (since 1.28)
681 *
682 * It is strongly recommended to set the 'lag' and 'since' fields to avoid race conditions
683 * that can cause stale values to get stuck at keys. Usually, callbacks ignore the current
684 * value, but it can be used to maintain "most recent X" values that come from time or
685 * sequence based source data, provided that the "as of" id/time is tracked. Note that
686 * preemptive regeneration and $checkKeys can result in a non-false current value.
687 *
688 * Usage of $checkKeys is similar to get() and getMulti(). However, rather than the caller
689 * having to inspect a "current time left" variable (e.g. $curTTL, $curTTLs), a cache
690 * regeneration will automatically be triggered using the callback.
691 *
692 * The simplest way to avoid stampedes for hot keys is to use
693 * the 'lockTSE' option in $opts. If cache purges are needed, also:
694 * - a) Pass $key into $checkKeys
695 * - b) Use touchCheckKey( $key ) instead of delete( $key )
696 *
697 * Example usage (typical key):
698 * @code
699 * $catInfo = $cache->getWithSetCallback(
700 * // Key to store the cached value under
701 * $cache->makeKey( 'cat-attributes', $catId ),
702 * // Time-to-live (in seconds)
703 * $cache::TTL_MINUTE,
704 * // Function that derives the new key value
705 * function ( $oldValue, &$ttl, array &$setOpts ) {
706 * $dbr = wfGetDB( DB_REPLICA );
707 * // Account for any snapshot/replica DB lag
708 * $setOpts += Database::getCacheSetOptions( $dbr );
709 *
710 * return $dbr->selectRow( ... );
711 * }
712 * );
713 * @endcode
714 *
715 * Example usage (key that is expensive and hot):
716 * @code
717 * $catConfig = $cache->getWithSetCallback(
718 * // Key to store the cached value under
719 * $cache->makeKey( 'site-cat-config' ),
720 * // Time-to-live (in seconds)
721 * $cache::TTL_DAY,
722 * // Function that derives the new key value
723 * function ( $oldValue, &$ttl, array &$setOpts ) {
724 * $dbr = wfGetDB( DB_REPLICA );
725 * // Account for any snapshot/replica DB lag
726 * $setOpts += Database::getCacheSetOptions( $dbr );
727 *
728 * return CatConfig::newFromRow( $dbr->selectRow( ... ) );
729 * },
730 * [
731 * // Calling touchCheckKey() on this key invalidates the cache
732 * 'checkKeys' => [ $cache->makeKey( 'site-cat-config' ) ],
733 * // Try to only let one datacenter thread manage cache updates at a time
734 * 'lockTSE' => 30,
735 * // Avoid querying cache servers multiple times in a web request
736 * 'pcTTL' => $cache::TTL_PROC_LONG
737 * ]
738 * );
739 * @endcode
740 *
741 * Example usage (key with dynamic dependencies):
742 * @code
743 * $catState = $cache->getWithSetCallback(
744 * // Key to store the cached value under
745 * $cache->makeKey( 'cat-state', $cat->getId() ),
746 * // Time-to-live (seconds)
747 * $cache::TTL_HOUR,
748 * // Function that derives the new key value
749 * function ( $oldValue, &$ttl, array &$setOpts ) {
750 * // Determine new value from the DB
751 * $dbr = wfGetDB( DB_REPLICA );
752 * // Account for any snapshot/replica DB lag
753 * $setOpts += Database::getCacheSetOptions( $dbr );
754 *
755 * return CatState::newFromResults( $dbr->select( ... ) );
756 * },
757 * [
758 * // The "check" keys that represent things the value depends on;
759 * // Calling touchCheckKey() on any of them invalidates the cache
760 * 'checkKeys' => [
761 * $cache->makeKey( 'sustenance-bowls', $cat->getRoomId() ),
762 * $cache->makeKey( 'people-present', $cat->getHouseId() ),
763 * $cache->makeKey( 'cat-laws', $cat->getCityId() ),
764 * ]
765 * ]
766 * );
767 * @endcode
768 *
769 * Example usage (hot key holding most recent 100 events):
770 * @code
771 * $lastCatActions = $cache->getWithSetCallback(
772 * // Key to store the cached value under
773 * $cache->makeKey( 'cat-last-actions', 100 ),
774 * // Time-to-live (in seconds)
775 * 10,
776 * // Function that derives the new key value
777 * function ( $oldValue, &$ttl, array &$setOpts ) {
778 * $dbr = wfGetDB( DB_REPLICA );
779 * // Account for any snapshot/replica DB lag
780 * $setOpts += Database::getCacheSetOptions( $dbr );
781 *
782 * // Start off with the last cached list
783 * $list = $oldValue ?: [];
784 * // Fetch the last 100 relevant rows in descending order;
785 * // only fetch rows newer than $list[0] to reduce scanning
786 * $rows = iterator_to_array( $dbr->select( ... ) );
787 * // Merge them and get the new "last 100" rows
788 * return array_slice( array_merge( $new, $list ), 0, 100 );
789 * },
790 * [
791 * // Try to only let one datacenter thread manage cache updates at a time
792 * 'lockTSE' => 30,
793 * // Use a magic value when no cache value is ready rather than stampeding
794 * 'busyValue' => 'computing'
795 * ]
796 * );
797 * @endcode
798 *
799 * @see WANObjectCache::get()
800 * @see WANObjectCache::set()
801 *
802 * @param string $key Cache key
803 * @param integer $ttl Seconds to live for key updates. Special values are:
804 * - WANObjectCache::TTL_INDEFINITE: Cache forever
805 * - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
806 * @param callable $callback Value generation function
807 * @param array $opts Options map:
808 * - checkKeys: List of "check" keys. The key at $key will be seen as invalid when either
809 * touchCheckKey() or resetCheckKey() is called on any of these keys.
810 * Default: [].
811 * - lockTSE: If the key is tombstoned or expired (by checkKeys) less than this many seconds
812 * ago, then try to have a single thread handle cache regeneration at any given time.
813 * Other threads will try to use stale values if possible. If, on miss, the time since
814 * expiration is low, the assumption is that the key is hot and that a stampede is worth
815 * avoiding. Setting this above WANObjectCache::HOLDOFF_TTL makes no difference. The
816 * higher this is set, the higher the worst-case staleness can be.
817 * Use WANObjectCache::TSE_NONE to disable this logic.
818 * Default: WANObjectCache::TSE_NONE.
819 * - busyValue: If no value exists and another thread is currently regenerating it, use this
820 * as a fallback value (or a callback to generate such a value). This assures that cache
821 * stampedes cannot happen if the value falls out of cache. This can be used as insurance
822 * against cache regeneration becoming very slow for some reason (greater than the TTL).
823 * Default: null.
824 * - pcTTL: Process cache the value in this PHP instance for this many seconds. This avoids
825 * network I/O when a key is read several times. This will not cache when the callback
826 * returns false, however. Note that any purges will not be seen while process cached;
827 * since the callback should use replica DBs and they may be lagged or have snapshot
828 * isolation anyway, this should not typically matter.
829 * Default: WANObjectCache::TTL_UNCACHEABLE.
830 * - pcGroup: Process cache group to use instead of the primary one. If set, this must be
831 * of the format ALPHANUMERIC_NAME:MAX_KEY_SIZE, e.g. "mydata:10". Use this for storing
832 * large values, small yet numerous values, or some values with a high cost of eviction.
833 * It is generally preferable to use a class constant when setting this value.
834 * This has no effect unless pcTTL is used.
835 * Default: WANObjectCache::PC_PRIMARY.
836 * - version: Integer version number. This allows for callers to make breaking changes to
837 * how values are stored while maintaining compatability and correct cache purges. New
838 * versions are stored alongside older versions concurrently. Avoid storing class objects
839 * however, as this reduces compatibility (due to serialization).
840 * Default: null.
841 * - minAsOf: Reject values if they were generated before this UNIX timestamp.
842 * This is useful if the source of a key is suspected of having possibly changed
843 * recently, and the caller wants any such changes to be reflected.
844 * Default: WANObjectCache::MIN_TIMESTAMP_NONE.
845 * - hotTTR: Expected time-till-refresh for keys that average ~1 hit/second.
846 * This should be greater than "ageNew". Keys with higher hit rates will regenerate
847 * more often. This is useful when a popular key is changed but the cache purge was
848 * delayed or lost. Seldom used keys are rarely affected by this setting, unless an
849 * extremely low "hotTTR" value is passed in.
850 * Default: WANObjectCache::HOT_TTR.
851 * - lowTTL: Consider pre-emptive updates when the current TTL (seconds) of the key is less
852 * than this. It becomes more likely over time, becoming certain once the key is expired.
853 * Default: WANObjectCache::LOW_TTL.
854 * - ageNew: Consider popularity refreshes only once a key reaches this age in seconds.
855 * Default: WANObjectCache::AGE_NEW.
856 * @return mixed Value found or written to the key
857 * @note Options added in 1.28: version, busyValue, hotTTR, ageNew, pcGroup, minAsOf
858 * @note Callable type hints are not used to avoid class-autoloading
859 */
860 final public function getWithSetCallback( $key, $ttl, $callback, array $opts = [] ) {
861 $pcTTL = isset( $opts['pcTTL'] ) ? $opts['pcTTL'] : self::TTL_UNCACHEABLE;
862
863 // Try the process cache if enabled and the cache callback is not within a cache callback.
864 // Process cache use in nested callbacks is not lag-safe with regard to HOLDOFF_TTL since
865 // the in-memory value is further lagged than the shared one since it uses a blind TTL.
866 if ( $pcTTL >= 0 && $this->callbackDepth == 0 ) {
867 $group = isset( $opts['pcGroup'] ) ? $opts['pcGroup'] : self::PC_PRIMARY;
868 $procCache = $this->getProcessCache( $group );
869 $value = $procCache->get( $key );
870 } else {
871 $procCache = false;
872 $value = false;
873 }
874
875 if ( $value === false ) {
876 // Fetch the value over the network
877 if ( isset( $opts['version'] ) ) {
878 $version = $opts['version'];
879 $asOf = null;
880 $cur = $this->doGetWithSetCallback(
881 $key,
882 $ttl,
883 function ( $oldValue, &$ttl, &$setOpts, $oldAsOf )
884 use ( $callback, $version ) {
885 if ( is_array( $oldValue )
886 && array_key_exists( self::VFLD_DATA, $oldValue )
887 ) {
888 $oldData = $oldValue[self::VFLD_DATA];
889 } else {
890 // VFLD_DATA is not set if an old, unversioned, key is present
891 $oldData = false;
892 }
893
894 return [
895 self::VFLD_DATA => $callback( $oldData, $ttl, $setOpts, $oldAsOf ),
896 self::VFLD_VERSION => $version
897 ];
898 },
899 $opts,
900 $asOf
901 );
902 if ( $cur[self::VFLD_VERSION] === $version ) {
903 // Value created or existed before with version; use it
904 $value = $cur[self::VFLD_DATA];
905 } else {
906 // Value existed before with a different version; use variant key.
907 // Reflect purges to $key by requiring that this key value be newer.
908 $value = $this->doGetWithSetCallback(
909 'cache-variant:' . md5( $key ) . ":$version",
910 $ttl,
911 $callback,
912 // Regenerate value if not newer than $key
913 [ 'version' => null, 'minAsOf' => $asOf ] + $opts
914 );
915 }
916 } else {
917 $value = $this->doGetWithSetCallback( $key, $ttl, $callback, $opts );
918 }
919
920 // Update the process cache if enabled
921 if ( $procCache && $value !== false ) {
922 $procCache->set( $key, $value, $pcTTL );
923 }
924 }
925
926 return $value;
927 }
928
929 /**
930 * Do the actual I/O for getWithSetCallback() when needed
931 *
932 * @see WANObjectCache::getWithSetCallback()
933 *
934 * @param string $key
935 * @param integer $ttl
936 * @param callback $callback
937 * @param array $opts Options map for getWithSetCallback()
938 * @param float &$asOf Cache generation timestamp of returned value [returned]
939 * @return mixed
940 * @note Callable type hints are not used to avoid class-autoloading
941 */
942 protected function doGetWithSetCallback( $key, $ttl, $callback, array $opts, &$asOf = null ) {
943 $lowTTL = isset( $opts['lowTTL'] ) ? $opts['lowTTL'] : min( self::LOW_TTL, $ttl );
944 $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
945 $checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : [];
946 $busyValue = isset( $opts['busyValue'] ) ? $opts['busyValue'] : null;
947 $popWindow = isset( $opts['hotTTR'] ) ? $opts['hotTTR'] : self::HOT_TTR;
948 $ageNew = isset( $opts['ageNew'] ) ? $opts['ageNew'] : self::AGE_NEW;
949 $minTime = isset( $opts['minAsOf'] ) ? $opts['minAsOf'] : self::MIN_TIMESTAMP_NONE;
950 $versioned = isset( $opts['version'] );
951
952 // Get the current key value
953 $curTTL = null;
954 $cValue = $this->get( $key, $curTTL, $checkKeys, $asOf ); // current value
955 $value = $cValue; // return value
956
957 $preCallbackTime = microtime( true );
958 // Determine if a cached value regeneration is needed or desired
959 if ( $value !== false
960 && $curTTL > 0
961 && $this->isValid( $value, $versioned, $asOf, $minTime )
962 && !$this->worthRefreshExpiring( $curTTL, $lowTTL )
963 && !$this->worthRefreshPopular( $asOf, $ageNew, $popWindow, $preCallbackTime )
964 ) {
965 return $value;
966 }
967
968 // A deleted key with a negative TTL left must be tombstoned
969 $isTombstone = ( $curTTL !== null && $value === false );
970 // Assume a key is hot if requested soon after invalidation
971 $isHot = ( $curTTL !== null && $curTTL <= 0 && abs( $curTTL ) <= $lockTSE );
972 // Use the mutex if there is no value and a busy fallback is given
973 $checkBusy = ( $busyValue !== null && $value === false );
974 // Decide whether a single thread should handle regenerations.
975 // This avoids stampedes when $checkKeys are bumped and when preemptive
976 // renegerations take too long. It also reduces regenerations while $key
977 // is tombstoned. This balances cache freshness with avoiding DB load.
978 $useMutex = ( $isHot || ( $isTombstone && $lockTSE > 0 ) || $checkBusy );
979
980 $lockAcquired = false;
981 if ( $useMutex ) {
982 // Acquire a datacenter-local non-blocking lock
983 if ( $this->cache->add( self::MUTEX_KEY_PREFIX . $key, 1, self::LOCK_TTL ) ) {
984 // Lock acquired; this thread should update the key
985 $lockAcquired = true;
986 } elseif ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
987 // If it cannot be acquired; then the stale value can be used
988 return $value;
989 } else {
990 // Use the INTERIM value for tombstoned keys to reduce regeneration load.
991 // For hot keys, either another thread has the lock or the lock failed;
992 // use the INTERIM value from the last thread that regenerated it.
993 $wrapped = $this->cache->get( self::INTERIM_KEY_PREFIX . $key );
994 list( $value ) = $this->unwrap( $wrapped, microtime( true ) );
995 if ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
996 $asOf = $wrapped[self::FLD_TIME];
997
998 return $value;
999 }
1000 // Use the busy fallback value if nothing else
1001 if ( $busyValue !== null ) {
1002 return is_callable( $busyValue ) ? $busyValue() : $busyValue;
1003 }
1004 }
1005 }
1006
1007 if ( !is_callable( $callback ) ) {
1008 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
1009 }
1010
1011 // Generate the new value from the callback...
1012 $setOpts = [];
1013 ++$this->callbackDepth;
1014 try {
1015 $tag = call_user_func( $this->sowSetOptsCallback );
1016 $value = call_user_func_array( $callback, [ $cValue, &$ttl, &$setOpts, $asOf ] );
1017 $setOptDefaults = call_user_func( $this->reapSetOptsCallback, $tag );
1018 } finally {
1019 --$this->callbackDepth;
1020 }
1021 // When delete() is called, writes are write-holed by the tombstone,
1022 // so use a special INTERIM key to pass the new value around threads.
1023 if ( ( $isTombstone && $lockTSE > 0 ) && $value !== false && $ttl >= 0 ) {
1024 $tempTTL = max( 1, (int)$lockTSE ); // set() expects seconds
1025 $newAsOf = microtime( true );
1026 $wrapped = $this->wrap( $value, $tempTTL, $newAsOf );
1027 // Avoid using set() to avoid pointless mcrouter broadcasting
1028 $this->cache->merge(
1029 self::INTERIM_KEY_PREFIX . $key,
1030 function () use ( $wrapped ) {
1031 return $wrapped;
1032 },
1033 $tempTTL,
1034 1
1035 );
1036 }
1037
1038 if ( $value !== false && $ttl >= 0 ) {
1039 $setOpts['lockTSE'] = $lockTSE;
1040 // Use best known "since" timestamp if not provided
1041 $setOpts += [ 'since' => $preCallbackTime ];
1042 // Use default "lag" and "pending" values if not set
1043 $setOpts += $setOptDefaults;
1044 // Update the cache; this will fail if the key is tombstoned
1045 $this->set( $key, $value, $ttl, $setOpts );
1046 }
1047
1048 if ( $lockAcquired ) {
1049 // Avoid using delete() to avoid pointless mcrouter broadcasting
1050 $this->cache->changeTTL( self::MUTEX_KEY_PREFIX . $key, 1 );
1051 }
1052
1053 return $value;
1054 }
1055
1056 /**
1057 * Method to fetch/regenerate multiple cache keys at once
1058 *
1059 * This works the same as getWithSetCallback() except:
1060 * - a) The $keys argument expects the result of WANObjectCache::makeMultiKeys()
1061 * - b) The $callback argument expects a callback taking the following arguments:
1062 * - $id: ID of an entity to query
1063 * - $oldValue : the prior cache value or false if none was present
1064 * - &$ttl : a reference to the new value TTL in seconds
1065 * - &$setOpts : a reference to options for set() which can be altered
1066 * - $oldAsOf : generation UNIX timestamp of $oldValue or null if not present
1067 * Aside from the additional $id argument, the other arguments function the same
1068 * way they do in getWithSetCallback().
1069 * - c) The return value is a map of (cache key => value) in the order of $keyedIds
1070 *
1071 * @see WANObjectCache::getWithSetCallback()
1072 *
1073 * Example usage:
1074 * @code
1075 * $rows = $cache->getMultiWithSetCallback(
1076 * // Map of cache keys to entity IDs
1077 * $cache->makeMultiKeys(
1078 * $this->fileVersionIds(),
1079 * function ( $id, WANObjectCache $cache ) {
1080 * return $cache->makeKey( 'file-version', $id );
1081 * }
1082 * ),
1083 * // Time-to-live (in seconds)
1084 * $cache::TTL_DAY,
1085 * // Function that derives the new key value
1086 * return function ( $id, $oldValue, &$ttl, array &$setOpts ) {
1087 * $dbr = wfGetDB( DB_REPLICA );
1088 * // Account for any snapshot/replica DB lag
1089 * $setOpts += Database::getCacheSetOptions( $dbr );
1090 *
1091 * // Load the row for this file
1092 * $row = $dbr->selectRow( 'file', '*', [ 'id' => $id ], __METHOD__ );
1093 *
1094 * return $row ? (array)$row : false;
1095 * },
1096 * [
1097 * // Process cache for 30 seconds
1098 * 'pcTTL' => 30,
1099 * // Use a dedicated 500 item cache (initialized on-the-fly)
1100 * 'pcGroup' => 'file-versions:500'
1101 * ]
1102 * );
1103 * $files = array_map( [ __CLASS__, 'newFromRow' ], $rows );
1104 * @endcode
1105 *
1106 * @param ArrayIterator $keyedIds Result of WANObjectCache::makeMultiKeys()
1107 * @param integer $ttl Seconds to live for key updates
1108 * @param callable $callback Callback the yields entity regeneration callbacks
1109 * @param array $opts Options map
1110 * @return array Map of (cache key => value) in the same order as $keyedIds
1111 * @since 1.28
1112 */
1113 final public function getMultiWithSetCallback(
1114 ArrayIterator $keyedIds, $ttl, callable $callback, array $opts = []
1115 ) {
1116 $keysWarmUp = iterator_to_array( $keyedIds, true );
1117 $checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : [];
1118 foreach ( $checkKeys as $i => $checkKeyOrKeys ) {
1119 if ( is_int( $i ) ) {
1120 $keysWarmUp[] = $checkKeyOrKeys;
1121 } else {
1122 $keysWarmUp = array_merge( $keysWarmUp, $checkKeyOrKeys );
1123 }
1124 }
1125
1126 $this->warmupCache = $this->cache->getMulti( $keysWarmUp );
1127 $this->warmupCache += array_fill_keys( $keysWarmUp, false );
1128
1129 // Wrap $callback to match the getWithSetCallback() format while passing $id to $callback
1130 $id = null;
1131 $func = function ( $oldValue, &$ttl, array $setOpts, $oldAsOf ) use ( $callback, &$id ) {
1132 return $callback( $id, $oldValue, $ttl, $setOpts, $oldAsOf );
1133 };
1134
1135 $values = [];
1136 foreach ( $keyedIds as $key => $id ) {
1137 $values[$key] = $this->getWithSetCallback( $key, $ttl, $func, $opts );
1138 }
1139
1140 $this->warmupCache = [];
1141
1142 return $values;
1143 }
1144
1145 /**
1146 * @see BagOStuff::makeKey()
1147 * @param string ... Key component
1148 * @return string
1149 * @since 1.27
1150 */
1151 public function makeKey() {
1152 return call_user_func_array( [ $this->cache, __FUNCTION__ ], func_get_args() );
1153 }
1154
1155 /**
1156 * @see BagOStuff::makeGlobalKey()
1157 * @param string ... Key component
1158 * @return string
1159 * @since 1.27
1160 */
1161 public function makeGlobalKey() {
1162 return call_user_func_array( [ $this->cache, __FUNCTION__ ], func_get_args() );
1163 }
1164
1165 /**
1166 * @param array $entities List of entity IDs
1167 * @param callable $keyFunc Callback yielding a key from (entity ID, this WANObjectCache)
1168 * @return ArrayIterator Iterator yielding (cache key => entity ID) in $entities order
1169 * @since 1.28
1170 */
1171 public function makeMultiKeys( array $entities, callable $keyFunc ) {
1172 $map = [];
1173 foreach ( $entities as $entity ) {
1174 $map[$keyFunc( $entity, $this )] = $entity;
1175 }
1176
1177 return new ArrayIterator( $map );
1178 }
1179
1180 /**
1181 * Get the "last error" registered; clearLastError() should be called manually
1182 * @return int ERR_* class constant for the "last error" registry
1183 */
1184 final public function getLastError() {
1185 if ( $this->lastRelayError ) {
1186 // If the cache and the relayer failed, focus on the latter.
1187 // An update not making it to the relayer means it won't show up
1188 // in other DCs (nor will consistent re-hashing see up-to-date values).
1189 // On the other hand, if just the cache update failed, then it should
1190 // eventually be applied by the relayer.
1191 return $this->lastRelayError;
1192 }
1193
1194 $code = $this->cache->getLastError();
1195 switch ( $code ) {
1196 case BagOStuff::ERR_NONE:
1197 return self::ERR_NONE;
1198 case BagOStuff::ERR_NO_RESPONSE:
1199 return self::ERR_NO_RESPONSE;
1200 case BagOStuff::ERR_UNREACHABLE:
1201 return self::ERR_UNREACHABLE;
1202 default:
1203 return self::ERR_UNEXPECTED;
1204 }
1205 }
1206
1207 /**
1208 * Clear the "last error" registry
1209 */
1210 final public function clearLastError() {
1211 $this->cache->clearLastError();
1212 $this->lastRelayError = self::ERR_NONE;
1213 }
1214
1215 /**
1216 * Clear the in-process caches; useful for testing
1217 *
1218 * @since 1.27
1219 */
1220 public function clearProcessCache() {
1221 $this->processCaches = [];
1222 }
1223
1224 /**
1225 * @param integer $flag ATTR_* class constant
1226 * @return integer QOS_* class constant
1227 * @since 1.28
1228 */
1229 public function getQoS( $flag ) {
1230 return $this->cache->getQoS( $flag );
1231 }
1232
1233 /**
1234 * Get a TTL that is higher for objects that have not changed recently
1235 *
1236 * This is useful for keys that get explicit purges and DB or purge relay
1237 * lag is a potential concern (especially how it interacts with CDN cache)
1238 *
1239 * Example usage:
1240 * @code
1241 * // Last-modified time of page
1242 * $mtime = wfTimestamp( TS_UNIX, $page->getTimestamp() );
1243 * // Get adjusted TTL. If $mtime is 3600 seconds ago and $minTTL/$factor left at
1244 * // defaults, then $ttl is 3600 * .2 = 720. If $minTTL was greater than 720, then
1245 * // $ttl would be $minTTL. If $maxTTL was smaller than 720, $ttl would be $maxTTL.
1246 * $ttl = $cache->adaptiveTTL( $mtime, $cache::TTL_DAY );
1247 * @endcode
1248 *
1249 * @param integer|float $mtime UNIX timestamp
1250 * @param integer $maxTTL Maximum TTL (seconds)
1251 * @param integer $minTTL Minimum TTL (seconds); Default: 30
1252 * @param float $factor Value in the range (0,1); Default: .2
1253 * @return integer Adaptive TTL
1254 * @since 1.28
1255 */
1256 public function adaptiveTTL( $mtime, $maxTTL, $minTTL = 30, $factor = .2 ) {
1257 if ( is_float( $mtime ) || ctype_digit( $mtime ) ) {
1258 $mtime = (int)$mtime; // handle fractional seconds and string integers
1259 }
1260
1261 if ( !is_int( $mtime ) || $mtime <= 0 ) {
1262 return $minTTL; // no last-modified time provided
1263 }
1264
1265 $age = time() - $mtime;
1266
1267 return (int)min( $maxTTL, max( $minTTL, $factor * $age ) );
1268 }
1269
1270 /**
1271 * Set the callbacks that provide the fallback values for cache set options
1272 *
1273 * The $reap callback returns default values to use for the "lag", "since", and "pending"
1274 * options used by WANObjectCache::set(). It takes the ID from $sow as the sole parameter.
1275 * An empty array should be returned if there is no usage to base the return value on.
1276 *
1277 * @param callable $sow Function that starts recording and returns an ID
1278 * @param callable $reap Function that takes an ID, stops recording, and returns the options
1279 * @since 1.28
1280 */
1281 public function setDefaultCacheSetOptionCallbacks( callable $sow, callable $reap ) {
1282 $this->sowSetOptsCallback = $sow;
1283 $this->reapSetOptsCallback = $reap;
1284 }
1285
1286 /**
1287 * Do the actual async bus purge of a key
1288 *
1289 * This must set the key to "PURGED:<UNIX timestamp>:<holdoff>"
1290 *
1291 * @param string $key Cache key
1292 * @param integer $ttl How long to keep the tombstone [seconds]
1293 * @param integer $holdoff HOLDOFF_* constant controlling how long to ignore sets for this key
1294 * @return bool Success
1295 */
1296 protected function relayPurge( $key, $ttl, $holdoff ) {
1297 if ( $this->purgeRelayer instanceof EventRelayerNull ) {
1298 // This handles the mcrouter and the single-DC case
1299 $ok = $this->cache->set( $key,
1300 $this->makePurgeValue( microtime( true ), self::HOLDOFF_NONE ),
1301 $ttl
1302 );
1303 } else {
1304 $event = $this->cache->modifySimpleRelayEvent( [
1305 'cmd' => 'set',
1306 'key' => $key,
1307 'val' => 'PURGED:$UNIXTIME$:' . (int)$holdoff,
1308 'ttl' => max( $ttl, 1 ),
1309 'sbt' => true, // substitute $UNIXTIME$ with actual microtime
1310 ] );
1311
1312 $ok = $this->purgeRelayer->notify( $this->purgeChannel, $event );
1313 if ( !$ok ) {
1314 $this->lastRelayError = self::ERR_RELAY;
1315 }
1316 }
1317
1318 return $ok;
1319 }
1320
1321 /**
1322 * Do the actual async bus delete of a key
1323 *
1324 * @param string $key Cache key
1325 * @return bool Success
1326 */
1327 protected function relayDelete( $key ) {
1328 if ( $this->purgeRelayer instanceof EventRelayerNull ) {
1329 // This handles the mcrouter and the single-DC case
1330 $ok = $this->cache->delete( $key );
1331 } else {
1332 $event = $this->cache->modifySimpleRelayEvent( [
1333 'cmd' => 'delete',
1334 'key' => $key,
1335 ] );
1336
1337 $ok = $this->purgeRelayer->notify( $this->purgeChannel, $event );
1338 if ( !$ok ) {
1339 $this->lastRelayError = self::ERR_RELAY;
1340 }
1341 }
1342
1343 return $ok;
1344 }
1345
1346 /**
1347 * Check if a key should be regenerated (using random probability)
1348 *
1349 * This returns false if $curTTL >= $lowTTL. Otherwise, the chance
1350 * of returning true increases steadily from 0% to 100% as the $curTTL
1351 * moves from $lowTTL to 0 seconds. This handles widely varying
1352 * levels of cache access traffic.
1353 *
1354 * @param float $curTTL Approximate TTL left on the key if present
1355 * @param float $lowTTL Consider a refresh when $curTTL is less than this
1356 * @return bool
1357 */
1358 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1359 if ( $curTTL >= $lowTTL ) {
1360 return false;
1361 } elseif ( $curTTL <= 0 ) {
1362 return true;
1363 }
1364
1365 $chance = ( 1 - $curTTL / $lowTTL );
1366
1367 return mt_rand( 1, 1e9 ) <= 1e9 * $chance;
1368 }
1369
1370 /**
1371 * Check if a key is due for randomized regeneration due to its popularity
1372 *
1373 * This is used so that popular keys can preemptively refresh themselves for higher
1374 * consistency (especially in the case of purge loss/delay). Unpopular keys can remain
1375 * in cache with their high nominal TTL. This means popular keys keep good consistency,
1376 * whether the data changes frequently or not, and long-tail keys get to stay in cache
1377 * and get hits too. Similar to worthRefreshExpiring(), randomization is used.
1378 *
1379 * @param float $asOf UNIX timestamp of the value
1380 * @param integer $ageNew Age of key when this might recommend refreshing (seconds)
1381 * @param integer $timeTillRefresh Age of key when it should be refreshed if popular (seconds)
1382 * @param float $now The current UNIX timestamp
1383 * @return bool
1384 */
1385 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1386 $age = $now - $asOf;
1387 $timeOld = $age - $ageNew;
1388 if ( $timeOld <= 0 ) {
1389 return false;
1390 }
1391
1392 // Lifecycle is: new, ramp-up refresh chance, full refresh chance
1393 $refreshWindowSec = max( $timeTillRefresh - $ageNew - self::RAMPUP_TTL / 2, 1 );
1394 // P(refresh) * (# hits in $refreshWindowSec) = (expected # of refreshes)
1395 // P(refresh) * ($refreshWindowSec * $popularHitsPerSec) = 1
1396 // P(refresh) = 1/($refreshWindowSec * $popularHitsPerSec)
1397 $chance = 1 / ( self::HIT_RATE_HIGH * $refreshWindowSec );
1398
1399 // Ramp up $chance from 0 to its nominal value over RAMPUP_TTL seconds to avoid stampedes
1400 $chance *= ( $timeOld <= self::RAMPUP_TTL ) ? $timeOld / self::RAMPUP_TTL : 1;
1401
1402 return mt_rand( 1, 1e9 ) <= 1e9 * $chance;
1403 }
1404
1405 /**
1406 * Check whether $value is appropriately versioned and not older than $minTime (if set)
1407 *
1408 * @param array $value
1409 * @param bool $versioned
1410 * @param float $asOf The time $value was generated
1411 * @param float $minTime The last time the main value was generated (0.0 if unknown)
1412 * @return bool
1413 */
1414 protected function isValid( $value, $versioned, $asOf, $minTime ) {
1415 if ( $versioned && !isset( $value[self::VFLD_VERSION] ) ) {
1416 return false;
1417 } elseif ( $minTime > 0 && $asOf < $minTime ) {
1418 return false;
1419 }
1420
1421 return true;
1422 }
1423
1424 /**
1425 * Do not use this method outside WANObjectCache
1426 *
1427 * @param mixed $value
1428 * @param integer $ttl [0=forever]
1429 * @param float $now Unix Current timestamp just before calling set()
1430 * @return array
1431 */
1432 protected function wrap( $value, $ttl, $now ) {
1433 return [
1434 self::FLD_VERSION => self::VERSION,
1435 self::FLD_VALUE => $value,
1436 self::FLD_TTL => $ttl,
1437 self::FLD_TIME => $now
1438 ];
1439 }
1440
1441 /**
1442 * Do not use this method outside WANObjectCache
1443 *
1444 * @param array|string|bool $wrapped
1445 * @param float $now Unix Current timestamp (preferrably pre-query)
1446 * @return array (mixed; false if absent/invalid, current time left)
1447 */
1448 protected function unwrap( $wrapped, $now ) {
1449 // Check if the value is a tombstone
1450 $purge = self::parsePurgeValue( $wrapped );
1451 if ( $purge !== false ) {
1452 // Purged values should always have a negative current $ttl
1453 $curTTL = min( $purge[self::FLD_TIME] - $now, self::TINY_NEGATIVE );
1454 return [ false, $curTTL ];
1455 }
1456
1457 if ( !is_array( $wrapped ) // not found
1458 || !isset( $wrapped[self::FLD_VERSION] ) // wrong format
1459 || $wrapped[self::FLD_VERSION] !== self::VERSION // wrong version
1460 ) {
1461 return [ false, null ];
1462 }
1463
1464 $flags = isset( $wrapped[self::FLD_FLAGS] ) ? $wrapped[self::FLD_FLAGS] : 0;
1465 if ( ( $flags & self::FLG_STALE ) == self::FLG_STALE ) {
1466 // Treat as expired, with the cache time as the expiration
1467 $age = $now - $wrapped[self::FLD_TIME];
1468 $curTTL = min( -$age, self::TINY_NEGATIVE );
1469 } elseif ( $wrapped[self::FLD_TTL] > 0 ) {
1470 // Get the approximate time left on the key
1471 $age = $now - $wrapped[self::FLD_TIME];
1472 $curTTL = max( $wrapped[self::FLD_TTL] - $age, 0.0 );
1473 } else {
1474 // Key had no TTL, so the time left is unbounded
1475 $curTTL = INF;
1476 }
1477
1478 return [ $wrapped[self::FLD_VALUE], $curTTL ];
1479 }
1480
1481 /**
1482 * @param array $keys
1483 * @param string $prefix
1484 * @return string[]
1485 */
1486 protected static function prefixCacheKeys( array $keys, $prefix ) {
1487 $res = [];
1488 foreach ( $keys as $key ) {
1489 $res[] = $prefix . $key;
1490 }
1491
1492 return $res;
1493 }
1494
1495 /**
1496 * @param string $value Wrapped value like "PURGED:<timestamp>:<holdoff>"
1497 * @return array|bool Array containing a UNIX timestamp (float) and holdoff period (integer),
1498 * or false if value isn't a valid purge value
1499 */
1500 protected static function parsePurgeValue( $value ) {
1501 if ( !is_string( $value ) ) {
1502 return false;
1503 }
1504 $segments = explode( ':', $value, 3 );
1505 if ( !isset( $segments[0] ) || !isset( $segments[1] )
1506 || "{$segments[0]}:" !== self::PURGE_VAL_PREFIX
1507 ) {
1508 return false;
1509 }
1510 if ( !isset( $segments[2] ) ) {
1511 // Back-compat with old purge values without holdoff
1512 $segments[2] = self::HOLDOFF_TTL;
1513 }
1514 return [
1515 self::FLD_TIME => (float)$segments[1],
1516 self::FLD_HOLDOFF => (int)$segments[2],
1517 ];
1518 }
1519
1520 /**
1521 * @param float $timestamp
1522 * @param int $holdoff In seconds
1523 * @return string Wrapped purge value
1524 */
1525 protected function makePurgeValue( $timestamp, $holdoff ) {
1526 return self::PURGE_VAL_PREFIX . (float)$timestamp . ':' . (int)$holdoff;
1527 }
1528
1529 /**
1530 * @param string $group
1531 * @return HashBagOStuff
1532 */
1533 protected function getProcessCache( $group ) {
1534 if ( !isset( $this->processCaches[$group] ) ) {
1535 list( , $n ) = explode( ':', $group );
1536 $this->processCaches[$group] = new HashBagOStuff( [ 'maxKeys' => (int)$n ] );
1537 }
1538
1539 return $this->processCaches[$group];
1540 }
1541 }