3 * Copyright © 2003-2004 Brion Vibber <brion@pobox.com>
4 * https://www.mediawiki.org/
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
26 * @defgroup Cache Cache
29 use Psr\Log\LoggerAwareInterface
;
30 use Psr\Log\LoggerInterface
;
31 use Psr\Log\NullLogger
;
34 * interface is intended to be more or less compatible with
35 * the PHP memcached client.
37 * backends for local hash array and SQL table included:
39 * $bag = new HashBagOStuff();
40 * $bag = new SqlBagOStuff(); # connect to db first
45 abstract class BagOStuff
implements IExpiringStore
, LoggerAwareInterface
{
46 /** @var array[] Lock tracking */
47 protected $locks = [];
49 /** @var integer ERR_* class constant */
50 protected $lastError = self
::ERR_NONE
;
53 protected $keyspace = 'local';
55 /** @var LoggerInterface */
58 /** @var callback|null */
59 protected $asyncHandler;
62 private $debugMode = false;
65 private $duplicateKeyLookups = [];
68 private $reportDupes = false;
71 private $dupeTrackScheduled = false;
73 /** @var integer[] Map of (ATTR_* class constant => QOS_* class constant) */
74 protected $attrMap = [];
76 /** Possible values for getLastError() */
77 const ERR_NONE
= 0; // no error
78 const ERR_NO_RESPONSE
= 1; // no response
79 const ERR_UNREACHABLE
= 2; // can't connect
80 const ERR_UNEXPECTED
= 3; // response gave some error
82 /** Bitfield constants for get()/getMulti() */
83 const READ_LATEST
= 1; // use latest data for replicated stores
84 const READ_VERIFIED
= 2; // promise that caller can tell when keys are stale
85 /** Bitfield constants for set()/merge() */
86 const WRITE_SYNC
= 1; // synchronously write to all locations for replicated stores
87 const WRITE_CACHE_ONLY
= 2; // Only change state of the in-memory cache
91 * - logger: Psr\Log\LoggerInterface instance
92 * - keyspace: Default keyspace for $this->makeKey()
93 * - asyncHandler: Callable to use for scheduling tasks after the web request ends.
94 * In CLI mode, it should run the task immediately.
95 * - reportDupes: Whether to emit warning log messages for all keys that were
96 * requested more than once (requires an asyncHandler).
97 * @param array $params
99 public function __construct( array $params = [] ) {
100 if ( isset( $params['logger'] ) ) {
101 $this->setLogger( $params['logger'] );
103 $this->setLogger( new NullLogger() );
106 if ( isset( $params['keyspace'] ) ) {
107 $this->keyspace
= $params['keyspace'];
110 $this->asyncHandler
= isset( $params['asyncHandler'] )
111 ?
$params['asyncHandler']
114 if ( !empty( $params['reportDupes'] ) && is_callable( $this->asyncHandler
) ) {
115 $this->reportDupes
= true;
120 * @param LoggerInterface $logger
123 public function setLogger( LoggerInterface
$logger ) {
124 $this->logger
= $logger;
130 public function setDebug( $bool ) {
131 $this->debugMode
= $bool;
135 * Get an item with the given key, regenerating and setting it if not found
137 * If the callback returns false, then nothing is stored.
140 * @param int $ttl Time-to-live (seconds)
141 * @param callable $callback Callback that derives the new value
142 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
143 * @return mixed The cached value if found or the result of $callback otherwise
146 final public function getWithSetCallback( $key, $ttl, $callback, $flags = 0 ) {
147 $value = $this->get( $key, $flags );
149 if ( $value === false ) {
150 if ( !is_callable( $callback ) ) {
151 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
153 $value = call_user_func( $callback );
154 if ( $value !== false ) {
155 $this->set( $key, $value, $ttl );
163 * Get an item with the given key
165 * If the key includes a determistic input hash (e.g. the key can only have
166 * the correct value) or complete staleness checks are handled by the caller
167 * (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set.
168 * This lets tiered backends know they can safely upgrade a cached value to
169 * higher tiers using standard TTLs.
172 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
173 * @param integer $oldFlags [unused]
174 * @return mixed Returns false on failure and if the item does not exist
176 public function get( $key, $flags = 0, $oldFlags = null ) {
177 // B/C for ( $key, &$casToken = null, $flags = 0 )
178 $flags = is_int( $oldFlags ) ?
$oldFlags : $flags;
180 $this->trackDuplicateKeys( $key );
182 return $this->doGet( $key, $flags );
186 * Track the number of times that a given key has been used.
189 private function trackDuplicateKeys( $key ) {
190 if ( !$this->reportDupes
) {
194 if ( !isset( $this->duplicateKeyLookups
[$key] ) ) {
195 // Track that we have seen this key. This N-1 counting style allows
196 // easy filtering with array_filter() later.
197 $this->duplicateKeyLookups
[$key] = 0;
199 $this->duplicateKeyLookups
[$key] +
= 1;
201 if ( $this->dupeTrackScheduled
=== false ) {
202 $this->dupeTrackScheduled
= true;
203 // Schedule a callback that logs keys processed more than once by get().
204 call_user_func( $this->asyncHandler
, function () {
205 $dups = array_filter( $this->duplicateKeyLookups
);
206 foreach ( $dups as $key => $count ) {
207 $this->logger
->warning(
208 'Duplicate get(): "{key}" fetched {count} times',
209 // Count is N-1 of the actual lookup count
210 [ 'key' => $key, 'count' => $count +
1, ]
220 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
221 * @return mixed Returns false on failure and if the item does not exist
223 abstract protected function doGet( $key, $flags = 0 );
226 * @note: This method is only needed if merge() uses mergeViaCas()
229 * @param mixed $casToken
230 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
231 * @return mixed Returns false on failure and if the item does not exist
234 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
235 throw new Exception( __METHOD__
. ' not implemented.' );
242 * @param mixed $value
243 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
244 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
245 * @return bool Success
247 abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
253 * @return bool True if the item was deleted or not found, false on failure
255 abstract public function delete( $key );
258 * Merge changes into the existing cache value (possibly creating a new one)
260 * The callback function returns the new value given the current value
261 * (which will be false if not present), and takes the arguments:
262 * (this BagOStuff, cache key, current value, TTL).
263 * The TTL parameter is reference set to $exptime. It can be overriden in the callback.
266 * @param callable $callback Callback method to be executed
267 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
268 * @param int $attempts The amount of times to attempt a merge in case of failure
269 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
270 * @return bool Success
271 * @throws InvalidArgumentException
273 public function merge( $key, callable
$callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
274 return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
278 * @see BagOStuff::merge()
281 * @param callable $callback Callback method to be executed
282 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
283 * @param int $attempts The amount of times to attempt a merge in case of failure
284 * @return bool Success
286 protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
288 $this->clearLastError();
289 $reportDupes = $this->reportDupes
;
290 $this->reportDupes
= false;
291 $casToken = null; // passed by reference
292 $currentValue = $this->getWithToken( $key, $casToken, self
::READ_LATEST
);
293 $this->reportDupes
= $reportDupes;
295 if ( $this->getLastError() ) {
296 return false; // don't spam retries (retry only on races)
299 // Derive the new value from the old value
300 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
302 $this->clearLastError();
303 if ( $value === false ) {
304 $success = true; // do nothing
305 } elseif ( $currentValue === false ) {
306 // Try to create the key, failing if it gets created in the meantime
307 $success = $this->add( $key, $value, $exptime );
309 // Try to update the key, failing if it gets changed in the meantime
310 $success = $this->cas( $casToken, $key, $value, $exptime );
312 if ( $this->getLastError() ) {
313 return false; // IO error; don't spam retries
315 } while ( !$success && --$attempts );
321 * Check and set an item
323 * @param mixed $casToken
325 * @param mixed $value
326 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
327 * @return bool Success
330 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
331 throw new Exception( "CAS is not implemented in " . __CLASS__
);
335 * @see BagOStuff::merge()
338 * @param callable $callback Callback method to be executed
339 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
340 * @param int $attempts The amount of times to attempt a merge in case of failure
341 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
342 * @return bool Success
344 protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
345 if ( !$this->lock( $key, 6 ) ) {
349 $this->clearLastError();
350 $reportDupes = $this->reportDupes
;
351 $this->reportDupes
= false;
352 $currentValue = $this->get( $key, self
::READ_LATEST
);
353 $this->reportDupes
= $reportDupes;
355 if ( $this->getLastError() ) {
358 // Derive the new value from the old value
359 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
360 if ( $value === false ) {
361 $success = true; // do nothing
363 $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
367 if ( !$this->unlock( $key ) ) {
368 // this should never happen
369 trigger_error( "Could not release lock for key '$key'." );
376 * Acquire an advisory lock on a key string
378 * Note that if reentry is enabled, duplicate calls ignore $expiry
381 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
382 * @param int $expiry Lock expiry [optional]; 1 day maximum
383 * @param string $rclass Allow reentry if set and the current lock used this value
384 * @return bool Success
386 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
387 // Avoid deadlocks and allow lock reentry if specified
388 if ( isset( $this->locks
[$key] ) ) {
389 if ( $rclass != '' && $this->locks
[$key]['class'] === $rclass ) {
390 ++
$this->locks
[$key]['depth'];
397 $expiry = min( $expiry ?
: INF
, self
::TTL_DAY
);
399 $this->clearLastError();
400 $timestamp = microtime( true ); // starting UNIX timestamp
401 if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
403 } elseif ( $this->getLastError() ||
$timeout <= 0 ) {
404 $locked = false; // network partition or non-blocking
406 // Estimate the RTT (us); use 1ms minimum for sanity
407 $uRTT = max( 1e3
, ceil( 1e6
* ( microtime( true ) - $timestamp ) ) );
408 $sleep = 2 * $uRTT; // rough time to do get()+set()
410 $attempts = 0; // failed attempts
412 if ( ++
$attempts >= 3 && $sleep <= 5e5
) {
413 // Exponentially back off after failed attempts to avoid network spam.
414 // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
417 usleep( $sleep ); // back off
418 $this->clearLastError();
419 $locked = $this->add( "{$key}:lock", 1, $expiry );
420 if ( $this->getLastError() ) {
421 $locked = false; // network partition
424 } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
428 $this->locks
[$key] = [ 'class' => $rclass, 'depth' => 1 ];
435 * Release an advisory lock on a key string
438 * @return bool Success
440 public function unlock( $key ) {
441 if ( isset( $this->locks
[$key] ) && --$this->locks
[$key]['depth'] <= 0 ) {
442 unset( $this->locks
[$key] );
444 return $this->delete( "{$key}:lock" );
451 * Get a lightweight exclusive self-unlocking lock
453 * Note that the same lock cannot be acquired twice.
455 * This is useful for task de-duplication or to avoid obtrusive
456 * (though non-corrupting) DB errors like INSERT key conflicts
457 * or deadlocks when using LOCK IN SHARE MODE.
460 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
461 * @param int $expiry Lock expiry [optional]; 1 day maximum
462 * @param string $rclass Allow reentry if set and the current lock used this value
463 * @return ScopedCallback|null Returns null on failure
466 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
467 $expiry = min( $expiry ?
: INF
, self
::TTL_DAY
);
469 if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
473 $lSince = microtime( true ); // lock timestamp
475 return new ScopedCallback( function() use ( $key, $lSince, $expiry ) {
476 $latency = .050; // latency skew (err towards keeping lock present)
477 $age = ( microtime( true ) - $lSince +
$latency );
478 if ( ( $age +
$latency ) >= $expiry ) {
479 $this->logger
->warning( "Lock for $key held too long ($age sec)." );
480 return; // expired; it's not "safe" to delete the key
482 $this->unlock( $key );
487 * Delete all objects expiring before a certain date.
488 * @param string $date The reference date in MW format
489 * @param callable|bool $progressCallback Optional, a function which will be called
490 * regularly during long-running operations with the percentage progress
491 * as the first parameter.
493 * @return bool Success, false if unimplemented
495 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
501 * Get an associative array containing the item for each of the keys that have items.
502 * @param array $keys List of strings
503 * @param integer $flags Bitfield; supports READ_LATEST [optional]
506 public function getMulti( array $keys, $flags = 0 ) {
508 foreach ( $keys as $key ) {
509 $val = $this->get( $key );
510 if ( $val !== false ) {
519 * @param array $data $key => $value assoc array
520 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
521 * @return bool Success
524 public function setMulti( array $data, $exptime = 0 ) {
526 foreach ( $data as $key => $value ) {
527 if ( !$this->set( $key, $value, $exptime ) ) {
536 * @param mixed $value
537 * @param int $exptime
538 * @return bool Success
540 public function add( $key, $value, $exptime = 0 ) {
541 if ( $this->get( $key ) === false ) {
542 return $this->set( $key, $value, $exptime );
544 return false; // key already set
548 * Increase stored value of $key by $value while preserving its TTL
549 * @param string $key Key to increase
550 * @param int $value Value to add to $key (Default 1)
551 * @return int|bool New value or false on failure
553 public function incr( $key, $value = 1 ) {
554 if ( !$this->lock( $key ) ) {
557 $n = $this->get( $key );
558 if ( $this->isInteger( $n ) ) { // key exists?
559 $n +
= intval( $value );
560 $this->set( $key, max( 0, $n ) ); // exptime?
564 $this->unlock( $key );
570 * Decrease stored value of $key by $value while preserving its TTL
573 * @return int|bool New value or false on failure
575 public function decr( $key, $value = 1 ) {
576 return $this->incr( $key, - $value );
580 * Increase stored value of $key by $value while preserving its TTL
582 * This will create the key with value $init and TTL $ttl instead if not present
588 * @return int|bool New value or false on failure
591 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
592 $newValue = $this->incr( $key, $value );
593 if ( $newValue === false ) {
594 // No key set; initialize
595 $newValue = $this->add( $key, (int)$init, $ttl ) ?
$init : false;
597 if ( $newValue === false ) {
598 // Raced out initializing; increment
599 $newValue = $this->incr( $key, $value );
606 * Get the "last error" registered; clearLastError() should be called manually
607 * @return int ERR_* constant for the "last error" registry
610 public function getLastError() {
611 return $this->lastError
;
615 * Clear the "last error" registry
618 public function clearLastError() {
619 $this->lastError
= self
::ERR_NONE
;
623 * Set the "last error" registry
624 * @param int $err ERR_* constant
627 protected function setLastError( $err ) {
628 $this->lastError
= $err;
632 * Modify a cache update operation array for EventRelayer::notify()
634 * This is used for relayed writes, e.g. for broadcasting a change
635 * to multiple data-centers. If the array contains a 'val' field
636 * then the command involves setting a key to that value. Note that
637 * for simplicity, 'val' is always a simple scalar value. This method
638 * is used to possibly serialize the value and add any cache-specific
639 * key/values needed for the relayer daemon (e.g. memcached flags).
641 * @param array $event
645 public function modifySimpleRelayEvent( array $event ) {
650 * @param string $text
652 protected function debug( $text ) {
653 if ( $this->debugMode
) {
654 $this->logger
->debug( "{class} debug: $text", [
655 'class' => get_class( $this ),
661 * Convert an optionally relative time to an absolute time
662 * @param int $exptime
665 protected function convertExpiry( $exptime ) {
666 if ( $exptime != 0 && $exptime < ( 10 * self
::TTL_YEAR
) ) {
667 return time() +
$exptime;
674 * Convert an optionally absolute expiry time to a relative time. If an
675 * absolute time is specified which is in the past, use a short expiry time.
677 * @param int $exptime
680 protected function convertToRelative( $exptime ) {
681 if ( $exptime >= ( 10 * self
::TTL_YEAR
) ) {
683 if ( $exptime <= 0 ) {
693 * Check if a value is an integer
695 * @param mixed $value
698 protected function isInteger( $value ) {
699 return ( is_int( $value ) ||
ctype_digit( $value ) );
703 * Construct a cache key.
706 * @param string $keyspace
710 public function makeKeyInternal( $keyspace, $args ) {
712 foreach ( $args as $arg ) {
713 $arg = str_replace( ':', '%3A', $arg );
714 $key = $key . ':' . $arg;
716 return strtr( $key, ' ', '_' );
720 * Make a global cache key.
723 * @param string ... Key component (variadic)
726 public function makeGlobalKey() {
727 return $this->makeKeyInternal( 'global', func_get_args() );
731 * Make a cache key, scoped to this instance's keyspace.
734 * @param string ... Key component (variadic)
737 public function makeKey() {
738 return $this->makeKeyInternal( $this->keyspace
, func_get_args() );
742 * @param integer $flag ATTR_* class constant
743 * @return integer QOS_* class constant
746 public function getQoS( $flag ) {
747 return isset( $this->attrMap
[$flag] ) ?
$this->attrMap
[$flag] : self
::QOS_UNKNOWN
;
751 * Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map
753 * @param BagOStuff[] $bags
754 * @return integer[] Resulting flag map (class ATTR_* constant => class QOS_* constant)
756 protected function mergeFlagMaps( array $bags ) {
758 foreach ( $bags as $bag ) {
759 foreach ( $bag->attrMap
as $attr => $rank ) {
760 if ( isset( $map[$attr] ) ) {
761 $map[$attr] = min( $map[$attr], $rank );