8use InvalidArgumentException;
9use Wikimedia\ObjectFactory\ObjectFactory;
99 private static $UPGRADE_TTL = 3600;
124 parent::__construct( $params );
126 if ( empty( $params[
'caches'] ) || !is_array( $params[
'caches'] ) ) {
127 throw new InvalidArgumentException(
128 __METHOD__ .
': "caches" parameter must be an array of caches'
133 foreach ( $params[
'caches'] as $cacheInfo ) {
135 $this->caches[] = $cacheInfo;
137 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
143 $this->asyncWrites = (
144 isset( $params[
'replication'] ) &&
145 $params[
'replication'] ===
'async' &&
146 is_callable( $this->asyncHandler )
149 $this->cacheIndexes = array_keys( $this->caches );
153 public function get( $key, $flags = 0 ) {
154 $args = func_get_args();
160 return $this->callKeyMethodOnTierCache(
172 foreach ( $this->cacheIndexes as $i ) {
173 $value = $this->callKeyMethodOnTierCache(
180 if ( $value !==
false ) {
192 $this->callKeyWriteMethodOnTierCaches(
197 [ $key, $value, self::$UPGRADE_TTL ]
205 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
206 return $this->callKeyWriteMethodOnTierCaches(
216 public function delete( $key, $flags = 0 ) {
217 return $this->callKeyWriteMethodOnTierCaches(
227 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
229 $ok = $this->callKeyMethodOnTierCache(
241 $okSecondaries = $this->callKeyWriteMethodOnTierCaches(
242 array_slice( $this->cacheIndexes, 1 ),
246 [ $key, $value, $exptime, $flags ]
248 if ( $okSecondaries ===
false ) {
257 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
258 return $this->callKeyWriteMethodOnTierCaches(
268 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
269 return $this->callKeyWriteMethodOnTierCaches(
279 public function lock( $key, $timeout = 6, $exptime = 6, $rclass =
'' ) {
281 return $this->callKeyMethodOnTierCache(
293 return $this->callKeyMethodOnTierCache(
305 ?callable $progress =
null,
310 foreach ( $this->caches as $cache ) {
311 if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) {
320 public function getMulti( array $keys, $flags = 0 ) {
323 foreach ( $keys as $key ) {
324 $val = $this->
get( $key, $flags );
325 if ( $val !==
false ) {
334 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
335 return $this->callKeyWriteMethodOnTierCaches(
346 return $this->callKeyWriteMethodOnTierCaches(
357 return $this->callKeyWriteMethodOnTierCaches(
367 public function incrWithInit( $key, $exptime, $step = 1, $init =
null, $flags = 0 ) {
368 return $this->callKeyWriteMethodOnTierCaches(
379 parent::setMockTime( $time );
380 foreach ( $this->caches as $cache ) {
381 $cache->setMockTime( $time );
396 private function callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args ) {
397 return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig, $args, $this );
411 private function callKeyWriteMethodOnTierCaches(
420 if ( $this->asyncWrites && array_diff( $indexes, [ 0 ] ) && $method !==
'merge' ) {
423 $args = unserialize( serialize( $args ) );
426 foreach ( $indexes as $i ) {
427 $cache = $this->caches[$i];
429 if ( $i == 0 || !$this->asyncWrites ) {
431 $storeRes = $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
432 if ( $storeRes ===
false ) {
434 } elseif ( $res ===
null ) {
441 function () use ( $cache, $method, $arg0Sig, $resSig, $args ) {
442 $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
453class_alias( MultiWriteBagOStuff::class,
'MultiWriteBagOStuff' );