23use Wikimedia\ObjectFactory;
70 parent::__construct( $params );
72 if ( empty( $params[
'caches'] ) || !is_array( $params[
'caches'] ) ) {
73 throw new InvalidArgumentException(
74 __METHOD__ .
': "caches" parameter must be an array of caches'
79 foreach ( $params[
'caches'] as $cacheInfo ) {
81 $this->caches[] = $cacheInfo;
83 if ( !isset( $cacheInfo[
'args'] ) ) {
88 $cacheInfo[
'args'] = [ $cacheInfo ];
93 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
99 $this->asyncWrites = (
100 isset( $params[
'replication'] ) &&
101 $params[
'replication'] ===
'async' &&
102 is_callable( $this->asyncHandler )
105 $this->cacheIndexes = array_keys( $this->caches );
108 public function get( $key, $flags = 0 ) {
109 $args = func_get_args();
126 foreach ( $this->cacheIndexes as $i ) {
134 if ( $value !==
false ) {
152 [ $key, $value, self::$UPGRADE_TTL ]
159 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
170 public function delete( $key, $flags = 0 ) {
181 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
196 array_slice( $this->cacheIndexes, 1 ),
201 [ $key, $value, $exptime, $flags ]
203 if ( $okSecondaries ===
false ) {
211 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
222 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
233 public function lock( $key, $timeout = 6, $exptime = 6, $rclass =
'' ) {
257 callable $progress =
null,
262 foreach ( $this->caches as
$cache ) {
263 if (
$cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) {
274 foreach (
$keys as $key ) {
275 $val = $this->
get( $key, $flags );
276 if ( $val !==
false ) {
284 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
317 public function incr( $key, $value = 1, $flags = 0 ) {
328 public function decr( $key, $value = 1, $flags = 0 ) {
339 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
351 foreach ( $this->caches as
$cache ) {
352 $error =
$cache->getLastError();
353 if ( $error !== self::ERR_NONE ) {
362 foreach ( $this->caches as
$cache ) {
371 public function makeKey( $collection, ...$components ) {
384 $this->caches[0]->addBusyCallback( $workCallback );
398 parent::setMockTime( $time );
399 foreach ( $this->caches as
$cache ) {
400 $cache->setMockTime( $time );
415 return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig,
$args );
439 if ( $asyncSecondary && array_diff( $indexes, [ 0 ] ) && $method !==
'merge' ) {
445 foreach ( $indexes as $i ) {
446 $cache = $this->caches[$i];
448 if ( $i == 0 || !$asyncSecondary ) {
450 $storeRes =
$cache->proxyCall( $method, $arg0Sig, $resSig,
$args );
451 if ( $storeRes ===
false ) {
453 } elseif (
$res ===
null ) {
459 static function () use (
$cache, $method, $arg0Sig, $resSig,
$args ) {
460 $cache->proxyCall( $method, $arg0Sig, $resSig,
$args );
unserialize( $serialized)
Class representing a cache/ephemeral data store.
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
genericKeyFromComponents(... $components)
At a minimum, there must be a keyspace and collection name component.
string $keyspace
Default keyspace; used by makeKey()
fieldHasFlags( $field, $flags)
callable null $asyncHandler
A cache class that replicates all writes to multiple child caches.
deleteMulti(array $keys, $flags=0)
Batch deletion.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
makeKey( $collection,... $components)
Make a cache key for the global keyspace and given components.
incrWithInit( $key, $exptime, $value=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise.
static int $UPGRADE_TTL
TTL when a key is copied to a higher cache tier.
int[] $cacheIndexes
List of all backing cache indexes.
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
clearLastError()
Clear the "last error" registry.
callKeyWriteMethodOnTierCaches(array $indexes, $asyncSecondary, $method, $arg0Sig, $resSig, array $args)
Call a write method on the cache instances, in order, for the given tiers (indexes)
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
makeGlobalKey( $collection,... $components)
Make a cache key for the default keyspace and given components.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on a key if it exists.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF, string $tag=null)
Delete all objects expiring before a certain date.
BagOStuff[] $caches
Backing cache stores in order of highest to lowest tier.
bool $asyncWrites
Use async secondary writes.
setMulti(array $valueByKey, $exptime=0, $flags=0)
Batch insertion/replace.
lock( $key, $timeout=6, $exptime=6, $rclass='')
Acquire an advisory lock on a key string, exclusive to the caller.
convertGenericKey( $key)
Convert a "generic" reversible cache key into one for this cache.
makeKeyInternal( $keyspace, $components)
Make a cache key for the given keyspace and components.
setNewPreparedValues(array $valueByKey)
Make a "generic" reversible cache key from the given components.
useAsyncSecondaryWrites( $flags)
callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args)
Call a method on the cache instance for the given cache tier (index)
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
incr( $key, $value=1, $flags=0)
Increase stored value of $key by $value while preserving its TTL.
addBusyCallback(callable $workCallback)
Let a callback be run to avoid wasting time on special blocking calls.
unlock( $key)
Release an advisory lock on a key string.