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 = [];
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 /** Possible values for getLastError() */
74 const ERR_NONE
= 0; // no error
75 const ERR_NO_RESPONSE
= 1; // no response
76 const ERR_UNREACHABLE
= 2; // can't connect
77 const ERR_UNEXPECTED
= 3; // response gave some error
79 /** Bitfield constants for get()/getMulti() */
80 const READ_LATEST
= 1; // use latest data for replicated stores
81 const READ_VERIFIED
= 2; // promise that caller can tell when keys are stale
82 /** Bitfield constants for set()/merge() */
83 const WRITE_SYNC
= 1; // synchronously write to all locations for replicated stores
84 const WRITE_CACHE_ONLY
= 2; // Only change state of the in-memory cache
88 * - logger: Psr\Log\LoggerInterface instance
89 * - keyspace: Default keyspace for $this->makeKey()
90 * - asyncHandler: Callable to use for scheduling tasks after the web request ends.
91 * In CLI mode, it should run the task immediately.
92 * - reportDupes: Whether to emit warning log messages for all keys that were
93 * requested more than once (requires an asyncHandler).
94 * @param array $params
96 public function __construct( array $params = [] ) {
97 if ( isset( $params['logger'] ) ) {
98 $this->setLogger( $params['logger'] );
100 $this->setLogger( new NullLogger() );
103 if ( isset( $params['keyspace'] ) ) {
104 $this->keyspace
= $params['keyspace'];
107 $this->asyncHandler
= isset( $params['asyncHandler'] )
108 ?
$params['asyncHandler']
111 if ( !empty( $params['reportDupes'] ) && is_callable( $this->asyncHandler
) ) {
112 $this->reportDupes
= true;
117 * @param LoggerInterface $logger
120 public function setLogger( LoggerInterface
$logger ) {
121 $this->logger
= $logger;
127 public function setDebug( $bool ) {
128 $this->debugMode
= $bool;
132 * Get an item with the given key, regenerating and setting it if not found
134 * If the callback returns false, then nothing is stored.
137 * @param int $ttl Time-to-live (seconds)
138 * @param callable $callback Callback that derives the new value
139 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
140 * @return mixed The cached value if found or the result of $callback otherwise
143 final public function getWithSetCallback( $key, $ttl, $callback, $flags = 0 ) {
144 $value = $this->get( $key, $flags );
146 if ( $value === false ) {
147 if ( !is_callable( $callback ) ) {
148 throw new InvalidArgumentException( "Invalid cache miss callback provided." );
150 $value = call_user_func( $callback );
151 if ( $value !== false ) {
152 $this->set( $key, $value, $ttl );
160 * Get an item with the given key
162 * If the key includes a determistic input hash (e.g. the key can only have
163 * the correct value) or complete staleness checks are handled by the caller
164 * (e.g. nothing relies on the TTL), then the READ_VERIFIED flag should be set.
165 * This lets tiered backends know they can safely upgrade a cached value to
166 * higher tiers using standard TTLs.
169 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
170 * @param integer $oldFlags [unused]
171 * @return mixed Returns false on failure and if the item does not exist
173 public function get( $key, $flags = 0, $oldFlags = null ) {
174 // B/C for ( $key, &$casToken = null, $flags = 0 )
175 $flags = is_int( $oldFlags ) ?
$oldFlags : $flags;
177 $this->trackDuplicateKeys( $key );
179 return $this->doGet( $key, $flags );
183 * Track the number of times that a given key has been used.
186 private function trackDuplicateKeys( $key ) {
187 if ( !$this->reportDupes
) {
191 if ( !isset( $this->duplicateKeyLookups
[$key] ) ) {
192 // Track that we have seen this key. This N-1 counting style allows
193 // easy filtering with array_filter() later.
194 $this->duplicateKeyLookups
[$key] = 0;
196 $this->duplicateKeyLookups
[$key] +
= 1;
198 if ( $this->dupeTrackScheduled
=== false ) {
199 $this->dupeTrackScheduled
= true;
200 // Schedule a callback that logs keys processed more than once by get().
201 call_user_func( $this->asyncHandler
, function () {
202 $dups = array_filter( $this->duplicateKeyLookups
);
203 foreach ( $dups as $key => $count ) {
204 $this->logger
->warning(
205 'Duplicate get(): "{key}" fetched {count} times',
206 // Count is N-1 of the actual lookup count
207 [ 'key' => $key, 'count' => $count +
1, ]
217 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
218 * @return mixed Returns false on failure and if the item does not exist
220 abstract protected function doGet( $key, $flags = 0 );
223 * @note: This method is only needed if merge() uses mergeViaCas()
226 * @param mixed $casToken
227 * @param integer $flags Bitfield of BagOStuff::READ_* constants [optional]
228 * @return mixed Returns false on failure and if the item does not exist
231 protected function getWithToken( $key, &$casToken, $flags = 0 ) {
232 throw new Exception( __METHOD__
. ' not implemented.' );
239 * @param mixed $value
240 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
241 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
242 * @return bool Success
244 abstract public function set( $key, $value, $exptime = 0, $flags = 0 );
250 * @return bool True if the item was deleted or not found, false on failure
252 abstract public function delete( $key );
255 * Merge changes into the existing cache value (possibly creating a new one)
257 * The callback function returns the new value given the current value
258 * (which will be false if not present), and takes the arguments:
259 * (this BagOStuff, cache key, current value, TTL).
260 * The TTL parameter is reference set to $exptime. It can be overriden in the callback.
263 * @param callable $callback Callback method to be executed
264 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
265 * @param int $attempts The amount of times to attempt a merge in case of failure
266 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
267 * @return bool Success
268 * @throws InvalidArgumentException
270 public function merge( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
271 if ( !is_callable( $callback ) ) {
272 throw new InvalidArgumentException( "Got invalid callback." );
275 return $this->mergeViaLock( $key, $callback, $exptime, $attempts, $flags );
279 * @see BagOStuff::merge()
282 * @param callable $callback Callback method to be executed
283 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
284 * @param int $attempts The amount of times to attempt a merge in case of failure
285 * @return bool Success
287 protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10 ) {
289 $this->clearLastError();
290 $reportDupes = $this->reportDupes
;
291 $this->reportDupes
= false;
292 $casToken = null; // passed by reference
293 $currentValue = $this->getWithToken( $key, $casToken, self
::READ_LATEST
);
294 $this->reportDupes
= $reportDupes;
296 if ( $this->getLastError() ) {
297 return false; // don't spam retries (retry only on races)
300 // Derive the new value from the old value
301 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
303 $this->clearLastError();
304 if ( $value === false ) {
305 $success = true; // do nothing
306 } elseif ( $currentValue === false ) {
307 // Try to create the key, failing if it gets created in the meantime
308 $success = $this->add( $key, $value, $exptime );
310 // Try to update the key, failing if it gets changed in the meantime
311 $success = $this->cas( $casToken, $key, $value, $exptime );
313 if ( $this->getLastError() ) {
314 return false; // IO error; don't spam retries
316 } while ( !$success && --$attempts );
322 * Check and set an item
324 * @param mixed $casToken
326 * @param mixed $value
327 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
328 * @return bool Success
331 protected function cas( $casToken, $key, $value, $exptime = 0 ) {
332 throw new Exception( "CAS is not implemented in " . __CLASS__
);
336 * @see BagOStuff::merge()
339 * @param callable $callback Callback method to be executed
340 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
341 * @param int $attempts The amount of times to attempt a merge in case of failure
342 * @param int $flags Bitfield of BagOStuff::WRITE_* constants
343 * @return bool Success
345 protected function mergeViaLock( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
346 if ( !$this->lock( $key, 6 ) ) {
350 $this->clearLastError();
351 $reportDupes = $this->reportDupes
;
352 $this->reportDupes
= false;
353 $currentValue = $this->get( $key, self
::READ_LATEST
);
354 $this->reportDupes
= $reportDupes;
356 if ( $this->getLastError() ) {
359 // Derive the new value from the old value
360 $value = call_user_func( $callback, $this, $key, $currentValue, $exptime );
361 if ( $value === false ) {
362 $success = true; // do nothing
364 $success = $this->set( $key, $value, $exptime, $flags ); // set the new value
368 if ( !$this->unlock( $key ) ) {
369 // this should never happen
370 trigger_error( "Could not release lock for key '$key'." );
377 * Acquire an advisory lock on a key string
379 * Note that if reentry is enabled, duplicate calls ignore $expiry
382 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
383 * @param int $expiry Lock expiry [optional]; 1 day maximum
384 * @param string $rclass Allow reentry if set and the current lock used this value
385 * @return bool Success
387 public function lock( $key, $timeout = 6, $expiry = 6, $rclass = '' ) {
388 // Avoid deadlocks and allow lock reentry if specified
389 if ( isset( $this->locks
[$key] ) ) {
390 if ( $rclass != '' && $this->locks
[$key]['class'] === $rclass ) {
391 ++
$this->locks
[$key]['depth'];
398 $expiry = min( $expiry ?
: INF
, self
::TTL_DAY
);
400 $this->clearLastError();
401 $timestamp = microtime( true ); // starting UNIX timestamp
402 if ( $this->add( "{$key}:lock", 1, $expiry ) ) {
404 } elseif ( $this->getLastError() ||
$timeout <= 0 ) {
405 $locked = false; // network partition or non-blocking
407 // Estimate the RTT (us); use 1ms minimum for sanity
408 $uRTT = max( 1e3
, ceil( 1e6
* ( microtime( true ) - $timestamp ) ) );
409 $sleep = 2 * $uRTT; // rough time to do get()+set()
411 $attempts = 0; // failed attempts
413 if ( ++
$attempts >= 3 && $sleep <= 5e5
) {
414 // Exponentially back off after failed attempts to avoid network spam.
415 // About 2*$uRTT*(2^n-1) us of "sleep" happen for the next n attempts.
418 usleep( $sleep ); // back off
419 $this->clearLastError();
420 $locked = $this->add( "{$key}:lock", 1, $expiry );
421 if ( $this->getLastError() ) {
422 $locked = false; // network partition
425 } while ( !$locked && ( microtime( true ) - $timestamp ) < $timeout );
429 $this->locks
[$key] = [ 'class' => $rclass, 'depth' => 1 ];
436 * Release an advisory lock on a key string
439 * @return bool Success
441 public function unlock( $key ) {
442 if ( isset( $this->locks
[$key] ) && --$this->locks
[$key]['depth'] <= 0 ) {
443 unset( $this->locks
[$key] );
445 return $this->delete( "{$key}:lock" );
452 * Get a lightweight exclusive self-unlocking lock
454 * Note that the same lock cannot be acquired twice.
456 * This is useful for task de-duplication or to avoid obtrusive
457 * (though non-corrupting) DB errors like INSERT key conflicts
458 * or deadlocks when using LOCK IN SHARE MODE.
461 * @param int $timeout Lock wait timeout; 0 for non-blocking [optional]
462 * @param int $expiry Lock expiry [optional]; 1 day maximum
463 * @param string $rclass Allow reentry if set and the current lock used this value
464 * @return ScopedCallback|null Returns null on failure
467 final public function getScopedLock( $key, $timeout = 6, $expiry = 30, $rclass = '' ) {
468 $expiry = min( $expiry ?
: INF
, self
::TTL_DAY
);
470 if ( !$this->lock( $key, $timeout, $expiry, $rclass ) ) {
474 $lSince = microtime( true ); // lock timestamp
476 return new ScopedCallback( function() use ( $key, $lSince, $expiry ) {
477 $latency = .050; // latency skew (err towards keeping lock present)
478 $age = ( microtime( true ) - $lSince +
$latency );
479 if ( ( $age +
$latency ) >= $expiry ) {
480 $this->logger
->warning( "Lock for $key held too long ($age sec)." );
481 return; // expired; it's not "safe" to delete the key
483 $this->unlock( $key );
488 * Delete all objects expiring before a certain date.
489 * @param string $date The reference date in MW format
490 * @param callable|bool $progressCallback Optional, a function which will be called
491 * regularly during long-running operations with the percentage progress
492 * as the first parameter.
494 * @return bool Success, false if unimplemented
496 public function deleteObjectsExpiringBefore( $date, $progressCallback = false ) {
502 * Get an associative array containing the item for each of the keys that have items.
503 * @param array $keys List of strings
504 * @param integer $flags Bitfield; supports READ_LATEST [optional]
507 public function getMulti( array $keys, $flags = 0 ) {
509 foreach ( $keys as $key ) {
510 $val = $this->get( $key );
511 if ( $val !== false ) {
520 * @param array $data $key => $value assoc array
521 * @param int $exptime Either an interval in seconds or a unix timestamp for expiry
522 * @return bool Success
525 public function setMulti( array $data, $exptime = 0 ) {
527 foreach ( $data as $key => $value ) {
528 if ( !$this->set( $key, $value, $exptime ) ) {
537 * @param mixed $value
538 * @param int $exptime
539 * @return bool Success
541 public function add( $key, $value, $exptime = 0 ) {
542 if ( $this->get( $key ) === false ) {
543 return $this->set( $key, $value, $exptime );
545 return false; // key already set
549 * Increase stored value of $key by $value while preserving its TTL
550 * @param string $key Key to increase
551 * @param int $value Value to add to $key (Default 1)
552 * @return int|bool New value or false on failure
554 public function incr( $key, $value = 1 ) {
555 if ( !$this->lock( $key ) ) {
558 $n = $this->get( $key );
559 if ( $this->isInteger( $n ) ) { // key exists?
560 $n +
= intval( $value );
561 $this->set( $key, max( 0, $n ) ); // exptime?
565 $this->unlock( $key );
571 * Decrease stored value of $key by $value while preserving its TTL
574 * @return int|bool New value or false on failure
576 public function decr( $key, $value = 1 ) {
577 return $this->incr( $key, - $value );
581 * Increase stored value of $key by $value while preserving its TTL
583 * This will create the key with value $init and TTL $ttl instead if not present
589 * @return int|bool New value or false on failure
592 public function incrWithInit( $key, $ttl, $value = 1, $init = 1 ) {
593 $newValue = $this->incr( $key, $value );
594 if ( $newValue === false ) {
595 // No key set; initialize
596 $newValue = $this->add( $key, (int)$init, $ttl ) ?
$init : false;
598 if ( $newValue === false ) {
599 // Raced out initializing; increment
600 $newValue = $this->incr( $key, $value );
607 * Get the "last error" registered; clearLastError() should be called manually
608 * @return int ERR_* constant for the "last error" registry
611 public function getLastError() {
612 return $this->lastError
;
616 * Clear the "last error" registry
619 public function clearLastError() {
620 $this->lastError
= self
::ERR_NONE
;
624 * Set the "last error" registry
625 * @param int $err ERR_* constant
628 protected function setLastError( $err ) {
629 $this->lastError
= $err;
633 * Modify a cache update operation array for EventRelayer::notify()
635 * This is used for relayed writes, e.g. for broadcasting a change
636 * to multiple data-centers. If the array contains a 'val' field
637 * then the command involves setting a key to that value. Note that
638 * for simplicity, 'val' is always a simple scalar value. This method
639 * is used to possibly serialize the value and add any cache-specific
640 * key/values needed for the relayer daemon (e.g. memcached flags).
642 * @param array $event
646 public function modifySimpleRelayEvent( array $event ) {
651 * @param string $text
653 protected function debug( $text ) {
654 if ( $this->debugMode
) {
655 $this->logger
->debug( "{class} debug: $text", [
656 'class' => get_class( $this ),
662 * Convert an optionally relative time to an absolute time
663 * @param int $exptime
666 protected function convertExpiry( $exptime ) {
667 if ( $exptime != 0 && $exptime < ( 10 * self
::TTL_YEAR
) ) {
668 return time() +
$exptime;
675 * Convert an optionally absolute expiry time to a relative time. If an
676 * absolute time is specified which is in the past, use a short expiry time.
678 * @param int $exptime
681 protected function convertToRelative( $exptime ) {
682 if ( $exptime >= ( 10 * self
::TTL_YEAR
) ) {
684 if ( $exptime <= 0 ) {
694 * Check if a value is an integer
696 * @param mixed $value
699 protected function isInteger( $value ) {
700 return ( is_int( $value ) ||
ctype_digit( $value ) );
704 * Construct a cache key.
707 * @param string $keyspace
711 public function makeKeyInternal( $keyspace, $args ) {
713 foreach ( $args as $arg ) {
714 $arg = str_replace( ':', '%3A', $arg );
715 $key = $key . ':' . $arg;
717 return strtr( $key, ' ', '_' );
721 * Make a global cache key.
724 * @param string ... Key component (variadic)
727 public function makeGlobalKey() {
728 return $this->makeKeyInternal( 'global', func_get_args() );
732 * Make a cache key, scoped to this instance's keyspace.
735 * @param string ... Key component (variadic)
738 public function makeKey() {
739 return $this->makeKeyInternal( $this->keyspace
, func_get_args() );