Sync up with Parsoid parserTests.
[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 * When querying the store on cache miss, the closest DB replica
37 * should be used. Try to avoid heavyweight DB master or quorum reads.
38 * When the source data changes, a purge method should be called.
39 * Since purges are expensive, they should be avoided. One can do so if:
40 * - a) The object cached is immutable; or
41 * - b) Validity is checked against the source after get(); or
42 * - c) Using a modest TTL is reasonably correct and performant
43 *
44 * The simplest purge method is delete().
45 *
46 * Instances of this class must be configured to point to a valid
47 * PubSub endpoint, and there must be listeners on the cache servers
48 * that subscribe to the endpoint and update the caches.
49 *
50 * Broadcasted operations like delete() and touchCheckKey() are done
51 * synchronously in the local datacenter, but are relayed asynchronously.
52 * This means that callers in other datacenters will see older values
53 * for however many milliseconds the datacenters are apart. As with
54 * any cache, this should not be relied on for cases where reads are
55 * used to determine writes to source (e.g. non-cache) data stores.
56 *
57 * All values are wrapped in metadata arrays. Keys use a "WANCache:" prefix
58 * to avoid collisions with keys that are not wrapped as metadata arrays. The
59 * prefixes are as follows:
60 * - a) "WANCache:v" : used for regular value keys
61 * - b) "WANCache:i" : used for temporarily storing values of tombstoned keys
62 * - c) "WANCache:t" : used for storing timestamp "check" keys
63 *
64 * @ingroup Cache
65 * @since 1.26
66 */
67 class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
68 /** @var BagOStuff The local datacenter cache */
69 protected $cache;
70 /** @var HashBagOStuff Script instance PHP cache */
71 protected $procCache;
72 /** @var string Purge channel name */
73 protected $purgeChannel;
74 /** @var EventRelayer Bus that handles purge broadcasts */
75 protected $purgeRelayer;
76 /** @var LoggerInterface */
77 protected $logger;
78
79 /** @var int ERR_* constant for the "last error" registry */
80 protected $lastRelayError = self::ERR_NONE;
81
82 /** Max time expected to pass between delete() and DB commit finishing */
83 const MAX_COMMIT_DELAY = 3;
84 /** Max replication+snapshot lag before applying TTL_LAGGED or disallowing set() */
85 const MAX_READ_LAG = 7;
86 /** Seconds to tombstone keys on delete() */
87 const HOLDOFF_TTL = 11; // MAX_COMMIT_DELAY + MAX_READ_LAG + 1
88
89 /** Seconds to keep dependency purge keys around */
90 const CHECK_KEY_TTL = self::TTL_YEAR;
91 /** Seconds to keep lock keys around */
92 const LOCK_TTL = 10;
93 /** Default remaining TTL at which to consider pre-emptive regeneration */
94 const LOW_TTL = 30;
95 /** Default time-since-expiry on a miss that makes a key "hot" */
96 const LOCK_TSE = 1;
97
98 /** Idiom for getWithSetCallback() callbacks to avoid calling set() */
99 const TTL_UNCACHEABLE = -1;
100 /** Idiom for getWithSetCallback() callbacks to 'lockTSE' logic */
101 const TSE_NONE = -1;
102 /** Max TTL to store keys when a data sourced is lagged */
103 const TTL_LAGGED = 30;
104 /** Idiom for delete() for "no hold-off" */
105 const HOLDOFF_NONE = 0;
106
107 /** Tiny negative float to use when CTL comes up >= 0 due to clock skew */
108 const TINY_NEGATIVE = -0.000001;
109
110 /** Cache format version number */
111 const VERSION = 1;
112
113 const FLD_VERSION = 0; // key to cache version number
114 const FLD_VALUE = 1; // key to the cached value
115 const FLD_TTL = 2; // key to the original TTL
116 const FLD_TIME = 3; // key to the cache time
117 const FLD_FLAGS = 4; // key to the flags bitfield
118 const FLD_HOLDOFF = 5; // key to any hold-off TTL
119
120 /** @var integer Treat this value as expired-on-arrival */
121 const FLG_STALE = 1;
122
123 const ERR_NONE = 0; // no error
124 const ERR_NO_RESPONSE = 1; // no response
125 const ERR_UNREACHABLE = 2; // can't connect
126 const ERR_UNEXPECTED = 3; // response gave some error
127 const ERR_RELAY = 4; // relay broadcast failed
128
129 const VALUE_KEY_PREFIX = 'WANCache:v:';
130 const INTERIM_KEY_PREFIX = 'WANCache:i:';
131 const TIME_KEY_PREFIX = 'WANCache:t:';
132
133 const PURGE_VAL_PREFIX = 'PURGED:';
134
135 const VFLD_DATA = 'WOC:d'; // key to the value of versioned data
136 const VFLD_VERSION = 'WOC:v'; // key to the version of the value present
137
138 const MAX_PC_KEYS = 1000; // max keys to keep in process cache
139
140 const DEFAULT_PURGE_CHANNEL = 'wancache-purge';
141
142 /**
143 * @param array $params
144 * - cache : BagOStuff object for a persistent cache
145 * - channels : Map of (action => channel string). Actions include "purge".
146 * - relayers : Map of (action => EventRelayer object). Actions include "purge".
147 * - logger : LoggerInterface object
148 */
149 public function __construct( array $params ) {
150 $this->cache = $params['cache'];
151 $this->procCache = new HashBagOStuff( [ 'maxKeys' => self::MAX_PC_KEYS ] );
152 $this->purgeChannel = isset( $params['channels']['purge'] )
153 ? $params['channels']['purge']
154 : self::DEFAULT_PURGE_CHANNEL;
155 $this->purgeRelayer = isset( $params['relayers']['purge'] )
156 ? $params['relayers']['purge']
157 : new EventRelayerNull( [] );
158 $this->setLogger( isset( $params['logger'] ) ? $params['logger'] : new NullLogger() );
159 }
160
161 public function setLogger( LoggerInterface $logger ) {
162 $this->logger = $logger;
163 }
164
165 /**
166 * Get an instance that wraps EmptyBagOStuff
167 *
168 * @return WANObjectCache
169 */
170 public static function newEmpty() {
171 return new self( [
172 'cache' => new EmptyBagOStuff(),
173 'pool' => 'empty',
174 'relayer' => new EventRelayerNull( [] )
175 ] );
176 }
177
178 /**
179 * Fetch the value of a key from cache
180 *
181 * If supplied, $curTTL is set to the remaining TTL (current time left):
182 * - a) INF; if $key exists, has no TTL, and is not expired by $checkKeys
183 * - b) float (>=0); if $key exists, has a TTL, and is not expired by $checkKeys
184 * - c) float (<0); if $key is tombstoned, stale, or existing but expired by $checkKeys
185 * - d) null; if $key does not exist and is not tombstoned
186 *
187 * If a key is tombstoned, $curTTL will reflect the time since delete().
188 *
189 * The timestamp of $key will be checked against the last-purge timestamp
190 * of each of $checkKeys. Those $checkKeys not in cache will have the last-purge
191 * initialized to the current timestamp. If any of $checkKeys have a timestamp
192 * greater than that of $key, then $curTTL will reflect how long ago $key
193 * became invalid. Callers can use $curTTL to know when the value is stale.
194 * The $checkKeys parameter allow mass invalidations by updating a single key:
195 * - a) Each "check" key represents "last purged" of some source data
196 * - b) Callers pass in relevant "check" keys as $checkKeys in get()
197 * - c) When the source data that "check" keys represent changes,
198 * the touchCheckKey() method is called on them
199 *
200 * Source data entities might exists in a DB that uses snapshot isolation
201 * (e.g. the default REPEATABLE-READ in innoDB). Even for mutable data, that
202 * isolation can largely be maintained by doing the following:
203 * - a) Calling delete() on entity change *and* creation, before DB commit
204 * - b) Keeping transaction duration shorter than delete() hold-off TTL
205 *
206 * However, pre-snapshot values might still be seen if an update was made
207 * in a remote datacenter but the purge from delete() didn't relay yet.
208 *
209 * Consider using getWithSetCallback() instead of get() and set() cycles.
210 * That method has cache slam avoiding features for hot/expensive keys.
211 *
212 * @param string $key Cache key
213 * @param mixed $curTTL Approximate TTL left on the key if present/tombstoned [returned]
214 * @param array $checkKeys List of "check" keys
215 * @param float &$asOf UNIX timestamp of cached value; null on failure [returned]
216 * @return mixed Value of cache key or false on failure
217 */
218 final public function get( $key, &$curTTL = null, array $checkKeys = [], &$asOf = null ) {
219 $curTTLs = [];
220 $asOfs = [];
221 $values = $this->getMulti( [ $key ], $curTTLs, $checkKeys, $asOfs );
222 $curTTL = isset( $curTTLs[$key] ) ? $curTTLs[$key] : null;
223 $asOf = isset( $asOfs[$key] ) ? $asOfs[$key] : null;
224
225 return isset( $values[$key] ) ? $values[$key] : false;
226 }
227
228 /**
229 * Fetch the value of several keys from cache
230 *
231 * @see WANObjectCache::get()
232 *
233 * @param array $keys List of cache keys
234 * @param array $curTTLs Map of (key => approximate TTL left) for existing keys [returned]
235 * @param array $checkKeys List of check keys to apply to all $keys. May also apply "check"
236 * keys to specific cache keys only by using cache keys as keys in the $checkKeys array.
237 * @param float[] &$asOfs Map of (key => UNIX timestamp of cached value; null on failure)
238 * @return array Map of (key => value) for keys that exist
239 */
240 final public function getMulti(
241 array $keys, &$curTTLs = [], array $checkKeys = [], array &$asOfs = []
242 ) {
243 $result = [];
244 $curTTLs = [];
245 $asOfs = [];
246
247 $vPrefixLen = strlen( self::VALUE_KEY_PREFIX );
248 $valueKeys = self::prefixCacheKeys( $keys, self::VALUE_KEY_PREFIX );
249
250 $checkKeysForAll = [];
251 $checkKeysByKey = [];
252 $checkKeysFlat = [];
253 foreach ( $checkKeys as $i => $keys ) {
254 $prefixed = self::prefixCacheKeys( (array)$keys, self::TIME_KEY_PREFIX );
255 $checkKeysFlat = array_merge( $checkKeysFlat, $prefixed );
256 // Is this check keys for a specific cache key, or for all keys being fetched?
257 if ( is_int( $i ) ) {
258 $checkKeysForAll = array_merge( $checkKeysForAll, $prefixed );
259 } else {
260 $checkKeysByKey[$i] = isset( $checkKeysByKey[$i] )
261 ? array_merge( $checkKeysByKey[$i], $prefixed )
262 : $prefixed;
263 }
264 }
265
266 // Fetch all of the raw values
267 $wrappedValues = $this->cache->getMulti( array_merge( $valueKeys, $checkKeysFlat ) );
268 // Time used to compare/init "check" keys (derived after getMulti() to be pessimistic)
269 $now = microtime( true );
270
271 // Collect timestamps from all "check" keys
272 $purgeValuesForAll = $this->processCheckKeys( $checkKeysForAll, $wrappedValues, $now );
273 $purgeValuesByKey = [];
274 foreach ( $checkKeysByKey as $cacheKey => $checks ) {
275 $purgeValuesByKey[$cacheKey] =
276 $this->processCheckKeys( $checks, $wrappedValues, $now );
277 }
278
279 // Get the main cache value for each key and validate them
280 foreach ( $valueKeys as $vKey ) {
281 if ( !isset( $wrappedValues[$vKey] ) ) {
282 continue; // not found
283 }
284
285 $key = substr( $vKey, $vPrefixLen ); // unprefix
286
287 list( $value, $curTTL ) = $this->unwrap( $wrappedValues[$vKey], $now );
288 if ( $value !== false ) {
289 $result[$key] = $value;
290
291 // Force dependant keys to be invalid for a while after purging
292 // to reduce race conditions involving stale data getting cached
293 $purgeValues = $purgeValuesForAll;
294 if ( isset( $purgeValuesByKey[$key] ) ) {
295 $purgeValues = array_merge( $purgeValues, $purgeValuesByKey[$key] );
296 }
297 foreach ( $purgeValues as $purge ) {
298 $safeTimestamp = $purge[self::FLD_TIME] + $purge[self::FLD_HOLDOFF];
299 if ( $safeTimestamp >= $wrappedValues[$vKey][self::FLD_TIME] ) {
300 // How long ago this value was expired by *this* check key
301 $ago = min( $purge[self::FLD_TIME] - $now, self::TINY_NEGATIVE );
302 // How long ago this value was expired by *any* known check key
303 $curTTL = min( $curTTL, $ago );
304 }
305 }
306 }
307 $curTTLs[$key] = $curTTL;
308 $asOfs[$key] = ( $value !== false ) ? $wrappedValues[$vKey][self::FLD_TIME] : null;
309 }
310
311 return $result;
312 }
313
314 /**
315 * @since 1.27
316 * @param array $timeKeys List of prefixed time check keys
317 * @param array $wrappedValues
318 * @param float $now
319 * @return array List of purge value arrays
320 */
321 private function processCheckKeys( array $timeKeys, array $wrappedValues, $now ) {
322 $purgeValues = [];
323 foreach ( $timeKeys as $timeKey ) {
324 $purge = isset( $wrappedValues[$timeKey] )
325 ? self::parsePurgeValue( $wrappedValues[$timeKey] )
326 : false;
327 if ( $purge === false ) {
328 // Key is not set or invalid; regenerate
329 $newVal = $this->makePurgeValue( $now, self::HOLDOFF_TTL );
330 $this->cache->add( $timeKey, $newVal, self::CHECK_KEY_TTL );
331 $purge = self::parsePurgeValue( $newVal );
332 }
333 $purgeValues[] = $purge;
334 }
335 return $purgeValues;
336 }
337
338 /**
339 * Set the value of a key in cache
340 *
341 * Simply calling this method when source data changes is not valid because
342 * the changes do not replicate to the other WAN sites. In that case, delete()
343 * should be used instead. This method is intended for use on cache misses.
344 *
345 * If the data was read from a snapshot-isolated transactions (e.g. the default
346 * REPEATABLE-READ in innoDB), use 'since' to avoid the following race condition:
347 * - a) T1 starts
348 * - b) T2 updates a row, calls delete(), and commits
349 * - c) The HOLDOFF_TTL passes, expiring the delete() tombstone
350 * - d) T1 reads the row and calls set() due to a cache miss
351 * - e) Stale value is stuck in cache
352 *
353 * Setting 'lag' and 'since' help avoids keys getting stuck in stale states.
354 *
355 * Example usage:
356 * @code
357 * $dbr = wfGetDB( DB_SLAVE );
358 * $setOpts = Database::getCacheSetOptions( $dbr );
359 * // Fetch the row from the DB
360 * $row = $dbr->selectRow( ... );
361 * $key = $cache->makeKey( 'building', $buildingId );
362 * $cache->set( $key, $row, $cache::TTL_DAY, $setOpts );
363 * @endcode
364 *
365 * @param string $key Cache key
366 * @param mixed $value
367 * @param integer $ttl Seconds to live. Special values are:
368 * - WANObjectCache::TTL_INDEFINITE: Cache forever
369 * @param array $opts Options map:
370 * - lag : Seconds of slave lag. Typically, this is either the slave lag
371 * before the data was read or, if applicable, the slave lag before
372 * the snapshot-isolated transaction the data was read from started.
373 * Default: 0 seconds
374 * - since : UNIX timestamp of the data in $value. Typically, this is either
375 * the current time the data was read or (if applicable) the time when
376 * the snapshot-isolated transaction the data was read from started.
377 * Default: 0 seconds
378 * - pending : Whether this data is possibly from an uncommitted write transaction.
379 * Generally, other threads should not see values from the future and
380 * they certainly should not see ones that ended up getting rolled back.
381 * Default: false
382 * - lockTSE : if excessive replication/snapshot lag is detected, then store the value
383 * with this TTL and flag it as stale. This is only useful if the reads for
384 * this key use getWithSetCallback() with "lockTSE" set.
385 * Default: WANObjectCache::TSE_NONE
386 * @return bool Success
387 */
388 final public function set( $key, $value, $ttl = 0, array $opts = [] ) {
389 $now = microtime( true );
390 $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
391 $age = isset( $opts['since'] ) ? max( 0, $now - $opts['since'] ) : 0;
392 $lag = isset( $opts['lag'] ) ? $opts['lag'] : 0;
393
394 // Do not cache potentially uncommitted data as it might get rolled back
395 if ( !empty( $opts['pending'] ) ) {
396 $this->logger->info( "Rejected set() for $key due to pending writes." );
397
398 return true; // no-op the write for being unsafe
399 }
400
401 $wrapExtra = []; // additional wrapped value fields
402 // Check if there's a risk of writing stale data after the purge tombstone expired
403 if ( $lag === false || ( $lag + $age ) > self::MAX_READ_LAG ) {
404 // Case A: read lag with "lockTSE"; save but record value as stale
405 if ( $lockTSE >= 0 ) {
406 $ttl = max( 1, (int)$lockTSE ); // set() expects seconds
407 $wrapExtra[self::FLD_FLAGS] = self::FLG_STALE; // mark as stale
408 // Case B: any long-running transaction; ignore this set()
409 } elseif ( $age > self::MAX_READ_LAG ) {
410 $this->logger->warning( "Rejected set() for $key due to snapshot lag." );
411
412 return true; // no-op the write for being unsafe
413 // Case C: high replication lag; lower TTL instead of ignoring all set()s
414 } elseif ( $lag === false || $lag > self::MAX_READ_LAG ) {
415 $ttl = $ttl ? min( $ttl, self::TTL_LAGGED ) : self::TTL_LAGGED;
416 $this->logger->warning( "Lowered set() TTL for $key due to replication lag." );
417 // Case D: medium length request with medium replication lag; ignore this set()
418 } else {
419 $this->logger->warning( "Rejected set() for $key due to high read lag." );
420
421 return true; // no-op the write for being unsafe
422 }
423 }
424
425 // Wrap that value with time/TTL/version metadata
426 $wrapped = $this->wrap( $value, $ttl, $now ) + $wrapExtra;
427
428 $func = function ( $cache, $key, $cWrapped ) use ( $wrapped ) {
429 return ( is_string( $cWrapped ) )
430 ? false // key is tombstoned; do nothing
431 : $wrapped;
432 };
433
434 return $this->cache->merge( self::VALUE_KEY_PREFIX . $key, $func, $ttl, 1 );
435 }
436
437 /**
438 * Purge a key from all datacenters
439 *
440 * This should only be called when the underlying data (being cached)
441 * changes in a significant way. This deletes the key and starts a hold-off
442 * period where the key cannot be written to for a few seconds (HOLDOFF_TTL).
443 * This is done to avoid the following race condition:
444 * - a) Some DB data changes and delete() is called on a corresponding key
445 * - b) A request refills the key with a stale value from a lagged DB
446 * - c) The stale value is stuck there until the key is expired/evicted
447 *
448 * This is implemented by storing a special "tombstone" value at the cache
449 * key that this class recognizes; get() calls will return false for the key
450 * and any set() calls will refuse to replace tombstone values at the key.
451 * For this to always avoid stale value writes, the following must hold:
452 * - a) Replication lag is bounded to being less than HOLDOFF_TTL; or
453 * - b) If lag is higher, the DB will have gone into read-only mode already
454 *
455 * Note that set() can also be lag-aware and lower the TTL if it's high.
456 *
457 * When using potentially long-running ACID transactions, a good pattern is
458 * to use a pre-commit hook to issue the delete. This means that immediately
459 * after commit, callers will see the tombstone in cache in the local datacenter
460 * and in the others upon relay. It also avoids the following race condition:
461 * - a) T1 begins, changes a row, and calls delete()
462 * - b) The HOLDOFF_TTL passes, expiring the delete() tombstone
463 * - c) T2 starts, reads the row and calls set() due to a cache miss
464 * - d) T1 finally commits
465 * - e) Stale value is stuck in cache
466 *
467 * Example usage:
468 * @code
469 * $dbw->startAtomic( __METHOD__ ); // start of request
470 * ... <execute some stuff> ...
471 * // Update the row in the DB
472 * $dbw->update( ... );
473 * $key = $cache->makeKey( 'homes', $homeId );
474 * // Purge the corresponding cache entry just before committing
475 * $dbw->onTransactionPreCommitOrIdle( function() use ( $cache, $key ) {
476 * $cache->delete( $key );
477 * } );
478 * ... <execute some stuff> ...
479 * $dbw->endAtomic( __METHOD__ ); // end of request
480 * @endcode
481 *
482 * The $ttl parameter can be used when purging values that have not actually changed
483 * recently. For example, a cleanup script to purge cache entries does not really need
484 * a hold-off period, so it can use HOLDOFF_NONE. Likewise for user-requested purge.
485 * Note that $ttl limits the effective range of 'lockTSE' for getWithSetCallback().
486 *
487 * If called twice on the same key, then the last hold-off TTL takes precedence. For
488 * idempotence, the $ttl should not vary for different delete() calls on the same key.
489 *
490 * @param string $key Cache key
491 * @param integer $ttl Tombstone TTL; Default: WANObjectCache::HOLDOFF_TTL
492 * @return bool True if the item was purged or not found, false on failure
493 */
494 final public function delete( $key, $ttl = self::HOLDOFF_TTL ) {
495 $key = self::VALUE_KEY_PREFIX . $key;
496
497 if ( $ttl <= 0 ) {
498 // Update the local datacenter immediately
499 $ok = $this->cache->delete( $key );
500 // Publish the purge to all datacenters
501 $ok = $this->relayDelete( $key ) && $ok;
502 } else {
503 // Update the local datacenter immediately
504 $ok = $this->cache->set( $key,
505 $this->makePurgeValue( microtime( true ), self::HOLDOFF_NONE ),
506 $ttl
507 );
508 // Publish the purge to all datacenters
509 $ok = $this->relayPurge( $key, $ttl, self::HOLDOFF_NONE ) && $ok;
510 }
511
512 return $ok;
513 }
514
515 /**
516 * Fetch the value of a timestamp "check" key
517 *
518 * The key will be *initialized* to the current time if not set,
519 * so only call this method if this behavior is actually desired
520 *
521 * The timestamp can be used to check whether a cached value is valid.
522 * Callers should not assume that this returns the same timestamp in
523 * all datacenters due to relay delays.
524 *
525 * The level of staleness can roughly be estimated from this key, but
526 * if the key was evicted from cache, such calculations may show the
527 * time since expiry as ~0 seconds.
528 *
529 * Note that "check" keys won't collide with other regular keys.
530 *
531 * @param string $key
532 * @return float UNIX timestamp of the check key
533 */
534 final public function getCheckKeyTime( $key ) {
535 $key = self::TIME_KEY_PREFIX . $key;
536
537 $purge = self::parsePurgeValue( $this->cache->get( $key ) );
538 if ( $purge !== false ) {
539 $time = $purge[self::FLD_TIME];
540 } else {
541 // Casting assures identical floats for the next getCheckKeyTime() calls
542 $now = (string)microtime( true );
543 $this->cache->add( $key,
544 $this->makePurgeValue( $now, self::HOLDOFF_TTL ),
545 self::CHECK_KEY_TTL
546 );
547 $time = (float)$now;
548 }
549
550 return $time;
551 }
552
553 /**
554 * Purge a "check" key from all datacenters, invalidating keys that use it
555 *
556 * This should only be called when the underlying data (being cached)
557 * changes in a significant way, and it is impractical to call delete()
558 * on all keys that should be changed. When get() is called on those
559 * keys, the relevant "check" keys must be supplied for this to work.
560 *
561 * The "check" key essentially represents a last-modified field.
562 * When touched, keys using it via get(), getMulti(), or getWithSetCallback()
563 * will be invalidated. It is treated as being HOLDOFF_TTL seconds in the future
564 * by those methods to avoid race conditions where dependent keys get updated
565 * with stale values (e.g. from a DB slave).
566 *
567 * This is typically useful for keys with hardcoded names or in some cases
568 * dynamically generated names where a low number of combinations exist.
569 * When a few important keys get a large number of hits, a high cache
570 * time is usually desired as well as "lockTSE" logic. The resetCheckKey()
571 * method is less appropriate in such cases since the "time since expiry"
572 * cannot be inferred.
573 *
574 * Note that "check" keys won't collide with other regular keys.
575 *
576 * @see WANObjectCache::get()
577 * @see WANObjectCache::getWithSetCallback()
578 * @see WANObjectCache::resetCheckKey()
579 *
580 * @param string $key Cache key
581 * @param int $holdoff HOLDOFF_TTL or HOLDOFF_NONE constant
582 * @return bool True if the item was purged or not found, false on failure
583 */
584 final public function touchCheckKey( $key, $holdoff = self::HOLDOFF_TTL ) {
585 $key = self::TIME_KEY_PREFIX . $key;
586 // Update the local datacenter immediately
587 $ok = $this->cache->set( $key,
588 $this->makePurgeValue( microtime( true ), $holdoff ),
589 self::CHECK_KEY_TTL
590 );
591 // Publish the purge to all datacenters
592 return $this->relayPurge( $key, self::CHECK_KEY_TTL, $holdoff ) && $ok;
593 }
594
595 /**
596 * Delete a "check" key from all datacenters, invalidating keys that use it
597 *
598 * This is similar to touchCheckKey() in that keys using it via get(), getMulti(),
599 * or getWithSetCallback() will be invalidated. The differences are:
600 * - a) The timestamp will be deleted from all caches and lazily
601 * re-initialized when accessed (rather than set everywhere)
602 * - b) Thus, dependent keys will be known to be invalid, but not
603 * for how long (they are treated as "just" purged), which
604 * effects any lockTSE logic in getWithSetCallback()
605 *
606 * The advantage is that this does not place high TTL keys on every cache
607 * server, making it better for code that will cache many different keys
608 * and either does not use lockTSE or uses a low enough TTL anyway.
609 *
610 * This is typically useful for keys with dynamically generated names
611 * where a high number of combinations exist.
612 *
613 * Note that "check" keys won't collide with other regular keys.
614 *
615 * @see WANObjectCache::get()
616 * @see WANObjectCache::getWithSetCallback()
617 * @see WANObjectCache::touchCheckKey()
618 *
619 * @param string $key Cache key
620 * @return bool True if the item was purged or not found, false on failure
621 */
622 final public function resetCheckKey( $key ) {
623 $key = self::TIME_KEY_PREFIX . $key;
624 // Update the local datacenter immediately
625 $ok = $this->cache->delete( $key );
626 // Publish the purge to all datacenters
627 return $this->relayDelete( $key ) && $ok;
628 }
629
630 /**
631 * Method to fetch/regenerate cache keys
632 *
633 * On cache miss, the key will be set to the callback result via set()
634 * (unless the callback returns false) and that result will be returned.
635 * The arguments supplied to the callback are:
636 * - $oldValue : current cache value or false if not present
637 * - &$ttl : a reference to the TTL which can be altered
638 * - &$setOpts : a reference to options for set() which can be altered
639 *
640 * It is strongly recommended to set the 'lag' and 'since' fields to avoid race conditions
641 * that can cause stale values to get stuck at keys. Usually, callbacks ignore the current
642 * value, but it can be used to maintain "most recent X" values that come from time or
643 * sequence based source data, provided that the "as of" id/time is tracked. Note that
644 * preemptive regeneration and $checkKeys can result in a non-false current value.
645 *
646 * Usage of $checkKeys is similar to get() and getMulti(). However, rather than the caller
647 * having to inspect a "current time left" variable (e.g. $curTTL, $curTTLs), a cache
648 * regeneration will automatically be triggered using the callback.
649 *
650 * The simplest way to avoid stampedes for hot keys is to use
651 * the 'lockTSE' option in $opts. If cache purges are needed, also:
652 * - a) Pass $key into $checkKeys
653 * - b) Use touchCheckKey( $key ) instead of delete( $key )
654 *
655 * Example usage (typical key):
656 * @code
657 * $catInfo = $cache->getWithSetCallback(
658 * // Key to store the cached value under
659 * $cache->makeKey( 'cat-attributes', $catId ),
660 * // Time-to-live (in seconds)
661 * $cache::TTL_MINUTE,
662 * // Function that derives the new key value
663 * function ( $oldValue, &$ttl, array &$setOpts ) {
664 * $dbr = wfGetDB( DB_SLAVE );
665 * // Account for any snapshot/slave lag
666 * $setOpts += Database::getCacheSetOptions( $dbr );
667 *
668 * return $dbr->selectRow( ... );
669 * }
670 * );
671 * @endcode
672 *
673 * Example usage (key that is expensive and hot):
674 * @code
675 * $catConfig = $cache->getWithSetCallback(
676 * // Key to store the cached value under
677 * $cache->makeKey( 'site-cat-config' ),
678 * // Time-to-live (in seconds)
679 * $cache::TTL_DAY,
680 * // Function that derives the new key value
681 * function ( $oldValue, &$ttl, array &$setOpts ) {
682 * $dbr = wfGetDB( DB_SLAVE );
683 * // Account for any snapshot/slave lag
684 * $setOpts += Database::getCacheSetOptions( $dbr );
685 *
686 * return CatConfig::newFromRow( $dbr->selectRow( ... ) );
687 * },
688 * [
689 * // Calling touchCheckKey() on this key invalidates the cache
690 * 'checkKeys' => [ $cache->makeKey( 'site-cat-config' ) ],
691 * // Try to only let one datacenter thread manage cache updates at a time
692 * 'lockTSE' => 30,
693 * // Avoid querying cache servers multiple times in a web request
694 * 'pcTTL' => $cache::TTL_PROC_LONG
695 * ]
696 * );
697 * @endcode
698 *
699 * Example usage (key with dynamic dependencies):
700 * @code
701 * $catState = $cache->getWithSetCallback(
702 * // Key to store the cached value under
703 * $cache->makeKey( 'cat-state', $cat->getId() ),
704 * // Time-to-live (seconds)
705 * $cache::TTL_HOUR,
706 * // Function that derives the new key value
707 * function ( $oldValue, &$ttl, array &$setOpts ) {
708 * // Determine new value from the DB
709 * $dbr = wfGetDB( DB_SLAVE );
710 * // Account for any snapshot/slave lag
711 * $setOpts += Database::getCacheSetOptions( $dbr );
712 *
713 * return CatState::newFromResults( $dbr->select( ... ) );
714 * },
715 * [
716 * // The "check" keys that represent things the value depends on;
717 * // Calling touchCheckKey() on any of them invalidates the cache
718 * 'checkKeys' => [
719 * $cache->makeKey( 'sustenance-bowls', $cat->getRoomId() ),
720 * $cache->makeKey( 'people-present', $cat->getHouseId() ),
721 * $cache->makeKey( 'cat-laws', $cat->getCityId() ),
722 * ]
723 * ]
724 * );
725 * @endcode
726 *
727 * Example usage (hot key holding most recent 100 events):
728 * @code
729 * $lastCatActions = $cache->getWithSetCallback(
730 * // Key to store the cached value under
731 * $cache->makeKey( 'cat-last-actions', 100 ),
732 * // Time-to-live (in seconds)
733 * 10,
734 * // Function that derives the new key value
735 * function ( $oldValue, &$ttl, array &$setOpts ) {
736 * $dbr = wfGetDB( DB_SLAVE );
737 * // Account for any snapshot/slave lag
738 * $setOpts += Database::getCacheSetOptions( $dbr );
739 *
740 * // Start off with the last cached list
741 * $list = $oldValue ?: [];
742 * // Fetch the last 100 relevant rows in descending order;
743 * // only fetch rows newer than $list[0] to reduce scanning
744 * $rows = iterator_to_array( $dbr->select( ... ) );
745 * // Merge them and get the new "last 100" rows
746 * return array_slice( array_merge( $new, $list ), 0, 100 );
747 * },
748 * [
749 * // Try to only let one datacenter thread manage cache updates at a time
750 * 'lockTSE' => 30,
751 * // Use a magic value when no cache value is ready rather than stampeding
752 * 'busyValue' => 'computing'
753 * ]
754 * );
755 * @endcode
756 *
757 * @see WANObjectCache::get()
758 * @see WANObjectCache::set()
759 *
760 * @param string $key Cache key
761 * @param integer $ttl Seconds to live for key updates. Special values are:
762 * - WANObjectCache::TTL_INDEFINITE: Cache forever
763 * - WANObjectCache::TTL_UNCACHEABLE: Do not cache at all
764 * @param callable $callback Value generation function
765 * @param array $opts Options map:
766 * - checkKeys: List of "check" keys. The key at $key will be seen as invalid when either
767 * touchCheckKey() or resetCheckKey() is called on any of these keys.
768 * Default: [].
769 * - lowTTL: Consider pre-emptive updates when the current TTL (seconds) of the key is less
770 * than this. It becomes more likely over time, becoming certain once the key is expired.
771 * Default: WANObjectCache::LOW_TTL.
772 * - lockTSE: If the key is tombstoned or expired (by checkKeys) less than this many seconds
773 * ago, then try to have a single thread handle cache regeneration at any given time.
774 * Other threads will try to use stale values if possible. If, on miss, the time since
775 * expiration is low, the assumption is that the key is hot and that a stampede is worth
776 * avoiding. Setting this above WANObjectCache::HOLDOFF_TTL makes no difference. The
777 * higher this is set, the higher the worst-case staleness can be.
778 * Use WANObjectCache::TSE_NONE to disable this logic.
779 * Default: WANObjectCache::TSE_NONE.
780 * - busyValue: If no value exists and another thread is currently regenerating it, use this
781 * as a fallback value (or a callback to generate such a value). This assures that cache
782 * stampedes cannot happen if the value falls out of cache. This can be used as insurance
783 * against cache regeneration becoming very slow for some reason (greater than the TTL).
784 * Default: null.
785 * - pcTTL: Process cache the value in this PHP instance for this many seconds. This avoids
786 * network I/O when a key is read several times. This will not cache when the callback
787 * returns false, however. Note that any purges will not be seen while process cached;
788 * since the callback should use slave DBs and they may be lagged or have snapshot
789 * isolation anyway, this should not typically matter.
790 * Default: WANObjectCache::TTL_UNCACHEABLE.
791 * - version: Integer version number. This allows for callers to make breaking changes to
792 * how values are stored while maintaining compatability and correct cache purges. New
793 * versions are stored alongside older versions concurrently. Avoid storing class objects
794 * however, as this reduces compatibility (due to serialization).
795 * Default: null.
796 * @return mixed Value found or written to the key
797 * @note Callable type hints are not used to avoid class-autoloading
798 */
799 final public function getWithSetCallback( $key, $ttl, $callback, array $opts = [] ) {
800 $pcTTL = isset( $opts['pcTTL'] ) ? $opts['pcTTL'] : self::TTL_UNCACHEABLE;
801
802 // Try the process cache if enabled
803 $value = ( $pcTTL >= 0 ) ? $this->procCache->get( $key ) : false;
804
805 if ( $value === false ) {
806 unset( $opts['minTime'] ); // not a public feature
807
808 // Fetch the value over the network
809 if ( isset( $opts['version'] ) ) {
810 $version = $opts['version'];
811 $asOf = null;
812 $cur = $this->doGetWithSetCallback(
813 $key,
814 $ttl,
815 function ( $oldValue, &$ttl, &$setOpts ) use ( $callback, $version ) {
816 if ( is_array( $oldValue )
817 && array_key_exists( self::VFLD_DATA, $oldValue )
818 ) {
819 $oldData = $oldValue[self::VFLD_DATA];
820 } else {
821 // VFLD_DATA is not set if an old, unversioned, key is present
822 $oldData = false;
823 }
824
825 return [
826 self::VFLD_DATA => $callback( $oldData, $ttl, $setOpts ),
827 self::VFLD_VERSION => $version
828 ];
829 },
830 $opts,
831 $asOf
832 );
833 if ( $cur[self::VFLD_VERSION] === $version ) {
834 // Value created or existed before with version; use it
835 $value = $cur[self::VFLD_DATA];
836 } else {
837 // Value existed before with a different version; use variant key.
838 // Reflect purges to $key by requiring that this key value be newer.
839 $value = $this->doGetWithSetCallback(
840 'cache-variant:' . md5( $key ) . ":$version",
841 $ttl,
842 $callback,
843 // Regenerate value if not newer than $key
844 [ 'version' => null, 'minTime' => $asOf ] + $opts
845 );
846 }
847 } else {
848 $value = $this->doGetWithSetCallback( $key, $ttl, $callback, $opts );
849 }
850
851 // Update the process cache if enabled
852 if ( $pcTTL >= 0 && $value !== false ) {
853 $this->procCache->set( $key, $value, $pcTTL );
854 }
855 }
856
857 return $value;
858 }
859
860 /**
861 * Do the actual I/O for getWithSetCallback() when needed
862 *
863 * @see WANObjectCache::getWithSetCallback()
864 *
865 * @param string $key
866 * @param integer $ttl
867 * @param callback $callback
868 * @param array $opts Options map for getWithSetCallback() which also includes:
869 * - minTime: Treat values older than this UNIX timestamp as not existing. Default: null.
870 * @param float &$asOf Cache generation timestamp of returned value [returned]
871 * @return mixed
872 * @note Callable type hints are not used to avoid class-autoloading
873 */
874 protected function doGetWithSetCallback( $key, $ttl, $callback, array $opts, &$asOf = null ) {
875 $lowTTL = isset( $opts['lowTTL'] ) ? $opts['lowTTL'] : min( self::LOW_TTL, $ttl );
876 $lockTSE = isset( $opts['lockTSE'] ) ? $opts['lockTSE'] : self::TSE_NONE;
877 $checkKeys = isset( $opts['checkKeys'] ) ? $opts['checkKeys'] : [];
878 $busyValue = isset( $opts['busyValue'] ) ? $opts['busyValue'] : null;
879 $minTime = isset( $opts['minTime'] ) ? $opts['minTime'] : 0.0;
880 $versioned = isset( $opts['version'] );
881
882 // Get the current key value
883 $curTTL = null;
884 $cValue = $this->get( $key, $curTTL, $checkKeys, $asOf ); // current value
885 $value = $cValue; // return value
886
887 // Determine if a regeneration is desired
888 if ( $value !== false
889 && $curTTL > 0
890 && $this->isValid( $value, $versioned, $asOf, $minTime )
891 && !$this->worthRefresh( $curTTL, $lowTTL )
892 ) {
893 return $value;
894 }
895
896 // A deleted key with a negative TTL left must be tombstoned
897 $isTombstone = ( $curTTL !== null && $value === false );
898 // Assume a key is hot if requested soon after invalidation
899 $isHot = ( $curTTL !== null && $curTTL <= 0 && abs( $curTTL ) <= $lockTSE );
900 // Use the mutex if there is no value and a busy fallback is given
901 $checkBusy = ( $busyValue !== null && $value === false );
902 // Decide whether a single thread should handle regenerations.
903 // This avoids stampedes when $checkKeys are bumped and when preemptive
904 // renegerations take too long. It also reduces regenerations while $key
905 // is tombstoned. This balances cache freshness with avoiding DB load.
906 $useMutex = ( $isHot || ( $isTombstone && $lockTSE > 0 ) || $checkBusy );
907
908 $lockAcquired = false;
909 if ( $useMutex ) {
910 // Acquire a datacenter-local non-blocking lock
911 if ( $this->cache->lock( $key, 0, self::LOCK_TTL ) ) {
912 // Lock acquired; this thread should update the key
913 $lockAcquired = true;
914 } elseif ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
915 // If it cannot be acquired; then the stale value can be used
916 return $value;
917 } else {
918 // Use the INTERIM value for tombstoned keys to reduce regeneration load.
919 // For hot keys, either another thread has the lock or the lock failed;
920 // use the INTERIM value from the last thread that regenerated it.
921 $wrapped = $this->cache->get( self::INTERIM_KEY_PREFIX . $key );
922 list( $value ) = $this->unwrap( $wrapped, microtime( true ) );
923 if ( $value !== false && $this->isValid( $value, $versioned, $asOf, $minTime ) ) {
924 $asOf = $wrapped[self::FLD_TIME];
925
926 return $value;
927 }
928 // Use the busy fallback value if nothing else
929 if ( $busyValue !== null ) {
930 return is_callable( $busyValue ) ? $busyValue() : $busyValue;
931 }
932 }
933 }
934
935 if ( !is_callable( $callback ) ) {
936 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
937 }
938
939 // Generate the new value from the callback...
940 $setOpts = [];
941 $value = call_user_func_array( $callback, [ $cValue, &$ttl, &$setOpts ] );
942 $asOf = microtime( true );
943 // When delete() is called, writes are write-holed by the tombstone,
944 // so use a special INTERIM key to pass the new value around threads.
945 if ( ( $isTombstone && $lockTSE > 0 ) && $value !== false && $ttl >= 0 ) {
946 $tempTTL = max( 1, (int)$lockTSE ); // set() expects seconds
947 $wrapped = $this->wrap( $value, $tempTTL, $asOf );
948 $this->cache->set( self::INTERIM_KEY_PREFIX . $key, $wrapped, $tempTTL );
949 }
950
951 if ( $lockAcquired ) {
952 $this->cache->unlock( $key );
953 }
954
955 if ( $value !== false && $ttl >= 0 ) {
956 // Update the cache; this will fail if the key is tombstoned
957 $setOpts['lockTSE'] = $lockTSE;
958 $this->set( $key, $value, $ttl, $setOpts );
959 }
960
961 return $value;
962 }
963
964 /**
965 * @see BagOStuff::makeKey()
966 * @param string ... Key component
967 * @return string
968 * @since 1.27
969 */
970 public function makeKey() {
971 return call_user_func_array( [ $this->cache, __FUNCTION__ ], func_get_args() );
972 }
973
974 /**
975 * @see BagOStuff::makeGlobalKey()
976 * @param string ... Key component
977 * @return string
978 * @since 1.27
979 */
980 public function makeGlobalKey() {
981 return call_user_func_array( [ $this->cache, __FUNCTION__ ], func_get_args() );
982 }
983
984 /**
985 * Get the "last error" registered; clearLastError() should be called manually
986 * @return int ERR_* class constant for the "last error" registry
987 */
988 final public function getLastError() {
989 if ( $this->lastRelayError ) {
990 // If the cache and the relayer failed, focus on the later.
991 // An update not making it to the relayer means it won't show up
992 // in other DCs (nor will consistent re-hashing see up-to-date values).
993 // On the other hand, if just the cache update failed, then it should
994 // eventually be applied by the relayer.
995 return $this->lastRelayError;
996 }
997
998 $code = $this->cache->getLastError();
999 switch ( $code ) {
1000 case BagOStuff::ERR_NONE:
1001 return self::ERR_NONE;
1002 case BagOStuff::ERR_NO_RESPONSE:
1003 return self::ERR_NO_RESPONSE;
1004 case BagOStuff::ERR_UNREACHABLE:
1005 return self::ERR_UNREACHABLE;
1006 default:
1007 return self::ERR_UNEXPECTED;
1008 }
1009 }
1010
1011 /**
1012 * Clear the "last error" registry
1013 */
1014 final public function clearLastError() {
1015 $this->cache->clearLastError();
1016 $this->lastRelayError = self::ERR_NONE;
1017 }
1018
1019 /**
1020 * Clear the in-process caches; useful for testing
1021 *
1022 * @since 1.27
1023 */
1024 public function clearProcessCache() {
1025 $this->procCache->clear();
1026 }
1027
1028 /**
1029 * @param integer $flag ATTR_* class constant
1030 * @return integer QOS_* class constant
1031 * @since 1.28
1032 */
1033 public function getQoS( $flag ) {
1034 return $this->cache->getQoS( $flag );
1035 }
1036
1037 /**
1038 * Do the actual async bus purge of a key
1039 *
1040 * This must set the key to "PURGED:<UNIX timestamp>:<holdoff>"
1041 *
1042 * @param string $key Cache key
1043 * @param integer $ttl How long to keep the tombstone [seconds]
1044 * @param integer $holdoff HOLDOFF_* constant controlling how long to ignore sets for this key
1045 * @return bool Success
1046 */
1047 protected function relayPurge( $key, $ttl, $holdoff ) {
1048 $event = $this->cache->modifySimpleRelayEvent( [
1049 'cmd' => 'set',
1050 'key' => $key,
1051 'val' => 'PURGED:$UNIXTIME$:' . (int)$holdoff,
1052 'ttl' => max( $ttl, 1 ),
1053 'sbt' => true, // substitute $UNIXTIME$ with actual microtime
1054 ] );
1055
1056 $ok = $this->purgeRelayer->notify( $this->purgeChannel, $event );
1057 if ( !$ok ) {
1058 $this->lastRelayError = self::ERR_RELAY;
1059 }
1060
1061 return $ok;
1062 }
1063
1064 /**
1065 * Do the actual async bus delete of a key
1066 *
1067 * @param string $key Cache key
1068 * @return bool Success
1069 */
1070 protected function relayDelete( $key ) {
1071 $event = $this->cache->modifySimpleRelayEvent( [
1072 'cmd' => 'delete',
1073 'key' => $key,
1074 ] );
1075
1076 $ok = $this->purgeRelayer->notify( $this->purgeChannel, $event );
1077 if ( !$ok ) {
1078 $this->lastRelayError = self::ERR_RELAY;
1079 }
1080
1081 return $ok;
1082 }
1083
1084 /**
1085 * Check if a key should be regenerated (using random probability)
1086 *
1087 * This returns false if $curTTL >= $lowTTL. Otherwise, the chance
1088 * of returning true increases steadily from 0% to 100% as the $curTTL
1089 * moves from $lowTTL to 0 seconds. This handles widely varying
1090 * levels of cache access traffic.
1091 *
1092 * @param float $curTTL Approximate TTL left on the key if present
1093 * @param float $lowTTL Consider a refresh when $curTTL is less than this
1094 * @return bool
1095 */
1096 protected function worthRefresh( $curTTL, $lowTTL ) {
1097 if ( $curTTL >= $lowTTL ) {
1098 return false;
1099 } elseif ( $curTTL <= 0 ) {
1100 return true;
1101 }
1102
1103 $chance = ( 1 - $curTTL / $lowTTL );
1104
1105 return mt_rand( 1, 1e9 ) <= 1e9 * $chance;
1106 }
1107
1108 /**
1109 * Check whether $value is appropriately versioned and not older than $minTime (if set)
1110 *
1111 * @param array $value
1112 * @param bool $versioned
1113 * @param float $asOf The time $value was generated
1114 * @param float $minTime The last time the main value was generated (0.0 if unknown)
1115 * @return bool
1116 */
1117 protected function isValid( $value, $versioned, $asOf, $minTime ) {
1118 if ( $versioned && !isset( $value[self::VFLD_VERSION] ) ) {
1119 return false;
1120 } elseif ( $minTime > 0 && $asOf < $minTime ) {
1121 return false;
1122 }
1123
1124 return true;
1125 }
1126
1127 /**
1128 * Do not use this method outside WANObjectCache
1129 *
1130 * @param mixed $value
1131 * @param integer $ttl [0=forever]
1132 * @param float $now Unix Current timestamp just before calling set()
1133 * @return array
1134 */
1135 protected function wrap( $value, $ttl, $now ) {
1136 return [
1137 self::FLD_VERSION => self::VERSION,
1138 self::FLD_VALUE => $value,
1139 self::FLD_TTL => $ttl,
1140 self::FLD_TIME => $now
1141 ];
1142 }
1143
1144 /**
1145 * Do not use this method outside WANObjectCache
1146 *
1147 * @param array|string|bool $wrapped
1148 * @param float $now Unix Current timestamp (preferrably pre-query)
1149 * @return array (mixed; false if absent/invalid, current time left)
1150 */
1151 protected function unwrap( $wrapped, $now ) {
1152 // Check if the value is a tombstone
1153 $purge = self::parsePurgeValue( $wrapped );
1154 if ( $purge !== false ) {
1155 // Purged values should always have a negative current $ttl
1156 $curTTL = min( $purge[self::FLD_TIME] - $now, self::TINY_NEGATIVE );
1157 return [ false, $curTTL ];
1158 }
1159
1160 if ( !is_array( $wrapped ) // not found
1161 || !isset( $wrapped[self::FLD_VERSION] ) // wrong format
1162 || $wrapped[self::FLD_VERSION] !== self::VERSION // wrong version
1163 ) {
1164 return [ false, null ];
1165 }
1166
1167 $flags = isset( $wrapped[self::FLD_FLAGS] ) ? $wrapped[self::FLD_FLAGS] : 0;
1168 if ( ( $flags & self::FLG_STALE ) == self::FLG_STALE ) {
1169 // Treat as expired, with the cache time as the expiration
1170 $age = $now - $wrapped[self::FLD_TIME];
1171 $curTTL = min( -$age, self::TINY_NEGATIVE );
1172 } elseif ( $wrapped[self::FLD_TTL] > 0 ) {
1173 // Get the approximate time left on the key
1174 $age = $now - $wrapped[self::FLD_TIME];
1175 $curTTL = max( $wrapped[self::FLD_TTL] - $age, 0.0 );
1176 } else {
1177 // Key had no TTL, so the time left is unbounded
1178 $curTTL = INF;
1179 }
1180
1181 return [ $wrapped[self::FLD_VALUE], $curTTL ];
1182 }
1183
1184 /**
1185 * @param array $keys
1186 * @param string $prefix
1187 * @return string[]
1188 */
1189 protected static function prefixCacheKeys( array $keys, $prefix ) {
1190 $res = [];
1191 foreach ( $keys as $key ) {
1192 $res[] = $prefix . $key;
1193 }
1194
1195 return $res;
1196 }
1197
1198 /**
1199 * @param string $value Wrapped value like "PURGED:<timestamp>:<holdoff>"
1200 * @return array|bool Array containing a UNIX timestamp (float) and holdoff period (integer),
1201 * or false if value isn't a valid purge value
1202 */
1203 protected static function parsePurgeValue( $value ) {
1204 if ( !is_string( $value ) ) {
1205 return false;
1206 }
1207 $segments = explode( ':', $value, 3 );
1208 if ( !isset( $segments[0] ) || !isset( $segments[1] )
1209 || "{$segments[0]}:" !== self::PURGE_VAL_PREFIX
1210 ) {
1211 return false;
1212 }
1213 if ( !isset( $segments[2] ) ) {
1214 // Back-compat with old purge values without holdoff
1215 $segments[2] = self::HOLDOFF_TTL;
1216 }
1217 return [
1218 self::FLD_TIME => (float)$segments[1],
1219 self::FLD_HOLDOFF => (int)$segments[2],
1220 ];
1221 }
1222
1223 /**
1224 * @param float $timestamp
1225 * @param int $holdoff In seconds
1226 * @return string Wrapped purge value
1227 */
1228 protected function makePurgeValue( $timestamp, $holdoff ) {
1229 return self::PURGE_VAL_PREFIX . (float)$timestamp . ':' . (int)$holdoff;
1230 }
1231 }