3 use Wikimedia\TestingAccessWrapper
;
6 * @covers WANObjectCache::wrap
7 * @covers WANObjectCache::unwrap
8 * @covers WANObjectCache::worthRefreshExpiring
9 * @covers WANObjectCache::worthRefreshPopular
10 * @covers WANObjectCache::isValid
11 * @covers WANObjectCache::getWarmupKeyMisses
12 * @covers WANObjectCache::prefixCacheKeys
13 * @covers WANObjectCache::getProcessCache
14 * @covers WANObjectCache::getNonProcessCachedKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
19 class WANObjectCacheTest
extends PHPUnit\Framework\TestCase
{
21 use MediaWikiCoversValidator
;
23 /** @var TimeAdjustableWANObjectCache */
26 private $internalCache;
28 protected function setUp() {
31 $this->cache
= new TimeAdjustableWANObjectCache( [
32 'cache' => new TimeAdjustableHashBagOStuff(),
33 'pool' => 'testcache-hash',
34 'relayer' => new EventRelayerNull( [] )
37 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
38 /** @noinspection PhpUndefinedFieldInspection */
39 $this->internalCache
= $wanCache->cache
;
43 * @dataProvider provideSetAndGet
44 * @covers WANObjectCache::set()
45 * @covers WANObjectCache::get()
46 * @covers WANObjectCache::makeKey()
50 public function testSetAndGet( $value, $ttl ) {
53 $key = $this->cache
->makeKey( 'x', wfRandomString() );
55 $this->cache
->get( $key, $curTTL, [], $asOf );
56 $this->assertNull( $curTTL, "Current TTL is null" );
57 $this->assertNull( $asOf, "Current as-of-time is infinite" );
59 $t = microtime( true );
60 $this->cache
->set( $key, $value, $ttl );
62 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
63 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
64 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
66 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
67 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
69 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
70 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
73 public static function provideSetAndGet() {
80 [ (object)[ 'meow' ], 3 ],
88 * @covers WANObjectCache::get()
89 * @covers WANObjectCache::makeGlobalKey()
91 public function testGetNotExists() {
92 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
94 $value = $this->cache
->get( $key, $curTTL );
96 $this->assertFalse( $value, "Non-existing key has false value" );
97 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
101 * @covers WANObjectCache::set()
103 public function testSetOver() {
104 $key = wfRandomString();
105 for ( $i = 0; $i < 3; ++
$i ) {
106 $value = wfRandomString();
107 $this->cache
->set( $key, $value, 3 );
109 $this->assertEquals( $this->cache
->get( $key ), $value );
114 * @covers WANObjectCache::set()
116 public function testStaleSet() {
117 $key = wfRandomString();
118 $value = wfRandomString();
119 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
121 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
124 public function testProcessCache() {
126 $callback = function () use ( &$hit ) {
130 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
131 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
133 foreach ( $keys as $i => $key ) {
134 $this->cache
->getWithSetCallback(
135 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
137 $this->assertEquals( 3, $hit );
139 foreach ( $keys as $i => $key ) {
140 $this->cache
->getWithSetCallback(
141 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
143 $this->assertEquals( 3, $hit, "Values cached" );
145 foreach ( $keys as $i => $key ) {
146 $this->cache
->getWithSetCallback(
147 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
149 $this->assertEquals( 6, $hit );
151 foreach ( $keys as $i => $key ) {
152 $this->cache
->getWithSetCallback(
153 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
155 $this->assertEquals( 6, $hit, "New values cached" );
157 foreach ( $keys as $i => $key ) {
158 $this->cache
->delete( $key );
159 $this->cache
->getWithSetCallback(
160 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
162 $this->assertEquals( 9, $hit, "Values evicted" );
164 $key = reset( $keys );
165 // Get into cache (default process cache group)
166 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
167 $this->assertEquals( 10, $hit, "Value calculated" );
168 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
169 $this->assertEquals( 10, $hit, "Value cached" );
170 $outerCallback = function () use ( &$callback, $key ) {
171 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
175 // Outer key misses and refuses inner key process cache value
176 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
177 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
181 * @dataProvider getWithSetCallback_provider
182 * @covers WANObjectCache::getWithSetCallback()
183 * @covers WANObjectCache::doGetWithSetCallback()
184 * @param array $extOpts
185 * @param bool $versioned
187 public function testGetWithSetCallback( array $extOpts, $versioned ) {
188 $cache = $this->cache
;
190 $key = wfRandomString();
191 $value = wfRandomString();
192 $cKey1 = wfRandomString();
193 $cKey2 = wfRandomString();
198 $func = function ( $old, &$ttl, &$opts, $asOf )
199 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
204 $ttl = 20; // override with another value
209 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
210 $this->assertEquals( $value, $v, "Value returned" );
211 $this->assertEquals( 1, $wasSet, "Value regenerated" );
212 $this->assertFalse( $priorValue, "No prior value" );
213 $this->assertNull( $priorAsOf, "No prior value" );
216 $cache->get( $key, $curTTL );
217 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
218 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
221 $v = $cache->getWithSetCallback(
222 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
223 $this->assertEquals( $value, $v, "Value returned" );
224 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
226 $priorTime = microtime( true ); // reference time
227 $cache->setTime( $priorTime );
229 $cache->addTime( 1 );
232 $v = $cache->getWithSetCallback(
233 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
235 $this->assertEquals( $value, $v, "Value returned" );
236 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
237 $this->assertEquals( $value, $priorValue, "Has prior value" );
238 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
239 $t1 = $cache->getCheckKeyTime( $cKey1 );
240 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
241 $t2 = $cache->getCheckKeyTime( $cKey2 );
242 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
244 $priorTime = $cache->addTime( 0.01 );
247 $v = $cache->getWithSetCallback(
248 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
250 $this->assertEquals( $value, $v, "Value returned" );
251 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
252 $t1 = $cache->getCheckKeyTime( $cKey1 );
253 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
254 $t2 = $cache->getCheckKeyTime( $cKey2 );
255 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
258 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
260 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
262 $this->assertEquals( $value, $v, "Value returned" );
264 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
267 $key = wfRandomString();
268 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
269 $this->assertEquals( $value, $v, "Value returned" );
270 $cache->delete( $key );
271 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
272 $this->assertEquals( $value, $v, "Value still returned after deleted" );
273 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
275 $oldValReceived = -1;
276 $oldAsOfReceived = -1;
277 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
278 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
280 $oldValReceived = $oldVal;
281 $oldAsOfReceived = $oldAsOf;
283 return 'xxx' . $wasSet;
286 $now = microtime( true ); // reference time
289 $key = wfRandomString();
290 $v = $cache->getWithSetCallback(
291 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
292 $this->assertEquals( 'xxx1', $v, "Value returned" );
293 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
294 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
296 $cache->addTime( 40 );
297 $v = $cache->getWithSetCallback(
298 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
299 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
300 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
301 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
302 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
304 $cache->addTime( 260 );
305 $v = $cache->getWithSetCallback(
306 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
307 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
308 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
309 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
310 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
313 $key = wfRandomString();
314 $checkKey = $cache->makeKey( 'template', 'X' );
315 $cache->setTime( $now - $cache::HOLDOFF_TTL
- 1 );
316 $cache->touchCheckKey( $checkKey ); // init check key
317 $cache->setTime( $now );
318 $v = $cache->getWithSetCallback(
320 $cache::TTL_INDEFINITE
,
322 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
324 $this->assertEquals( 'xxx1', $v, "Value returned" );
325 $this->assertEquals( 1, $wasSet, "Value computed" );
326 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
327 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
329 $cache->addTime( $cache::TTL_HOUR
); // some time passes
330 $v = $cache->getWithSetCallback(
332 $cache::TTL_INDEFINITE
,
334 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
336 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
337 $this->assertEquals( 1, $wasSet, "Cached value returned" );
339 $cache->touchCheckKey( $checkKey ); // make key stale
340 $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
342 $v = $cache->getWithSetCallback(
344 $cache::TTL_INDEFINITE
,
346 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
348 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
349 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
351 // Change of refresh increase to unity as staleness approaches graceTTL
353 $cache->addTime( $cache::TTL_WEEK
); // 8 days of being stale
354 $v = $cache->getWithSetCallback(
356 $cache::TTL_INDEFINITE
,
358 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
360 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
361 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
362 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
363 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
366 public static function getWithSetCallback_provider() {
369 [ [ 'version' => 1 ], true ]
373 public function testPreemtiveRefresh() {
376 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
382 $cache = new NearExpiringWANObjectCache( [
383 'cache' => new HashBagOStuff(),
388 $key = wfRandomString();
389 $opts = [ 'lowTTL' => 30 ];
390 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
391 $this->assertEquals( $value, $v, "Value returned" );
392 $this->assertEquals( 1, $wasSet, "Value calculated" );
393 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
394 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
397 $key = wfRandomString();
398 $opts = [ 'lowTTL' => 1 ];
399 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
400 $this->assertEquals( $value, $v, "Value returned" );
401 $this->assertEquals( 1, $wasSet, "Value calculated" );
402 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
403 $this->assertEquals( 1, $wasSet, "Value cached" );
406 $asyncHandler = function ( $callback ) use ( &$asycList ) {
407 $asycList[] = $callback;
409 $cache = new NearExpiringWANObjectCache( [
410 'cache' => new TimeAdjustableHashBagOStuff(),
412 'asyncHandler' => $asyncHandler
415 $now = microtime( true ); // reference time
416 $cache->setTime( $now );
419 $key = wfRandomString();
420 $checkKey = wfRandomString();
421 $opts = [ 'lowTTL' => 100 ];
422 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
423 $this->assertEquals( $value, $v, "Value returned" );
424 $this->assertEquals( 1, $wasSet, "Value calculated" );
425 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
426 $this->assertEquals( 1, $wasSet, "Cached value used" );
427 $this->assertEquals( $v, $value, "Value cached" );
429 $cache->setTime( $now +
250 );
430 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
431 $this->assertEquals( $value, $v, "Value returned" );
432 $this->assertEquals( 1, $wasSet, "Stale value used" );
433 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
434 $value = 'NewCatsInTown'; // change callback return value
435 $asycList[0](); // run the refresh callback
437 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
438 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
439 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
440 $this->assertEquals( $value, $v, "New value stored" );
442 $cache = new PopularityRefreshingWANObjectCache( [
443 'cache' => new TimeAdjustableHashBagOStuff(),
447 $cache->setTime( $now );
450 $key = wfRandomString();
451 $opts = [ 'hotTTR' => 900 ];
452 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
453 $this->assertEquals( $value, $v, "Value returned" );
454 $this->assertEquals( 1, $wasSet, "Value calculated" );
455 $cache->setTime( $now +
30 );
456 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
457 $this->assertEquals( 1, $wasSet, "Value cached" );
460 $key = wfRandomString();
461 $opts = [ 'hotTTR' => 10 ];
462 $cache->setTime( $now );
463 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
464 $this->assertEquals( $value, $v, "Value returned" );
465 $this->assertEquals( 1, $wasSet, "Value calculated" );
466 $cache->setTime( $now +
30 );
467 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
468 $this->assertEquals( $value, $v, "Value returned" );
469 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
473 * @covers WANObjectCache::getWithSetCallback()
474 * @covers WANObjectCache::doGetWithSetCallback()
476 public function testGetWithSetCallback_invalidCallback() {
477 $this->setExpectedException( InvalidArgumentException
::class );
478 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
482 * @dataProvider getMultiWithSetCallback_provider
483 * @covers WANObjectCache::getMultiWithSetCallback
484 * @covers WANObjectCache::makeMultiKeys
485 * @covers WANObjectCache::getMulti
486 * @param array $extOpts
487 * @param bool $versioned
489 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
490 $cache = $this->cache
;
492 $keyA = wfRandomString();
493 $keyB = wfRandomString();
494 $keyC = wfRandomString();
495 $cKey1 = wfRandomString();
496 $cKey2 = wfRandomString();
501 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
502 &$wasSet, &$priorValue, &$priorAsOf
507 $ttl = 20; // override with another value
512 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
514 $v = $cache->getMultiWithSetCallback(
515 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
516 $this->assertEquals( $value, $v[$keyA], "Value returned" );
517 $this->assertEquals( 1, $wasSet, "Value regenerated" );
518 $this->assertFalse( $priorValue, "No prior value" );
519 $this->assertNull( $priorAsOf, "No prior value" );
522 $cache->get( $keyA, $curTTL );
523 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
524 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
528 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
529 $v = $cache->getMultiWithSetCallback(
530 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
531 $this->assertEquals( $value, $v[$keyB], "Value returned" );
532 $this->assertEquals( 1, $wasSet, "Value regenerated" );
533 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
534 $v = $cache->getMultiWithSetCallback(
535 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
536 $this->assertEquals( $value, $v[$keyB], "Value returned" );
537 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
538 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
540 $priorTime = microtime( true ); // reference time
541 $cache->setTime( $priorTime );
543 $cache->addTime( 1 );
546 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
547 $v = $cache->getMultiWithSetCallback(
548 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
550 $this->assertEquals( $value, $v[$keyB], "Value returned" );
551 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
552 $this->assertEquals( $value, $priorValue, "Has prior value" );
553 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
554 $t1 = $cache->getCheckKeyTime( $cKey1 );
555 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
556 $t2 = $cache->getCheckKeyTime( $cKey2 );
557 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
559 $priorTime = $cache->addTime( 0.01 );
562 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
563 $v = $cache->getMultiWithSetCallback(
564 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
566 $this->assertEquals( $value, $v[$keyC], "Value returned" );
567 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
568 $t1 = $cache->getCheckKeyTime( $cKey1 );
569 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
570 $t2 = $cache->getCheckKeyTime( $cKey2 );
571 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
574 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
576 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
578 $this->assertEquals( $value, $v, "Value returned" );
580 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
583 $key = wfRandomString();
584 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
585 $v = $cache->getMultiWithSetCallback(
586 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
587 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
588 $cache->delete( $key );
589 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
590 $v = $cache->getMultiWithSetCallback(
591 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
592 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
593 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
596 $ids = [ 1, 2, 3, 4, 5, 6 ];
597 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
598 return $wanCache->makeKey( 'test', $id );
600 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
601 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
606 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
609 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
610 array_values( $values ),
611 "Correct values in correct order"
614 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
615 array_keys( $values ),
616 "Correct keys in correct order"
618 $this->assertEquals( count( $ids ), $calls );
620 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
621 $this->assertEquals( count( $ids ), $calls, "Values cached" );
623 // Mock the BagOStuff to assure only one getMulti() call given process caching
624 $localBag = $this->getMockBuilder( HashBagOStuff
::class )
625 ->setMethods( [ 'getMulti' ] )->getMock();
626 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
627 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
628 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
630 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
632 // Warm the process cache
633 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
635 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
636 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
638 // Use the process cache
640 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
641 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
645 public static function getMultiWithSetCallback_provider() {
648 [ [ 'version' => 1 ], true ]
653 * @dataProvider getMultiWithUnionSetCallback_provider
654 * @covers WANObjectCache::getMultiWithUnionSetCallback()
655 * @covers WANObjectCache::makeMultiKeys()
656 * @param array $extOpts
657 * @param bool $versioned
659 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
660 $cache = $this->cache
;
662 $keyA = wfRandomString();
663 $keyB = wfRandomString();
664 $keyC = wfRandomString();
665 $cKey1 = wfRandomString();
666 $cKey2 = wfRandomString();
669 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
670 &$wasSet, &$priorValue, &$priorAsOf
673 foreach ( $ids as $id ) {
675 $newValues[$id] = "@$id$";
676 $ttls[$id] = 20; // override with another value
683 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
685 $v = $cache->getMultiWithUnionSetCallback(
686 $keyedIds, 30, $genFunc, $extOpts );
687 $this->assertEquals( $value, $v[$keyA], "Value returned" );
688 $this->assertEquals( 1, $wasSet, "Value regenerated" );
691 $cache->get( $keyA, $curTTL );
692 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
693 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
697 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
698 $v = $cache->getMultiWithUnionSetCallback(
699 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
700 $this->assertEquals( $value, $v[$keyB], "Value returned" );
701 $this->assertEquals( 1, $wasSet, "Value regenerated" );
702 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
703 $v = $cache->getMultiWithUnionSetCallback(
704 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
705 $this->assertEquals( $value, $v[$keyB], "Value returned" );
706 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
707 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
709 $priorTime = microtime( true ); // reference time
710 $cache->setTime( $priorTime );
712 $cache->addTime( 1 );
715 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
716 $v = $cache->getMultiWithUnionSetCallback(
717 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
719 $this->assertEquals( $value, $v[$keyB], "Value returned" );
720 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
721 $t1 = $cache->getCheckKeyTime( $cKey1 );
722 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
723 $t2 = $cache->getCheckKeyTime( $cKey2 );
724 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
726 $priorTime = $cache->addTime( 0.01 );
729 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
730 $v = $cache->getMultiWithUnionSetCallback(
731 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
733 $this->assertEquals( $value, $v[$keyC], "Value returned" );
734 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
735 $t1 = $cache->getCheckKeyTime( $cKey1 );
736 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
737 $t2 = $cache->getCheckKeyTime( $cKey2 );
738 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
741 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
743 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
745 $this->assertEquals( $value, $v, "Value returned" );
747 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
750 $key = wfRandomString();
751 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
752 $v = $cache->getMultiWithUnionSetCallback(
753 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
754 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
755 $cache->delete( $key );
756 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
757 $v = $cache->getMultiWithUnionSetCallback(
758 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
759 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
760 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
763 $ids = [ 1, 2, 3, 4, 5, 6 ];
764 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
765 return $wanCache->makeKey( 'test', $id );
767 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
768 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
770 foreach ( $ids as $id ) {
772 $newValues[$id] = "val-{$id}";
777 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
780 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
781 array_values( $values ),
782 "Correct values in correct order"
785 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
786 array_keys( $values ),
787 "Correct keys in correct order"
789 $this->assertEquals( count( $ids ), $calls );
791 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
792 $this->assertEquals( count( $ids ), $calls, "Values cached" );
795 public static function getMultiWithUnionSetCallback_provider() {
798 [ [ 'version' => 1 ], true ]
803 * @covers WANObjectCache::getWithSetCallback()
804 * @covers WANObjectCache::doGetWithSetCallback()
806 public function testLockTSE() {
807 $cache = $this->cache
;
808 $key = wfRandomString();
809 $value = wfRandomString();
812 $func = function () use ( &$calls, $value, $cache, $key ) {
817 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
818 $this->assertEquals( $value, $ret );
819 $this->assertEquals( 1, $calls, 'Value was populated' );
821 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
822 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
824 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
825 $ret = $cache->getWithSetCallback( $key, 30, $func,
826 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
827 $this->assertEquals( $value, $ret, 'Old value used' );
828 $this->assertEquals( 1, $calls, 'Callback was not used' );
830 $cache->delete( $key );
831 $ret = $cache->getWithSetCallback( $key, 30, $func,
832 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
833 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
834 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
836 $ret = $cache->getWithSetCallback( $key, 30, $func,
837 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
838 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
839 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
843 * @covers WANObjectCache::getWithSetCallback()
844 * @covers WANObjectCache::doGetWithSetCallback()
845 * @covers WANObjectCache::set()
847 public function testLockTSESlow() {
848 $cache = $this->cache
;
849 $key = wfRandomString();
850 $value = wfRandomString();
853 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
855 $setOpts['since'] = microtime( true ) - 10;
856 // Immediately kill any mutex rather than waiting a second
857 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
861 // Value should be marked as stale due to snapshot lag
863 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
864 $this->assertEquals( $value, $ret );
865 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
866 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
867 $this->assertEquals( 1, $calls, 'Value was generated' );
869 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
870 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
871 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
872 $this->assertEquals( $value, $ret );
873 $this->assertEquals( 1, $calls, 'Callback was not used' );
877 * @covers WANObjectCache::getWithSetCallback()
878 * @covers WANObjectCache::doGetWithSetCallback()
880 public function testBusyValue() {
881 $cache = $this->cache
;
882 $key = wfRandomString();
883 $value = wfRandomString();
884 $busyValue = wfRandomString();
887 $func = function () use ( &$calls, $value, $cache, $key ) {
889 // Immediately kill any mutex rather than waiting a second
890 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
894 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
895 $this->assertEquals( $value, $ret );
896 $this->assertEquals( 1, $calls, 'Value was populated' );
898 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
899 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
901 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
902 $ret = $cache->getWithSetCallback( $key, 30, $func,
903 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
904 $this->assertEquals( $value, $ret, 'Callback used' );
905 $this->assertEquals( 2, $calls, 'Callback used' );
907 $ret = $cache->getWithSetCallback( $key, 30, $func,
908 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
909 $this->assertEquals( $value, $ret, 'Old value used' );
910 $this->assertEquals( 2, $calls, 'Callback was not used' );
912 $cache->delete( $key ); // no value at all anymore and still locked
913 $ret = $cache->getWithSetCallback( $key, 30, $func,
914 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
915 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
916 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
918 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
919 $ret = $cache->getWithSetCallback( $key, 30, $func,
920 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
921 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
922 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
924 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
925 $ret = $cache->getWithSetCallback( $key, 30, $func,
926 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
927 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
928 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
932 * @covers WANObjectCache::getMulti()
934 public function testGetMulti() {
935 $cache = $this->cache
;
937 $value1 = [ 'this' => 'is', 'a' => 'test' ];
938 $value2 = [ 'this' => 'is', 'another' => 'test' ];
940 $key1 = wfRandomString();
941 $key2 = wfRandomString();
942 $key3 = wfRandomString();
944 $cache->set( $key1, $value1, 5 );
945 $cache->set( $key2, $value2, 10 );
949 [ $key1 => $value1, $key2 => $value2 ],
950 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
951 'Result array populated'
954 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
955 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
956 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
958 $cKey1 = wfRandomString();
959 $cKey2 = wfRandomString();
961 $priorTime = microtime( true ); // reference time
962 $cache->setTime( $priorTime );
964 $cache->addTime( 1 );
968 [ $key1 => $value1, $key2 => $value2 ],
969 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
970 "Result array populated even with new check keys"
972 $t1 = $cache->getCheckKeyTime( $cKey1 );
973 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
974 $t2 = $cache->getCheckKeyTime( $cKey2 );
975 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
976 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
977 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
978 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
980 $cache->addTime( 1 );
984 [ $key1 => $value1, $key2 => $value2 ],
985 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
986 "Result array still populated even with new check keys"
988 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
989 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
990 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
994 * @covers WANObjectCache::getMulti()
995 * @covers WANObjectCache::processCheckKeys()
997 public function testGetMultiCheckKeys() {
998 $cache = $this->cache
;
1000 $checkAll = wfRandomString();
1001 $check1 = wfRandomString();
1002 $check2 = wfRandomString();
1003 $check3 = wfRandomString();
1004 $value1 = wfRandomString();
1005 $value2 = wfRandomString();
1007 $cache->setTime( microtime( true ) );
1009 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1010 // several seconds during the test to assert the behaviour.
1011 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1012 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1015 $cache->addTime( 0.100 );
1017 $cache->set( 'key1', $value1, 10 );
1018 $cache->set( 'key2', $value2, 10 );
1021 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1027 $this->assertEquals(
1028 [ 'key1' => $value1, 'key2' => $value2 ],
1032 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1033 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1034 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1035 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1037 $cache->addTime( 0.100 );
1038 $cache->touchCheckKey( $check1 );
1041 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1047 $this->assertEquals(
1048 [ 'key1' => $value1, 'key2' => $value2 ],
1050 'key1 expired by check1, but value still provided'
1052 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1053 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1055 $cache->touchCheckKey( $checkAll );
1058 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1064 $this->assertEquals(
1065 [ 'key1' => $value1, 'key2' => $value2 ],
1067 'All keys expired by checkAll, but value still provided'
1069 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1070 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1074 * @covers WANObjectCache::get()
1075 * @covers WANObjectCache::processCheckKeys()
1077 public function testCheckKeyInitHoldoff() {
1078 $cache = $this->cache
;
1080 for ( $i = 0; $i < 500; ++
$i ) {
1081 $key = wfRandomString();
1082 $checkKey = wfRandomString();
1084 $cache->get( $key, $curTTL, [ $checkKey ] );
1085 $cache->set( $key, 'val', 10 );
1087 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1089 $this->assertEquals( 'val', $v );
1090 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1093 for ( $i = 0; $i < 500; ++
$i ) {
1094 $key = wfRandomString();
1095 $checkKey = wfRandomString();
1097 $cache->set( $key, 'val', 10 );
1099 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1101 $this->assertEquals( 'val', $v );
1102 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1107 * @covers WANObjectCache::delete
1108 * @covers WANObjectCache::relayDelete
1109 * @covers WANObjectCache::relayPurge
1111 public function testDelete() {
1112 $key = wfRandomString();
1113 $value = wfRandomString();
1114 $this->cache
->set( $key, $value );
1117 $v = $this->cache
->get( $key, $curTTL );
1118 $this->assertEquals( $value, $v, "Key was created with value" );
1119 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1121 $this->cache
->delete( $key );
1124 $v = $this->cache
->get( $key, $curTTL );
1125 $this->assertFalse( $v, "Deleted key has false value" );
1126 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1128 $this->cache
->set( $key, $value . 'more' );
1129 $v = $this->cache
->get( $key, $curTTL );
1130 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1131 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1133 $this->cache
->set( $key, $value );
1134 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1137 $v = $this->cache
->get( $key, $curTTL );
1138 $this->assertFalse( $v, "Deleted key has false value" );
1139 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1141 $this->cache
->set( $key, $value );
1142 $v = $this->cache
->get( $key, $curTTL );
1143 $this->assertEquals( $value, $v, "Key was created with value" );
1144 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1148 * @dataProvider getWithSetCallback_versions_provider
1149 * @covers WANObjectCache::getWithSetCallback()
1150 * @covers WANObjectCache::doGetWithSetCallback()
1151 * @param array $extOpts
1152 * @param bool $versioned
1154 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1155 $cache = $this->cache
;
1157 $key = wfRandomString();
1158 $valueV1 = wfRandomString();
1159 $valueV2 = [ wfRandomString() ];
1162 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1168 $priorValue = false;
1170 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1171 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1172 $priorValue = $oldValue;
1173 $priorAsOf = $oldAsOf;
1176 return $valueV2; // new array format
1179 // Set the main key (version N if versioned)
1181 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1182 $this->assertEquals( $valueV1, $v, "Value returned" );
1183 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1184 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1185 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1186 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1189 // Set the key for version N+1 format
1190 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1192 // Start versioning now with the unversioned key still there
1193 $verOpts = [ 'version' => 1 ];
1196 // Value goes to secondary key since V1 already used $key
1198 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1199 $this->assertEquals( $valueV2, $v, "Value returned" );
1200 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1201 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1202 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1205 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1206 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1207 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1209 // Clear out the older or unversioned key
1210 $cache->delete( $key, 0 );
1212 // Set the key for next/first versioned format
1214 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1215 $this->assertEquals( $valueV2, $v, "Value returned" );
1216 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1218 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1219 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1220 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1223 public static function getWithSetCallback_versions_provider() {
1226 [ [ 'version' => 1 ], true ]
1231 * @covers WANObjectCache::useInterimHoldOffCaching
1232 * @covers WANObjectCache::getInterimValue
1234 public function testInterimHoldOffCaching() {
1235 $cache = $this->cache
;
1237 $value = 'CRL-40-940';
1239 $func = function () use ( &$wasCalled, $value ) {
1245 $cache->useInterimHoldOffCaching( true );
1247 $key = wfRandomString( 32 );
1248 $v = $cache->getWithSetCallback( $key, 60, $func );
1249 $v = $cache->getWithSetCallback( $key, 60, $func );
1250 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1251 $cache->delete( $key );
1252 $v = $cache->getWithSetCallback( $key, 60, $func );
1253 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1254 $v = $cache->getWithSetCallback( $key, 60, $func );
1255 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1256 // Lock up the mutex so interim cache is used
1257 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1258 $v = $cache->getWithSetCallback( $key, 60, $func );
1259 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1260 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1262 $cache->useInterimHoldOffCaching( false );
1265 $key = wfRandomString( 32 );
1266 $v = $cache->getWithSetCallback( $key, 60, $func );
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1269 $cache->delete( $key );
1270 $v = $cache->getWithSetCallback( $key, 60, $func );
1271 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1272 $v = $cache->getWithSetCallback( $key, 60, $func );
1273 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1274 $v = $cache->getWithSetCallback( $key, 60, $func );
1275 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1276 // Lock up the mutex so interim cache is used
1277 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1278 $v = $cache->getWithSetCallback( $key, 60, $func );
1279 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1283 * @covers WANObjectCache::touchCheckKey
1284 * @covers WANObjectCache::resetCheckKey
1285 * @covers WANObjectCache::getCheckKeyTime
1286 * @covers WANObjectCache::getMultiCheckKeyTime
1287 * @covers WANObjectCache::makePurgeValue
1288 * @covers WANObjectCache::parsePurgeValue
1290 public function testTouchKeys() {
1291 $cache = $this->cache
;
1292 $key = wfRandomString();
1294 $priorTime = microtime( true ); // reference time
1295 $cache->setTime( $priorTime );
1297 $newTime = $cache->addTime( 0.100 );
1298 $t0 = $cache->getCheckKeyTime( $key );
1299 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1301 $priorTime = $newTime;
1302 $newTime = $cache->addTime( 0.100 );
1303 $cache->touchCheckKey( $key );
1304 $t1 = $cache->getCheckKeyTime( $key );
1305 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1307 $t2 = $cache->getCheckKeyTime( $key );
1308 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1310 $cache->addTime( 0.100 );
1311 $cache->touchCheckKey( $key );
1312 $t3 = $cache->getCheckKeyTime( $key );
1313 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1315 $t4 = $cache->getCheckKeyTime( $key );
1316 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1318 $cache->addTime( 0.100 );
1319 $cache->resetCheckKey( $key );
1320 $t5 = $cache->getCheckKeyTime( $key );
1321 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1323 $t6 = $cache->getCheckKeyTime( $key );
1324 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1328 * @covers WANObjectCache::getMulti()
1330 public function testGetWithSeveralCheckKeys() {
1331 $key = wfRandomString();
1332 $tKey1 = wfRandomString();
1333 $tKey2 = wfRandomString();
1336 // Two check keys are newer (given hold-off) than $key, another is older
1337 $this->internalCache
->set(
1338 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1339 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1341 $this->internalCache
->set(
1342 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1343 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1345 $this->internalCache
->set(
1346 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1347 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1349 $this->cache
->set( $key, $value, 30 );
1352 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1353 $this->assertEquals( $value, $v, "Value matches" );
1354 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1355 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1359 * @covers WANObjectCache::reap()
1360 * @covers WANObjectCache::reapCheckKey()
1362 public function testReap() {
1363 $vKey1 = wfRandomString();
1364 $vKey2 = wfRandomString();
1365 $tKey1 = wfRandomString();
1366 $tKey2 = wfRandomString();
1369 $knownPurge = time() - 60;
1370 $goodTime = microtime( true ) - 5;
1371 $badTime = microtime( true ) - 300;
1373 $this->internalCache
->set(
1374 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1376 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1377 WANObjectCache
::FLD_VALUE
=> $value,
1378 WANObjectCache
::FLD_TTL
=> 3600,
1379 WANObjectCache
::FLD_TIME
=> $goodTime
1382 $this->internalCache
->set(
1383 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1385 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1386 WANObjectCache
::FLD_VALUE
=> $value,
1387 WANObjectCache
::FLD_TTL
=> 3600,
1388 WANObjectCache
::FLD_TIME
=> $badTime
1391 $this->internalCache
->set(
1392 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1393 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1395 $this->internalCache
->set(
1396 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1397 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1400 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1401 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1402 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1403 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1405 $this->assertFalse( $bad1 );
1406 $this->assertTrue( $bad2 );
1408 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1409 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1410 $this->assertFalse( $tBad1 );
1411 $this->assertTrue( $tBad2 );
1415 * @covers WANObjectCache::reap()
1417 public function testReap_fail() {
1418 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1419 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1420 $backend->expects( $this->once() )->method( 'get' )
1422 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1423 WANObjectCache
::FLD_VALUE
=> 'value',
1424 WANObjectCache
::FLD_TTL
=> 3600,
1425 WANObjectCache
::FLD_TIME
=> 300,
1427 $backend->expects( $this->once() )->method( 'changeTTL' )
1428 ->willReturn( false );
1430 $wanCache = new WANObjectCache( [
1431 'cache' => $backend,
1432 'pool' => 'testcache-hash',
1433 'relayer' => new EventRelayerNull( [] )
1437 $ret = $wanCache->reap( 'key', 360, $isStale );
1438 $this->assertTrue( $isStale, 'value was stale' );
1439 $this->assertFalse( $ret, 'changeTTL failed' );
1443 * @covers WANObjectCache::set()
1445 public function testSetWithLag() {
1448 $key = wfRandomString();
1449 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1450 $this->cache
->set( $key, $value, 30, $opts );
1451 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1453 $key = wfRandomString();
1454 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1455 $this->cache
->set( $key, $value, 30, $opts );
1456 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1458 $key = wfRandomString();
1459 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1460 $this->cache
->set( $key, $value, 30, $opts );
1461 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1465 * @covers WANObjectCache::set()
1467 public function testWritePending() {
1470 $key = wfRandomString();
1471 $opts = [ 'pending' => true ];
1472 $this->cache
->set( $key, $value, 30, $opts );
1473 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1476 public function testMcRouterSupport() {
1477 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1478 ->setMethods( [ 'set', 'delete' ] )->getMock();
1479 $localBag->expects( $this->never() )->method( 'set' );
1480 $localBag->expects( $this->never() )->method( 'delete' );
1481 $wanCache = new WANObjectCache( [
1482 'cache' => $localBag,
1483 'pool' => 'testcache-hash',
1484 'relayer' => new EventRelayerNull( [] ),
1485 'mcrouterAware' => true,
1486 'region' => 'pmtpa',
1487 'cluster' => 'mw-wan'
1489 $valFunc = function () {
1493 // None of these should use broadcasting commands (e.g. SET, DELETE)
1494 $wanCache->get( 'x' );
1495 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1496 $wanCache->getMulti( [ 'x', 'y' ] );
1497 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1498 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1499 $wanCache->getCheckKeyTime( 'zzz' );
1500 $wanCache->reap( 'x', time() - 300 );
1501 $wanCache->reap( 'zzz', time() - 300 );
1504 public function testMcRouterSupportBroadcastDelete() {
1505 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1506 ->setMethods( [ 'set' ] )->getMock();
1507 $wanCache = new WANObjectCache( [
1508 'cache' => $localBag,
1509 'pool' => 'testcache-hash',
1510 'relayer' => new EventRelayerNull( [] ),
1511 'mcrouterAware' => true,
1512 'region' => 'pmtpa',
1513 'cluster' => 'mw-wan'
1516 $localBag->expects( $this->once() )->method( 'set' )
1517 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX
. "test" );
1519 $wanCache->delete( 'test' );
1522 public function testMcRouterSupportBroadcastTouchCK() {
1523 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1524 ->setMethods( [ 'set' ] )->getMock();
1525 $wanCache = new WANObjectCache( [
1526 'cache' => $localBag,
1527 'pool' => 'testcache-hash',
1528 'relayer' => new EventRelayerNull( [] ),
1529 'mcrouterAware' => true,
1530 'region' => 'pmtpa',
1531 'cluster' => 'mw-wan'
1534 $localBag->expects( $this->once() )->method( 'set' )
1535 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1537 $wanCache->touchCheckKey( 'test' );
1540 public function testMcRouterSupportBroadcastResetCK() {
1541 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1542 ->setMethods( [ 'delete' ] )->getMock();
1543 $wanCache = new WANObjectCache( [
1544 'cache' => $localBag,
1545 'pool' => 'testcache-hash',
1546 'relayer' => new EventRelayerNull( [] ),
1547 'mcrouterAware' => true,
1548 'region' => 'pmtpa',
1549 'cluster' => 'mw-wan'
1552 $localBag->expects( $this->once() )->method( 'delete' )
1553 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1555 $wanCache->resetCheckKey( 'test' );
1559 * @dataProvider provideAdaptiveTTL
1560 * @covers WANObjectCache::adaptiveTTL()
1561 * @param float|int $ago
1562 * @param int $maxTTL
1563 * @param int $minTTL
1564 * @param float $factor
1565 * @param int $adaptiveTTL
1567 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1568 $mtime = $ago ?
time() - $ago : $ago;
1570 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1572 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1573 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1575 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1577 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1578 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1581 public static function provideAdaptiveTTL() {
1583 [ 3600, 900, 30, 0.2, 720 ],
1584 [ 3600, 500, 30, 0.2, 500 ],
1585 [ 3600, 86400, 800, 0.2, 800 ],
1586 [ false, 86400, 800, 0.2, 800 ],
1587 [ null, 86400, 800, 0.2, 800 ]
1592 * @covers WANObjectCache::__construct
1593 * @covers WANObjectCache::newEmpty
1595 public function testNewEmpty() {
1596 $this->assertInstanceOf(
1597 WANObjectCache
::class,
1598 WANObjectCache
::newEmpty()
1603 * @covers WANObjectCache::setLogger
1605 public function testSetLogger() {
1606 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1610 * @covers WANObjectCache::getQoS
1612 public function testGetQoS() {
1613 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1614 ->setMethods( [ 'getQoS' ] )->getMock();
1615 $backend->expects( $this->once() )->method( 'getQoS' )
1616 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1617 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1620 $wanCache::QOS_UNKNOWN
,
1621 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1626 * @covers WANObjectCache::makeKey
1628 public function testMakeKey() {
1629 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1630 ->setMethods( [ 'makeKey' ] )->getMock();
1631 $backend->expects( $this->once() )->method( 'makeKey' )
1632 ->willReturn( 'special' );
1634 $wanCache = new WANObjectCache( [
1635 'cache' => $backend,
1636 'pool' => 'testcache-hash',
1637 'relayer' => new EventRelayerNull( [] )
1640 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1644 * @covers WANObjectCache::makeGlobalKey
1646 public function testMakeGlobalKey() {
1647 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1648 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1649 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1650 ->willReturn( 'special' );
1652 $wanCache = new WANObjectCache( [
1653 'cache' => $backend,
1654 'pool' => 'testcache-hash',
1655 'relayer' => new EventRelayerNull( [] )
1658 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1661 public static function statsKeyProvider() {
1663 [ 'domain:page:5', 'page' ],
1664 [ 'domain:main-key', 'main-key' ],
1665 [ 'domain:page:history', 'page' ],
1666 [ 'missingdomainkey', 'missingdomainkey' ]
1671 * @dataProvider statsKeyProvider
1672 * @covers WANObjectCache::determineKeyClass
1674 public function testStatsKeyClass( $key, $class ) {
1675 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1676 'cache' => new HashBagOStuff
,
1677 'pool' => 'testcache-hash',
1678 'relayer' => new EventRelayerNull( [] )
1681 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1685 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1686 private $timeOverride = 0;
1688 public function setTime( $time ) {
1689 $this->timeOverride
= $time;
1692 protected function getCurrentTime() {
1693 return $this->timeOverride ?
: parent
::getCurrentTime();
1697 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1698 private $timeOverride = 0;
1700 public function setTime( $time ) {
1701 $this->timeOverride
= $time;
1702 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1703 $this->cache
->setTime( $time );
1707 public function addTime( $time ) {
1708 $this->timeOverride +
= $time;
1709 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1710 $this->cache
->setTime( $this->timeOverride
);
1713 return $this->timeOverride
;
1716 protected function getCurrentTime() {
1717 return $this->timeOverride ?
: parent
::getCurrentTime();
1721 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1722 const CLOCK_SKEW
= 1;
1724 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1725 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1729 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1730 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1731 return ( ( $now - $asOf ) > $timeTillRefresh );