23use Wikimedia\ObjectFactory;
69 parent::__construct( $params );
71 if ( empty( $params[
'caches'] ) || !is_array( $params[
'caches'] ) ) {
72 throw new InvalidArgumentException(
73 __METHOD__ .
': "caches" parameter must be an array of caches'
78 foreach ( $params[
'caches'] as $cacheInfo ) {
80 $this->caches[] = $cacheInfo;
82 if ( !isset( $cacheInfo[
'args'] ) ) {
87 $cacheInfo[
'args'] = [ $cacheInfo ];
89 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
94 $this->asyncWrites = (
95 isset( $params[
'replication'] ) &&
96 $params[
'replication'] ===
'async' &&
97 is_callable( $this->asyncHandler )
100 $this->cacheIndexes = array_keys( $this->caches );
104 parent::setDebug( $enabled );
105 foreach ( $this->caches as
$cache ) {
106 $cache->setDebug( $enabled );
110 public function get( $key, $flags = 0 ) {
115 return $this->caches[0]->get( $key, $flags );
120 foreach ( $this->caches as $i =>
$cache ) {
121 $value =
$cache->get( $key, $flags );
122 if ( $value !==
false ) {
139 [ $key, $value, self::$UPGRADE_TTL ]
146 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
155 public function delete( $key, $flags = 0 ) {
164 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
178 array_slice( $this->cacheIndexes, 1 ),
181 [ $key, $value, $exptime, $flags ]
188 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
197 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
206 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
208 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
213 return $this->caches[0]->unlock( $key );
218 callable $progress =
null,
222 foreach ( $this->caches as
$cache ) {
223 if (
$cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ) ) {
234 foreach (
$keys as $key ) {
235 $val = $this->
get( $key, $flags );
236 if ( $val !==
false ) {
244 public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
271 public function incr( $key, $value = 1, $flags = 0 ) {
280 public function decr( $key, $value = 1, $flags = 0 ) {
289 public function incrWithInit( $key, $exptime, $value = 1, $init =
null, $flags = 0 ) {
299 return $this->caches[0]->getLastError();
303 $this->caches[0]->clearLastError();
318 if ( array_diff( $indexes, [ 0 ] ) &&
$asyncWrites && $method !==
'merge' ) {
324 foreach ( $indexes as $i ) {
325 $cache = $this->caches[$i];
328 if ( !
$cache->$method( ...$args ) ) {
336 if ( !
$cache->$method( ...$args ) ) {
337 $logger->warning(
"Async $method op failed" );
356 return $this->caches[0]->makeKeyInternal( $keyspace,
$args );
359 public function makeKey( $class, ...$components ) {
360 return $this->caches[0]->makeKey( ...func_get_args() );
364 return $this->caches[0]->makeGlobalKey( ...func_get_args() );
368 $this->caches[0]->addBusyCallback( $workCallback );
372 return $this->caches[0]->setNewPreparedValues( $valueByKey );
376 parent::setMockTime( $time );
377 foreach ( $this->caches as
$cache ) {
378 $cache->setMockTime( $time );
380 $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)
Stable to call.
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.
setNewPreparedValues(array $valueByKey)
Prepare values for storage and get their serialized sizes, or, estimate those sizes.
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.