23 use 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 );