23use Wikimedia\ObjectFactory\ObjectFactory;
46 private static $UPGRADE_TTL = 3600;
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 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
89 $this->asyncWrites = (
90 isset( $params[
'replication'] ) &&
91 $params[
'replication'] ===
'async' &&
92 is_callable( $this->asyncHandler )
95 $this->cacheIndexes = array_keys( $this->caches );
98 public function get( $key, $flags = 0 ) {
99 $args = func_get_args();
105 return $this->callKeyMethodOnTierCache(
117 foreach ( $this->cacheIndexes as $i ) {
118 $value = $this->callKeyMethodOnTierCache(
125 if ( $value !==
false ) {
137 $this->callKeyWriteMethodOnTierCaches(
142 [ $key, $value, self::$UPGRADE_TTL ]
149 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
150 return $this->callKeyWriteMethodOnTierCaches(
159 public function delete( $key, $flags = 0 ) {
160 return $this->callKeyWriteMethodOnTierCaches(
169 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
171 $ok = $this->callKeyMethodOnTierCache(
183 $okSecondaries = $this->callKeyWriteMethodOnTierCaches(
184 array_slice( $this->cacheIndexes, 1 ),
188 [ $key, $value, $exptime, $flags ]
190 if ( $okSecondaries ===
false ) {
198 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
199 return $this->callKeyWriteMethodOnTierCaches(
208 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
209 return $this->callKeyWriteMethodOnTierCaches(
218 public function lock( $key, $timeout = 6, $exptime = 6, $rclass =
'' ) {
220 return $this->callKeyMethodOnTierCache(
231 return $this->callKeyMethodOnTierCache(
242 callable $progress =
null,
247 foreach ( $this->caches as $cache ) {
248 if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) {
259 foreach (
$keys as $key ) {
260 $val = $this->
get( $key, $flags );
261 if ( $val !==
false ) {
269 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
270 return $this->callKeyWriteMethodOnTierCaches(
280 return $this->callKeyWriteMethodOnTierCaches(
290 return $this->callKeyWriteMethodOnTierCaches(
299 public function incrWithInit( $key, $exptime, $step = 1, $init =
null, $flags = 0 ) {
300 return $this->callKeyWriteMethodOnTierCaches(
313 public function makeKey( $collection, ...$components ) {
327 $this->caches[0]->addBusyCallback( $workCallback );
331 parent::setMockTime( $time );
332 foreach ( $this->caches as $cache ) {
333 $cache->setMockTime( $time );
347 private function callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args ) {
348 return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig, $args, $this );
361 private function callKeyWriteMethodOnTierCaches(
370 if ( $this->asyncWrites && array_diff( $indexes, [ 0 ] ) && $method !==
'merge' ) {
373 $args = unserialize( serialize( $args ) );
376 foreach ( $indexes as $i ) {
377 $cache = $this->caches[$i];
379 if ( $i == 0 || !$this->asyncWrites ) {
381 $storeRes = $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
382 if ( $storeRes ===
false ) {
384 } elseif (
$res ===
null ) {
391 function () use ( $cache, $method, $arg0Sig, $resSig, $args ) {
392 $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
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)
Delete a batch of items.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple items.
makeKey( $collection,... $components)
Make a cache key for the global keyspace and given components.
incrWithInit( $key, $exptime, $step=1, $init=null, $flags=0)
Increase the value of the given key (no TTL change) if it exists or create it otherwise.
int[] $cacheIndexes
List of all backing cache 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 an item.
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)
Set a batch of items.
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)
getMulti(array $keys, $flags=0)
Get a batch of items.
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.