22use InvalidArgumentException;
23use Wikimedia\ObjectFactory\ObjectFactory;
113 private static $UPGRADE_TTL = 3600;
138 parent::__construct( $params );
140 if ( empty( $params[
'caches'] ) || !is_array( $params[
'caches'] ) ) {
141 throw new InvalidArgumentException(
142 __METHOD__ .
': "caches" parameter must be an array of caches'
147 foreach ( $params[
'caches'] as $cacheInfo ) {
149 $this->caches[] = $cacheInfo;
151 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
157 $this->asyncWrites = (
158 isset( $params[
'replication'] ) &&
159 $params[
'replication'] ===
'async' &&
160 is_callable( $this->asyncHandler )
163 $this->cacheIndexes = array_keys( $this->caches );
166 public function get( $key, $flags = 0 ) {
167 $args = func_get_args();
173 return $this->callKeyMethodOnTierCache(
185 foreach ( $this->cacheIndexes as $i ) {
186 $value = $this->callKeyMethodOnTierCache(
193 if ( $value !==
false ) {
205 $this->callKeyWriteMethodOnTierCaches(
210 [ $key, $value, self::$UPGRADE_TTL ]
217 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
218 return $this->callKeyWriteMethodOnTierCaches(
227 public function delete( $key, $flags = 0 ) {
228 return $this->callKeyWriteMethodOnTierCaches(
237 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
239 $ok = $this->callKeyMethodOnTierCache(
251 $okSecondaries = $this->callKeyWriteMethodOnTierCaches(
252 array_slice( $this->cacheIndexes, 1 ),
256 [ $key, $value, $exptime, $flags ]
258 if ( $okSecondaries ===
false ) {
266 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
267 return $this->callKeyWriteMethodOnTierCaches(
276 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
277 return $this->callKeyWriteMethodOnTierCaches(
286 public function lock( $key, $timeout = 6, $exptime = 6, $rclass =
'' ) {
288 return $this->callKeyMethodOnTierCache(
299 return $this->callKeyMethodOnTierCache(
310 ?callable $progress =
null,
315 foreach ( $this->caches as $cache ) {
316 if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) {
324 public function getMulti( array $keys, $flags = 0 ) {
327 foreach ( $keys as $key ) {
328 $val = $this->
get( $key, $flags );
329 if ( $val !==
false ) {
337 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
338 return $this->callKeyWriteMethodOnTierCaches(
348 return $this->callKeyWriteMethodOnTierCaches(
358 return $this->callKeyWriteMethodOnTierCaches(
367 public function incrWithInit( $key, $exptime, $step = 1, $init =
null, $flags = 0 ) {
368 return $this->callKeyWriteMethodOnTierCaches(
378 parent::setMockTime( $time );
379 foreach ( $this->caches as $cache ) {
380 $cache->setMockTime( $time );
395 private function callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args ) {
396 return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig, $args, $this );
410 private function callKeyWriteMethodOnTierCaches(
419 if ( $this->asyncWrites && array_diff( $indexes, [ 0 ] ) && $method !==
'merge' ) {
422 $args = unserialize( serialize( $args ) );
425 foreach ( $indexes as $i ) {
426 $cache = $this->caches[$i];
428 if ( $i == 0 || !$this->asyncWrites ) {
430 $storeRes = $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
431 if ( $storeRes ===
false ) {
433 } elseif ( $res ===
null ) {
440 function () use ( $cache, $method, $arg0Sig, $resSig, $args ) {
441 $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
452class_alias( MultiWriteBagOStuff::class,
'MultiWriteBagOStuff' );