MediaWiki master
MultiWriteBagOStuff.php
Go to the documentation of this file.
1<?php
20namespace Wikimedia\ObjectCache;
21
22use InvalidArgumentException;
23use Wikimedia\ObjectFactory\ObjectFactory;
24
105 protected $caches;
106
108 protected $asyncWrites = false;
110 protected $cacheIndexes = [];
111
113 private static $UPGRADE_TTL = 3600;
114
137 public function __construct( $params ) {
138 parent::__construct( $params );
139
140 if ( empty( $params['caches'] ) || !is_array( $params['caches'] ) ) {
141 throw new InvalidArgumentException(
142 __METHOD__ . ': "caches" parameter must be an array of caches'
143 );
144 }
145
146 $this->caches = [];
147 foreach ( $params['caches'] as $cacheInfo ) {
148 if ( $cacheInfo instanceof BagOStuff ) {
149 $this->caches[] = $cacheInfo;
150 } else {
151 $this->caches[] = ObjectFactory::getObjectFromSpec( $cacheInfo );
152 }
153 }
154
155 $this->attrMap = $this->mergeFlagMaps( $this->caches );
156
157 $this->asyncWrites = (
158 isset( $params['replication'] ) &&
159 $params['replication'] === 'async' &&
160 is_callable( $this->asyncHandler )
161 );
162
163 $this->cacheIndexes = array_keys( $this->caches );
164 }
165
166 public function get( $key, $flags = 0 ) {
167 $args = func_get_args();
168
169 if ( $this->fieldHasFlags( $flags, self::READ_LATEST ) ) {
170 // If the latest write was a delete(), we do NOT want to fallback
171 // to the other tiers and possibly see the old value. Also, this
172 // is used by merge(), which only needs to hit the primary.
173 return $this->callKeyMethodOnTierCache(
174 0,
175 __FUNCTION__,
176 self::ARG0_KEY,
177 self::RES_NONKEY,
178 $args
179 );
180 }
181
182 $value = false;
183 // backends checked
184 $missIndexes = [];
185 foreach ( $this->cacheIndexes as $i ) {
186 $value = $this->callKeyMethodOnTierCache(
187 $i,
188 __FUNCTION__,
189 self::ARG0_KEY,
190 self::RES_NONKEY,
191 $args
192 );
193 if ( $value !== false ) {
194 break;
195 }
196 $missIndexes[] = $i;
197 }
198
199 if (
200 $value !== false &&
201 $this->fieldHasFlags( $flags, self::READ_VERIFIED ) &&
202 $missIndexes
203 ) {
204 // Backfill the value to the higher (and often faster/smaller) cache tiers
205 $this->callKeyWriteMethodOnTierCaches(
206 $missIndexes,
207 'set',
208 self::ARG0_KEY,
209 self::RES_NONKEY,
210 [ $key, $value, self::$UPGRADE_TTL ]
211 );
212 }
213
214 return $value;
215 }
216
217 public function set( $key, $value, $exptime = 0, $flags = 0 ) {
218 return $this->callKeyWriteMethodOnTierCaches(
219 $this->cacheIndexes,
220 __FUNCTION__,
221 self::ARG0_KEY,
222 self::RES_NONKEY,
223 func_get_args()
224 );
225 }
226
227 public function delete( $key, $flags = 0 ) {
228 return $this->callKeyWriteMethodOnTierCaches(
229 $this->cacheIndexes,
230 __FUNCTION__,
231 self::ARG0_KEY,
232 self::RES_NONKEY,
233 func_get_args()
234 );
235 }
236
237 public function add( $key, $value, $exptime = 0, $flags = 0 ) {
238 // Try the write to the top-tier cache
239 $ok = $this->callKeyMethodOnTierCache(
240 0,
241 __FUNCTION__,
242 self::ARG0_KEY,
243 self::RES_NONKEY,
244 func_get_args()
245 );
246
247 if ( $ok ) {
248 // Relay the add() using set() if it succeeded. This is meant to handle certain
249 // migration scenarios where the same store might get written to twice for certain
250 // keys. In that case, it makes no sense to return false due to "self-conflicts".
251 $okSecondaries = $this->callKeyWriteMethodOnTierCaches(
252 array_slice( $this->cacheIndexes, 1 ),
253 'set',
254 self::ARG0_KEY,
255 self::RES_NONKEY,
256 [ $key, $value, $exptime, $flags ]
257 );
258 if ( $okSecondaries === false ) {
259 $ok = false;
260 }
261 }
262
263 return $ok;
264 }
265
266 public function merge( $key, callable $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
267 return $this->callKeyWriteMethodOnTierCaches(
268 $this->cacheIndexes,
269 __FUNCTION__,
270 self::ARG0_KEY,
271 self::RES_NONKEY,
272 func_get_args()
273 );
274 }
275
276 public function changeTTL( $key, $exptime = 0, $flags = 0 ) {
277 return $this->callKeyWriteMethodOnTierCaches(
278 $this->cacheIndexes,
279 __FUNCTION__,
280 self::ARG0_KEY,
281 self::RES_NONKEY,
282 func_get_args()
283 );
284 }
285
286 public function lock( $key, $timeout = 6, $exptime = 6, $rclass = '' ) {
287 // Only need to lock the first cache; also avoids deadlocks
288 return $this->callKeyMethodOnTierCache(
289 0,
290 __FUNCTION__,
291 self::ARG0_KEY,
292 self::RES_NONKEY,
293 func_get_args()
294 );
295 }
296
297 public function unlock( $key ) {
298 // Only the first cache is locked
299 return $this->callKeyMethodOnTierCache(
300 0,
301 __FUNCTION__,
302 self::ARG0_KEY,
303 self::RES_NONKEY,
304 func_get_args()
305 );
306 }
307
309 $timestamp,
310 ?callable $progress = null,
311 $limit = INF,
312 ?string $tag = null
313 ) {
314 $ret = false;
315 foreach ( $this->caches as $cache ) {
316 if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit, $tag ) ) {
317 $ret = true;
318 }
319 }
320
321 return $ret;
322 }
323
324 public function getMulti( array $keys, $flags = 0 ) {
325 // Just iterate over each key in order to handle all the backfill logic
326 $res = [];
327 foreach ( $keys as $key ) {
328 $val = $this->get( $key, $flags );
329 if ( $val !== false ) {
330 $res[$key] = $val;
331 }
332 }
333
334 return $res;
335 }
336
337 public function setMulti( array $valueByKey, $exptime = 0, $flags = 0 ) {
338 return $this->callKeyWriteMethodOnTierCaches(
339 $this->cacheIndexes,
340 __FUNCTION__,
341 self::ARG0_KEYMAP,
342 self::RES_NONKEY,
343 func_get_args()
344 );
345 }
346
347 public function deleteMulti( array $keys, $flags = 0 ) {
348 return $this->callKeyWriteMethodOnTierCaches(
349 $this->cacheIndexes,
350 __FUNCTION__,
351 self::ARG0_KEYARR,
352 self::RES_NONKEY,
353 func_get_args()
354 );
355 }
356
357 public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
358 return $this->callKeyWriteMethodOnTierCaches(
359 $this->cacheIndexes,
360 __FUNCTION__,
361 self::ARG0_KEYARR,
362 self::RES_NONKEY,
363 func_get_args()
364 );
365 }
366
367 public function incrWithInit( $key, $exptime, $step = 1, $init = null, $flags = 0 ) {
368 return $this->callKeyWriteMethodOnTierCaches(
369 $this->cacheIndexes,
370 __FUNCTION__,
371 self::ARG0_KEY,
372 self::RES_NONKEY,
373 func_get_args()
374 );
375 }
376
377 public function setMockTime( &$time ) {
378 parent::setMockTime( $time );
379 foreach ( $this->caches as $cache ) {
380 $cache->setMockTime( $time );
381 }
382 }
383
395 private function callKeyMethodOnTierCache( $index, $method, $arg0Sig, $rvSig, array $args ) {
396 return $this->caches[$index]->proxyCall( $method, $arg0Sig, $rvSig, $args, $this );
397 }
398
410 private function callKeyWriteMethodOnTierCaches(
411 array $indexes,
412 $method,
413 $arg0Sig,
414 $resSig,
415 array $args
416 ) {
417 $res = null;
418
419 if ( $this->asyncWrites && array_diff( $indexes, [ 0 ] ) && $method !== 'merge' ) {
420 // Deep-clone $args to prevent misbehavior when something writes an
421 // object to the BagOStuff then modifies it afterwards, e.g. T168040.
422 $args = unserialize( serialize( $args ) );
423 }
424
425 foreach ( $indexes as $i ) {
426 $cache = $this->caches[$i];
427
428 if ( $i == 0 || !$this->asyncWrites ) {
429 // Tier 0 store or in sync mode: write synchronously and get result
430 $storeRes = $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
431 if ( $storeRes === false ) {
432 $res = false;
433 } elseif ( $res === null ) {
434 // first synchronous result
435 $res = $storeRes;
436 }
437 } else {
438 // Secondary write in async mode: do not block this HTTP request
440 function () use ( $cache, $method, $arg0Sig, $resSig, $args ) {
441 $cache->proxyCall( $method, $arg0Sig, $resSig, $args, $this );
442 }
443 );
444 }
445 }
446
447 return $res;
448 }
449}
450
452class_alias( MultiWriteBagOStuff::class, 'MultiWriteBagOStuff' );
Abstract class for any ephemeral data store.
Definition BagOStuff.php:88
mergeFlagMaps(array $bags)
Merge the flag maps of one or more BagOStuff objects into a "lowest common denominator" map.
Wrap multiple BagOStuff objects, to implement different caching tiers.
changeTTLMulti(array $keys, $exptime, $flags=0)
Change the expiration of multiple items.
add( $key, $value, $exptime=0, $flags=0)
Insert an item if it does not already exist.
deleteObjectsExpiringBefore( $timestamp, ?callable $progress=null, $limit=INF, ?string $tag=null)
Delete all objects expiring before a certain date.
int[] $cacheIndexes
List of all backing cache indexes.
deleteMulti(array $keys, $flags=0)
Delete a batch of items.
setMulti(array $valueByKey, $exptime=0, $flags=0)
Set a batch of items.
getMulti(array $keys, $flags=0)
Get a batch of items.
unlock( $key)
Release an advisory lock on a key string.
lock( $key, $timeout=6, $exptime=6, $rclass='')
Acquire an advisory lock on a key string, exclusive to the caller.
changeTTL( $key, $exptime=0, $flags=0)
Change the expiration on an item.
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.
BagOStuff[] $caches
Backing cache stores in order of highest to lowest tier.
bool $asyncWrites
Use async secondary writes.
merge( $key, callable $callback, $exptime=0, $attempts=10, $flags=0)
Merge changes into the existing cache value (possibly creating a new one)