23 use Wikimedia\ObjectFactory;
65 if ( empty(
$params[
'caches'] ) || !is_array(
$params[
'caches'] ) ) {
66 throw new InvalidArgumentException(
67 __METHOD__ .
': "caches" parameter must be an array of caches'
72 foreach (
$params[
'caches']
as $cacheInfo ) {
74 $this->caches[] = $cacheInfo;
76 if ( !isset( $cacheInfo[
'args'] ) ) {
81 $cacheInfo[
'args'] = [ $cacheInfo ];
83 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
88 $this->asyncWrites = (
89 isset(
$params[
'replication'] ) &&
90 $params[
'replication'] ===
'async' &&
91 is_callable( $this->asyncHandler )
94 $this->cacheIndexes = array_keys( $this->caches );
98 foreach ( $this->caches
as $cache ) {
103 protected function doGet( $key, $flags = 0 ) {
104 if ( ( $flags & self::READ_LATEST ) == self::READ_LATEST ) {
108 return $this->caches[0]->get( $key, $flags );
113 foreach ( $this->caches
as $i =>
$cache ) {
123 && ( $flags & self::READ_VERIFIED ) == self::READ_VERIFIED
127 $missIndexes, $this->asyncWrites,
'set', $key,
$value, self::UPGRADE_TTL
134 public function set( $key,
$value, $exptime = 0, $flags = 0 ) {
142 public function delete( $key ) {
143 return $this->
doWrite( $this->cacheIndexes, $this->asyncWrites,
'delete', $key );
148 $ok = $this->
doWrite( [ 0 ], $this->asyncWrites,
'add', $key,
$value, $exptime );
154 array_slice( $this->cacheIndexes, 1 ),
167 return $this->
doWrite( $this->cacheIndexes, $this->asyncWrites,
'incr', $key,
$value );
171 return $this->
doWrite( $this->cacheIndexes, $this->asyncWrites,
'decr', $key,
$value );
174 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
191 public function lock( $key, $timeout = 6, $expiry = 6, $rclass =
'' ) {
193 return $this->caches[0]->lock( $key, $timeout, $expiry, $rclass );
198 return $this->caches[0]->unlock( $key );
202 return $this->caches[0]->getLastError();
206 $this->caches[0]->clearLastError();
220 $args = array_slice( func_get_args(), 3 );
222 if ( array_diff( $indexes, [ 0 ] ) &&
$asyncWrites && $method !==
'merge' ) {
228 foreach ( $indexes
as $i ) {
229 $cache = $this->caches[$i];
232 if ( !
$cache->$method( ...$args ) ) {
240 if ( !
$cache->$method( ...$args ) ) {
241 $logger->warning(
"Async $method op failed" );
261 foreach ( $this->caches
as $cache ) {
262 if (
$cache->deleteObjectsExpiringBefore( $date, $progressCallback ) ) {
271 return $this->caches[0]->makeKeyInternal( ...func_get_args() );
274 public function makeKey( $class, $component =
null ) {
275 return $this->caches[0]->makeKey( ...func_get_args() );
279 return $this->caches[0]->makeGlobalKey( ...func_get_args() );