23use Wikimedia\ObjectFactory;
68 parent::__construct( $params );
70 if ( empty( $params[
'caches'] ) || !is_array( $params[
'caches'] ) ) {
71 throw new InvalidArgumentException(
72 __METHOD__ .
': "caches" parameter must be an array of caches'
77 foreach ( $params[
'caches'] as $cacheInfo ) {
79 $this->caches[] = $cacheInfo;
81 if ( !isset( $cacheInfo[
'args'] ) ) {
86 $cacheInfo[
'args'] = [ $cacheInfo ];
88 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
93 $this->asyncWrites = (
94 isset( $params[
'replication'] ) &&
95 $params[
'replication'] ===
'async' &&
96 is_callable( $this->asyncHandler )
99 $this->cacheIndexes = array_keys( $this->caches );
103 parent::setDebug( $enabled );
104 foreach ( $this->caches as
$cache ) {
105 $cache->setDebug( $enabled );
109 public function get( $key, $flags = 0 ) {
114 return $this->caches[0]->get( $key, $flags );
119 foreach ( $this->caches as $i =>
$cache ) {
120 $value =
$cache->get( $key, $flags );
121 if ( $value !==
false ) {
138 [ $key, $value, self::$UPGRADE_TTL ]
145 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
154 public function delete( $key, $flags = 0 ) {
163 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
177 array_slice( $this->cacheIndexes, 1 ),
180 [ $key, $value, $exptime, $flags ]
187 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
196 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
205 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
207 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
212 return $this->caches[0]->unlock( $key );
217 callable $progress =
null,
221 foreach ( $this->caches as
$cache ) {
222 if (
$cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ) ) {
233 foreach (
$keys as $key ) {
234 $val = $this->
get( $key, $flags );
235 if ( $val !==
false ) {
243 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
270 public function incr( $key, $value = 1, $flags = 0 ) {
279 public function decr( $key, $value = 1, $flags = 0 ) {
288 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
298 return $this->caches[0]->getLastError();
302 $this->caches[0]->clearLastError();
317 if ( array_diff( $indexes, [ 0 ] ) &&
$asyncWrites && $method !==
'merge' ) {
323 foreach ( $indexes as $i ) {
324 $cache = $this->caches[$i];
327 if ( !
$cache->$method( ...$args ) ) {
335 if ( !
$cache->$method( ...$args ) ) {
336 $logger->warning(
"Async $method op failed" );
355 return $this->caches[0]->makeKeyInternal( $keyspace,
$args );
358 public function makeKey( $class, ...$components ) {
359 return $this->caches[0]->makeKey( ...func_get_args() );
363 return $this->caches[0]->makeGlobalKey( ...func_get_args() );
367 $this->caches[0]->addBusyCallback( $workCallback );
371 parent::setMockTime( $time );
372 foreach ( $this->caches as
$cache ) {
373 $cache->setMockTime( $time );
374 $cache->setMockTime( $time );
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.
fieldHasFlags( $field, $flags)
callable null $asyncHandler
A cache class that replicates all writes to multiple child caches.
setMulti(array $data, $exptime=0, $flags=0)
Batch insertion/replace.
usesAsyncWritesGivenFlags( $flags)
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple keys that exist.
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.
doWrite( $indexes, $asyncWrites, $method, array $args)
Apply a write method to the backing caches specified by $indexes (in order)
static int $UPGRADE_TTL
TTL when a key is copied to a higher cache tier.
int[] $cacheIndexes
List of all backing cache indexes.
makeGlobalKey( $class,... $components)
Make a global cache key.
getLastError()
Get the "last error" registered; clearLastError() should be called manually.
__construct( $params)
$params include:
decr( $key, $value=1, $flags=0)
Decrease stored value of $key by $value while preserving its TTL.
clearLastError()
Clear the "last error" registry.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
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)
bool $asyncWrites
Use async secondary writes.
makeKeyInternal( $keyspace, $args)
Construct a cache key.
lock( $key, $timeout=6, $expiry=6, $rclass='')
Acquire an advisory lock on a key string.
getMulti(array $keys, $flags=0)
Get an associative array containing the item for each of the keys that have items.
makeKey( $class,... $components)
Make a cache key, scoped to this instance's keyspace.
deleteObjectsExpiringBefore( $timestamp, callable $progress=null, $limit=INF)
Delete all objects expiring before a certain date.
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.
deleteMulti(array $data, $flags=0)
Batch deletion.