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::getNonProcessCachedMultiKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
19 class WANObjectCacheTest
extends PHPUnit\Framework\TestCase
{
21 use MediaWikiCoversValidator
;
22 use PHPUnit4And6Compat
;
24 /** @var WANObjectCache */
27 private $internalCache;
29 protected function setUp() {
32 $this->cache
= new WANObjectCache( [
33 'cache' => new HashBagOStuff()
36 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
37 /** @noinspection PhpUndefinedFieldInspection */
38 $this->internalCache
= $wanCache->cache
;
42 * @dataProvider provideSetAndGet
43 * @covers WANObjectCache::set()
44 * @covers WANObjectCache::get()
45 * @covers WANObjectCache::makeKey()
49 public function testSetAndGet( $value, $ttl ) {
50 $cache = $this->cache
;
54 $key = $cache->makeKey( 'x', wfRandomString() );
56 $cache->get( $key, $curTTL, [], $asOf );
57 $this->assertNull( $curTTL, "Current TTL is null" );
58 $this->assertNull( $asOf, "Current as-of-time is infinite" );
60 $t = microtime( true );
62 $cache->set( $key, $value, $cache::TTL_UNCACHEABLE
);
63 $cache->get( $key, $curTTL, [], $asOf );
64 $this->assertNull( $curTTL, "Current TTL is null (TTL_UNCACHEABLE)" );
65 $this->assertNull( $asOf, "Current as-of-time is infinite (TTL_UNCACHEABLE)" );
67 $cache->set( $key, $value, $ttl );
69 $this->assertEquals( $value, $cache->get( $key, $curTTL, [], $asOf ) );
70 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
71 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
73 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
74 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
76 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
77 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
80 public static function provideSetAndGet() {
87 [ (object)[ 'meow' ], 3 ],
95 * @covers WANObjectCache::get()
96 * @covers WANObjectCache::makeGlobalKey()
98 public function testGetNotExists() {
99 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
101 $value = $this->cache
->get( $key, $curTTL );
103 $this->assertFalse( $value, "Non-existing key has false value" );
104 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
108 * @covers WANObjectCache::set()
110 public function testSetOver() {
111 $key = wfRandomString();
112 for ( $i = 0; $i < 3; ++
$i ) {
113 $value = wfRandomString();
114 $this->cache
->set( $key, $value, 3 );
116 $this->assertEquals( $this->cache
->get( $key ), $value );
121 * @covers WANObjectCache::set()
123 public function testStaleSet() {
124 $key = wfRandomString();
125 $value = wfRandomString();
126 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
128 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
132 * @covers WANObjectCache::getWithSetCallback
134 public function testProcessCacheLruAndDelete() {
135 $cache = $this->cache
;
136 $mockWallClock = 1549343530.2053;
137 $cache->setMockTime( $mockWallClock );
140 $fn = function () use ( &$hit ) {
144 $keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
145 $keysB = [ wfRandomString(), wfRandomString(), wfRandomString() ];
146 $pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
148 foreach ( $keysA as $i => $key ) {
149 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
151 $this->assertEquals( 3, $hit, "Values not cached yet" );
153 foreach ( $keysA as $i => $key ) {
154 // Should not evict from process cache
155 $cache->delete( $key );
156 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
158 $this->assertEquals( 3, $hit, "Values cached; not cleared by delete()" );
160 foreach ( $keysB as $i => $key ) {
161 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
163 $this->assertEquals( 6, $hit, "New values not cached yet" );
165 foreach ( $keysB as $i => $key ) {
166 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
168 $this->assertEquals( 6, $hit, "New values cached" );
170 foreach ( $keysA as $i => $key ) {
171 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
173 $this->assertEquals( 9, $hit, "Prior values evicted by new values" );
177 * @covers WANObjectCache::getWithSetCallback
179 public function testProcessCacheInterimKeys() {
180 $cache = $this->cache
;
181 $mockWallClock = 1549343530.2053;
182 $cache->setMockTime( $mockWallClock );
185 $fn = function () use ( &$hit ) {
189 $keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ];
190 $pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
192 foreach ( $keysA as $i => $key ) {
193 $cache->delete( $key ); // tombstone key
194 $mockWallClock +
= 0.001; // cached values will be newer than tombstone
195 // Get into process cache (specific group) and interim cache
196 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] );
198 $this->assertEquals( 3, $hit );
200 // Get into process cache (default group)
201 $key = reset( $keysA );
202 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
203 $this->assertEquals( 3, $hit, "Value recently interim-cached" );
205 $mockWallClock +
= 0.2; // interim key not brand new
206 $cache->clearProcessCache();
207 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
208 $this->assertEquals( 4, $hit, "Value calculated (interim key not recent and reset)" );
209 $cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] );
210 $this->assertEquals( 4, $hit, "Value process cached" );
214 * @covers WANObjectCache::getWithSetCallback
216 public function testProcessCacheNesting() {
217 $cache = $this->cache
;
218 $mockWallClock = 1549343530.2053;
219 $cache->setMockTime( $mockWallClock );
221 $keyOuter = "outer-" . wfRandomString();
222 $keyInner = "inner-" . wfRandomString();
225 $innerFn = function () use ( &$innerHit ) {
231 $outerFn = function () use ( $keyInner, $innerFn, $cache, &$outerHit ) {
233 $v = $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
238 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
239 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
241 $this->assertEquals( 1, $innerHit, "Inner callback value cached" );
242 $cache->delete( $keyInner, $cache::HOLDOFF_NONE
);
245 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
246 $this->assertEquals( 1, $innerHit, "Inner callback process cached" );
248 // Outer key misses and inner key process cache value is refused
249 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
251 $this->assertEquals( 1, $outerHit, "Outer callback value not yet cached" );
252 $this->assertEquals( 2, $innerHit, "Inner callback value process cache skipped" );
254 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
256 $this->assertEquals( 1, $outerHit, "Outer callback value cached" );
258 $cache->delete( $keyInner, $cache::HOLDOFF_NONE
);
259 $cache->delete( $keyOuter, $cache::HOLDOFF_NONE
);
261 $cache->clearProcessCache();
262 $cache->getWithSetCallback( $keyOuter, 100, $outerFn );
264 $this->assertEquals( 2, $outerHit, "Outer callback value not yet cached" );
265 $this->assertEquals( 3, $innerHit, "Inner callback value not yet cached" );
267 $cache->delete( $keyInner, $cache::HOLDOFF_NONE
);
269 $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] );
271 $this->assertEquals( 3, $innerHit, "Inner callback value process cached" );
275 * @dataProvider getWithSetCallback_provider
276 * @covers WANObjectCache::getWithSetCallback()
277 * @covers WANObjectCache::fetchOrRegenerate()
278 * @param array $extOpts
279 * @param bool $versioned
281 public function testGetWithSetCallback( array $extOpts, $versioned ) {
282 $cache = $this->cache
;
284 $key = wfRandomString();
285 $value = wfRandomString();
286 $cKey1 = wfRandomString();
287 $cKey2 = wfRandomString();
292 $func = function ( $old, &$ttl, &$opts, $asOf )
293 use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) {
297 $ttl = 20; // override with another value
301 $mockWallClock = 1549343530.2053;
302 $priorTime = $mockWallClock; // reference time
303 $cache->setMockTime( $mockWallClock );
306 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
307 $this->assertEquals( $value, $v, "Value returned" );
308 $this->assertEquals( 1, $wasSet, "Value regenerated" );
309 $this->assertFalse( $priorValue, "No prior value" );
310 $this->assertNull( $priorAsOf, "No prior value" );
313 $cache->get( $key, $curTTL );
314 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
315 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
318 $v = $cache->getWithSetCallback(
319 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
320 $this->assertEquals( $value, $v, "Value returned" );
321 $this->assertSame( 0, $wasSet, "Value not regenerated" );
326 $v = $cache->getWithSetCallback(
327 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
329 $this->assertEquals( $value, $v, "Value returned" );
330 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
331 $this->assertEquals( $value, $priorValue, "Has prior value" );
332 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
333 $t1 = $cache->getCheckKeyTime( $cKey1 );
334 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
335 $t2 = $cache->getCheckKeyTime( $cKey2 );
336 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
338 $mockWallClock +
= 0.2; // interim key is not brand new and check keys have past values
339 $priorTime = $mockWallClock; // reference time
341 $v = $cache->getWithSetCallback(
342 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
344 $this->assertEquals( $value, $v, "Value returned" );
345 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
346 $t1 = $cache->getCheckKeyTime( $cKey1 );
347 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
348 $t2 = $cache->getCheckKeyTime( $cKey2 );
349 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
352 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
353 $this->assertEquals( $value, $v, "Value returned" );
354 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
357 $key = wfRandomString();
358 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
359 $this->assertEquals( $value, $v, "Value returned" );
360 $cache->delete( $key );
361 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
362 $this->assertEquals( $value, $v, "Value still returned after deleted" );
363 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
365 $oldValReceived = -1;
366 $oldAsOfReceived = -1;
367 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
368 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
370 $oldValReceived = $oldVal;
371 $oldAsOfReceived = $oldAsOf;
373 return 'xxx' . $wasSet;
376 $mockWallClock = 1549343530.2053;
377 $priorTime = $mockWallClock; // reference time
380 $key = wfRandomString();
381 $v = $cache->getWithSetCallback(
382 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
383 $this->assertEquals( 'xxx1', $v, "Value returned" );
384 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
385 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
387 $mockWallClock +
= 40;
388 $v = $cache->getWithSetCallback(
389 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
390 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
391 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
392 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
393 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
395 $mockWallClock +
= 260;
396 $v = $cache->getWithSetCallback(
397 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
398 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
399 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
400 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
401 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
403 $mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL
- 1 );
405 $key = wfRandomString();
406 $checkKey = $cache->makeKey( 'template', 'X' );
407 $cache->touchCheckKey( $checkKey ); // init check key
408 $mockWallClock = $priorTime;
409 $v = $cache->getWithSetCallback(
411 $cache::TTL_INDEFINITE
,
413 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
415 $this->assertEquals( 'xxx1', $v, "Value returned" );
416 $this->assertEquals( 1, $wasSet, "Value computed" );
417 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
418 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
420 $mockWallClock +
= $cache::TTL_HOUR
; // some time passes
421 $v = $cache->getWithSetCallback(
423 $cache::TTL_INDEFINITE
,
425 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
427 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
428 $this->assertEquals( 1, $wasSet, "Cached value returned" );
430 $cache->touchCheckKey( $checkKey ); // make key stale
431 $mockWallClock +
= 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
433 $v = $cache->getWithSetCallback(
435 $cache::TTL_INDEFINITE
,
437 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
439 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
440 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
442 // Chance of refresh increase to unity as staleness approaches graceTTL
443 $mockWallClock +
= $cache::TTL_WEEK
; // 8 days of being stale
444 $v = $cache->getWithSetCallback(
446 $cache::TTL_INDEFINITE
,
448 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
450 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
451 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
452 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
453 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
457 * @dataProvider getWithSetCallback_provider
458 * @covers WANObjectCache::getWithSetCallback()
459 * @covers WANObjectCache::fetchOrRegenerate()
460 * @param array $extOpts
461 * @param bool $versioned
463 function testGetWithSetcallback_touched( array $extOpts, $versioned ) {
464 $cache = $this->cache
;
466 $mockWallClock = 1549343530.2053;
467 $cache->setMockTime( $mockWallClock );
469 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
473 return 'xxx' . $wasSet;
476 $key = wfRandomString();
479 $touchedCallback = function () use ( &$touched ) {
482 $v = $cache->getWithSetCallback(
484 $cache::TTL_INDEFINITE
,
486 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
488 $mockWallClock +
= 60;
489 $v = $cache->getWithSetCallback(
491 $cache::TTL_INDEFINITE
,
493 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
495 $this->assertEquals( 'xxx1', $v, "Value was computed once" );
496 $this->assertEquals( 1, $wasSet, "Value was computed once" );
498 $touched = $mockWallClock - 10;
499 $v = $cache->getWithSetCallback(
501 $cache::TTL_INDEFINITE
,
503 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
505 $v = $cache->getWithSetCallback(
507 $cache::TTL_INDEFINITE
,
509 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
511 $this->assertEquals( 'xxx2', $v, "Value was recomputed once" );
512 $this->assertEquals( 2, $wasSet, "Value was recomputed once" );
515 public static function getWithSetCallback_provider() {
518 [ [ 'version' => 1 ], true ]
522 public function testPreemtiveRefresh() {
525 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
531 $cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] );
532 $mockWallClock = 1549343530.2053;
533 $cache->setMockTime( $mockWallClock );
536 $key = wfRandomString();
537 $opts = [ 'lowTTL' => 30 ];
538 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
539 $this->assertEquals( $value, $v, "Value returned" );
540 $this->assertEquals( 1, $wasSet, "Value calculated" );
542 $mockWallClock +
= 0.2; // interim key is not brand new
543 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
544 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
547 $key = wfRandomString();
548 $opts = [ 'lowTTL' => 1 ];
549 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
550 $this->assertEquals( $value, $v, "Value returned" );
551 $this->assertEquals( 1, $wasSet, "Value calculated" );
552 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
553 $this->assertEquals( 1, $wasSet, "Value cached" );
556 $asyncHandler = function ( $callback ) use ( &$asycList ) {
557 $asycList[] = $callback;
559 $cache = new NearExpiringWANObjectCache( [
560 'cache' => new HashBagOStuff(),
561 'asyncHandler' => $asyncHandler
564 $mockWallClock = 1549343530.2053;
565 $priorTime = $mockWallClock; // reference time
566 $cache->setMockTime( $mockWallClock );
569 $key = wfRandomString();
570 $opts = [ 'lowTTL' => 100 ];
571 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
572 $this->assertEquals( $value, $v, "Value returned" );
573 $this->assertEquals( 1, $wasSet, "Value calculated" );
574 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
575 $this->assertEquals( 1, $wasSet, "Cached value used" );
576 $this->assertEquals( $v, $value, "Value cached" );
578 $mockWallClock +
= 250;
579 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
580 $this->assertEquals( $value, $v, "Value returned" );
581 $this->assertEquals( 1, $wasSet, "Stale value used" );
582 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
583 $value = 'NewCatsInTown'; // change callback return value
584 $asycList[0](); // run the refresh callback
586 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
587 $this->assertSame( 0, count( $asycList ), "No deferred refreshes added." );
588 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
589 $this->assertEquals( $value, $v, "New value stored" );
591 $cache = new PopularityRefreshingWANObjectCache( [
592 'cache' => new HashBagOStuff()
595 $mockWallClock = $priorTime;
596 $cache->setMockTime( $mockWallClock );
599 $key = wfRandomString();
600 $opts = [ 'hotTTR' => 900 ];
601 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
602 $this->assertEquals( $value, $v, "Value returned" );
603 $this->assertEquals( 1, $wasSet, "Value calculated" );
605 $mockWallClock +
= 30;
607 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
608 $this->assertEquals( 1, $wasSet, "Value cached" );
610 $mockWallClock = $priorTime;
612 $key = wfRandomString();
613 $opts = [ 'hotTTR' => 10 ];
614 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
615 $this->assertEquals( $value, $v, "Value returned" );
616 $this->assertEquals( 1, $wasSet, "Value calculated" );
618 $mockWallClock +
= 30;
620 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
621 $this->assertEquals( $value, $v, "Value returned" );
622 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
626 * @dataProvider getMultiWithSetCallback_provider
627 * @covers WANObjectCache::getMultiWithSetCallback
628 * @covers WANObjectCache::makeMultiKeys
629 * @covers WANObjectCache::getMulti
630 * @param array $extOpts
631 * @param bool $versioned
633 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
634 $cache = $this->cache
;
636 $keyA = wfRandomString();
637 $keyB = wfRandomString();
638 $keyC = wfRandomString();
639 $cKey1 = wfRandomString();
640 $cKey2 = wfRandomString();
645 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
646 &$wasSet, &$priorValue, &$priorAsOf
651 $ttl = 20; // override with another value
655 $mockWallClock = 1549343530.2053;
656 $priorTime = $mockWallClock; // reference time
657 $cache->setMockTime( $mockWallClock );
660 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
662 $v = $cache->getMultiWithSetCallback(
663 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
664 $this->assertEquals( $value, $v[$keyA], "Value returned" );
665 $this->assertEquals( 1, $wasSet, "Value regenerated" );
666 $this->assertFalse( $priorValue, "No prior value" );
667 $this->assertNull( $priorAsOf, "No prior value" );
670 $cache->get( $keyA, $curTTL );
671 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
672 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
676 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
677 $v = $cache->getMultiWithSetCallback(
678 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
679 $this->assertEquals( $value, $v[$keyB], "Value returned" );
680 $this->assertEquals( 1, $wasSet, "Value regenerated" );
681 $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
683 $v = $cache->getMultiWithSetCallback(
684 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
685 $this->assertEquals( $value, $v[$keyB], "Value returned" );
686 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
687 $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
692 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
693 $v = $cache->getMultiWithSetCallback(
694 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
696 $this->assertEquals( $value, $v[$keyB], "Value returned" );
697 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
698 $this->assertEquals( $value, $priorValue, "Has prior value" );
699 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
700 $t1 = $cache->getCheckKeyTime( $cKey1 );
701 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
702 $t2 = $cache->getCheckKeyTime( $cKey2 );
703 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
705 $mockWallClock +
= 0.01;
706 $priorTime = $mockWallClock;
709 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
710 $v = $cache->getMultiWithSetCallback(
711 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
713 $this->assertEquals( $value, $v[$keyC], "Value returned" );
714 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
715 $t1 = $cache->getCheckKeyTime( $cKey1 );
716 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
717 $t2 = $cache->getCheckKeyTime( $cKey2 );
718 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
721 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
722 $this->assertEquals( $value, $v, "Value returned" );
723 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
726 $key = wfRandomString();
727 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
728 $v = $cache->getMultiWithSetCallback(
729 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
730 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
731 $cache->delete( $key );
732 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
733 $v = $cache->getMultiWithSetCallback(
734 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
735 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
736 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
739 $ids = [ 1, 2, 3, 4, 5, 6 ];
740 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
741 return $wanCache->makeKey( 'test', $id );
743 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
744 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
749 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
752 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
753 array_values( $values ),
754 "Correct values in correct order"
757 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
758 array_keys( $values ),
759 "Correct keys in correct order"
761 $this->assertEquals( count( $ids ), $calls );
763 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
764 $this->assertEquals( count( $ids ), $calls, "Values cached" );
766 // Mock the BagOStuff to assure only one getMulti() call given process caching
767 $localBag = $this->getMockBuilder( HashBagOStuff
::class )
768 ->setMethods( [ 'getMulti' ] )->getMock();
769 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
770 'WANCache:v:' . 'k1' => 'val-id1',
771 'WANCache:v:' . 'k2' => 'val-id2'
773 $wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
775 // Warm the process cache
776 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
778 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
779 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
781 // Use the process cache
783 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
784 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
788 public static function getMultiWithSetCallback_provider() {
791 [ [ 'version' => 1 ], true ]
796 * @dataProvider getMultiWithUnionSetCallback_provider
797 * @covers WANObjectCache::getMultiWithUnionSetCallback()
798 * @covers WANObjectCache::makeMultiKeys()
799 * @param array $extOpts
800 * @param bool $versioned
802 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
803 $cache = $this->cache
;
805 $keyA = wfRandomString();
806 $keyB = wfRandomString();
807 $keyC = wfRandomString();
808 $cKey1 = wfRandomString();
809 $cKey2 = wfRandomString();
812 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
813 &$wasSet, &$priorValue, &$priorAsOf
816 foreach ( $ids as $id ) {
818 $newValues[$id] = "@$id$";
819 $ttls[$id] = 20; // override with another value
825 $mockWallClock = 1549343530.2053;
826 $priorTime = $mockWallClock; // reference time
827 $cache->setMockTime( $mockWallClock );
830 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
832 $v = $cache->getMultiWithUnionSetCallback(
833 $keyedIds, 30, $genFunc, $extOpts );
834 $this->assertEquals( $value, $v[$keyA], "Value returned" );
835 $this->assertEquals( 1, $wasSet, "Value regenerated" );
838 $cache->get( $keyA, $curTTL );
839 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
840 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
844 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
845 $v = $cache->getMultiWithUnionSetCallback(
846 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
847 $this->assertEquals( $value, $v[$keyB], "Value returned" );
848 $this->assertEquals( 1, $wasSet, "Value regenerated" );
849 $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
851 $v = $cache->getMultiWithUnionSetCallback(
852 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
853 $this->assertEquals( $value, $v[$keyB], "Value returned" );
854 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
855 $this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" );
860 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
861 $v = $cache->getMultiWithUnionSetCallback(
862 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
864 $this->assertEquals( $value, $v[$keyB], "Value returned" );
865 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
866 $t1 = $cache->getCheckKeyTime( $cKey1 );
867 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
868 $t2 = $cache->getCheckKeyTime( $cKey2 );
869 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
871 $mockWallClock +
= 0.01;
872 $priorTime = $mockWallClock;
875 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
876 $v = $cache->getMultiWithUnionSetCallback(
877 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
879 $this->assertEquals( $value, $v[$keyC], "Value returned" );
880 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
881 $t1 = $cache->getCheckKeyTime( $cKey1 );
882 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
883 $t2 = $cache->getCheckKeyTime( $cKey2 );
884 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
887 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
888 $this->assertEquals( $value, $v, "Value returned" );
889 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
892 $key = wfRandomString();
893 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
894 $v = $cache->getMultiWithUnionSetCallback(
895 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
896 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
897 $cache->delete( $key );
898 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
899 $v = $cache->getMultiWithUnionSetCallback(
900 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
901 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
902 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
905 $ids = [ 1, 2, 3, 4, 5, 6 ];
906 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
907 return $wanCache->makeKey( 'test', $id );
909 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
910 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
912 foreach ( $ids as $id ) {
914 $newValues[$id] = "val-{$id}";
919 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
922 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
923 array_values( $values ),
924 "Correct values in correct order"
927 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
928 array_keys( $values ),
929 "Correct keys in correct order"
931 $this->assertEquals( count( $ids ), $calls );
933 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
934 $this->assertEquals( count( $ids ), $calls, "Values cached" );
937 public static function getMultiWithUnionSetCallback_provider() {
940 [ [ 'version' => 1 ], true ]
945 * @covers WANObjectCache::getWithSetCallback()
946 * @covers WANObjectCache::fetchOrRegenerate()
948 public function testLockTSE() {
949 $cache = $this->cache
;
950 $key = wfRandomString();
951 $value = wfRandomString();
953 $mockWallClock = 1549343530.2053;
954 $cache->setMockTime( $mockWallClock );
957 $func = function () use ( &$calls, $value, $cache, $key ) {
962 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
963 $this->assertEquals( $value, $ret );
964 $this->assertEquals( 1, $calls, 'Value was populated' );
966 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
967 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
969 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
970 $ret = $cache->getWithSetCallback( $key, 30, $func,
971 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
972 $this->assertEquals( $value, $ret, 'Old value used' );
973 $this->assertEquals( 1, $calls, 'Callback was not used' );
975 $cache->delete( $key );
976 $mockWallClock +
= 0.001; // cached values will be newer than tombstone
977 $ret = $cache->getWithSetCallback( $key, 30, $func,
978 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
979 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
980 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
982 $ret = $cache->getWithSetCallback( $key, 30, $func,
983 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
984 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
985 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
989 * @covers WANObjectCache::getWithSetCallback()
990 * @covers WANObjectCache::fetchOrRegenerate()
991 * @covers WANObjectCache::set()
993 public function testLockTSESlow() {
994 $cache = $this->cache
;
995 $key = wfRandomString();
996 $key2 = wfRandomString();
997 $value = wfRandomString();
999 $mockWallClock = 1549343530.2053;
1000 $cache->setMockTime( $mockWallClock );
1003 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, &$mockWallClock ) {
1005 $setOpts['since'] = $mockWallClock - 10;
1009 // Value should be given a low logical TTL due to snapshot lag
1011 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1012 $this->assertEquals( $value, $ret );
1013 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
1014 $this->assertEquals( 1, $curTTL, 'Value has reduced logical TTL', 0.01 );
1015 $this->assertEquals( 1, $calls, 'Value was generated' );
1017 $mockWallClock +
= 2; // low logical TTL expired
1019 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1020 $this->assertEquals( $value, $ret );
1021 $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
1023 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1024 $this->assertEquals( $value, $ret );
1025 $this->assertEquals( 2, $calls, 'Callback was not used (interim value used)' );
1027 $mockWallClock +
= 2; // low logical TTL expired
1028 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
1029 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1031 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1032 $this->assertEquals( $value, $ret );
1033 $this->assertEquals( 2, $calls, 'Callback was not used (mutex not acquired)' );
1035 $mockWallClock +
= 301; // physical TTL expired
1036 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
1037 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1039 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
1040 $this->assertEquals( $value, $ret );
1041 $this->assertEquals( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' );
1044 $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
1046 $setOpts['lag'] = 15;
1050 // Value should be given a low logical TTL due to replication lag
1052 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1053 $this->assertEquals( $value, $ret );
1054 $this->assertEquals( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
1055 $this->assertEquals( 30, $curTTL, 'Value has reduced logical TTL', 0.01 );
1056 $this->assertEquals( 1, $calls, 'Value was generated' );
1058 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1059 $this->assertEquals( $value, $ret );
1060 $this->assertEquals( 1, $calls, 'Callback was used (not expired)' );
1062 $mockWallClock +
= 31;
1064 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
1065 $this->assertEquals( $value, $ret );
1066 $this->assertEquals( 2, $calls, 'Callback was used (mutex acquired)' );
1070 * @covers WANObjectCache::getWithSetCallback()
1071 * @covers WANObjectCache::fetchOrRegenerate()
1073 public function testBusyValueBasic() {
1074 $cache = $this->cache
;
1075 $key = wfRandomString();
1076 $value = wfRandomString();
1077 $busyValue = wfRandomString();
1079 $mockWallClock = 1549343530.2053;
1080 $cache->setMockTime( $mockWallClock );
1083 $func = function () use ( &$calls, $value ) {
1088 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
1089 $this->assertEquals( $value, $ret );
1090 $this->assertEquals( 1, $calls, 'Value was populated' );
1092 $mockWallClock +
= 0.2; // interim keys not brand new
1094 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
1095 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1097 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
1098 $ret = $cache->getWithSetCallback( $key, 30, $func,
1099 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1100 $this->assertEquals( $value, $ret, 'Callback used' );
1101 $this->assertEquals( 2, $calls, 'Callback used' );
1103 $ret = $cache->getWithSetCallback( $key, 30, $func,
1104 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1105 $this->assertEquals( $value, $ret, 'Old value used' );
1106 $this->assertEquals( 2, $calls, 'Callback was not used' );
1108 $cache->delete( $key ); // no value at all anymore and still locked
1109 $ret = $cache->getWithSetCallback( $key, 30, $func,
1110 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1111 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
1112 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
1114 $this->internalCache
->delete( 'WANCache:m:' . $key );
1115 $mockWallClock +
= 0.001; // cached values will be newer than tombstone
1116 $ret = $cache->getWithSetCallback( $key, 30, $func,
1117 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1118 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
1119 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
1121 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1122 $ret = $cache->getWithSetCallback( $key, 30, $func,
1123 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1124 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
1125 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
1128 public function getBusyValues_Provider() {
1129 $hash = new HashBagOStuff( [] );
1134 return "Saint Oliver Plunckett";
1136 'Saint Oliver Plunckett'
1138 [ 'strlen', 'strlen' ],
1139 [ 'WANObjectCache::newEmpty', 'WANObjectCache::newEmpty' ],
1140 [ [ 'WANObjectCache', 'newEmpty' ], [ 'WANObjectCache', 'newEmpty' ] ],
1141 [ [ $hash, 'getLastError' ], [ $hash, 'getLastError' ] ],
1142 [ [ 1, 2, 3 ], [ 1, 2, 3 ] ]
1147 * @covers WANObjectCache::getWithSetCallback()
1148 * @covers WANObjectCache::fetchOrRegenerate()
1149 * @dataProvider getBusyValues_Provider
1150 * @param mixed $busyValue
1151 * @param mixed $expected
1153 public function testBusyValueTypes( $busyValue, $expected ) {
1154 $cache = $this->cache
;
1155 $key = wfRandomString();
1157 $mockWallClock = 1549343530.2053;
1158 $cache->setMockTime( $mockWallClock );
1161 $func = function () use ( &$calls ) {
1166 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
1167 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1169 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
1170 $this->assertSame( $expected, $ret, 'busyValue used as expected' );
1171 $this->assertSame( 0, $calls, 'busyValue was used' );
1175 * @covers WANObjectCache::getMulti()
1177 public function testGetMulti() {
1178 $cache = $this->cache
;
1180 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1181 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1183 $key1 = wfRandomString();
1184 $key2 = wfRandomString();
1185 $key3 = wfRandomString();
1187 $mockWallClock = 1549343530.2053;
1188 $priorTime = $mockWallClock; // reference time
1189 $cache->setMockTime( $mockWallClock );
1191 $cache->set( $key1, $value1, 5 );
1192 $cache->set( $key2, $value2, 10 );
1196 [ $key1 => $value1, $key2 => $value2 ],
1197 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1198 'Result array populated'
1201 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1202 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1203 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1205 $cKey1 = wfRandomString();
1206 $cKey2 = wfRandomString();
1208 $mockWallClock +
= 1;
1212 [ $key1 => $value1, $key2 => $value2 ],
1213 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1214 "Result array populated even with new check keys"
1216 $t1 = $cache->getCheckKeyTime( $cKey1 );
1217 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1218 $t2 = $cache->getCheckKeyTime( $cKey2 );
1219 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1220 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1221 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1222 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1224 $mockWallClock +
= 1;
1227 $this->assertEquals(
1228 [ $key1 => $value1, $key2 => $value2 ],
1229 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1230 "Result array still populated even with new check keys"
1232 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1233 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1234 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1238 * @covers WANObjectCache::getMulti()
1239 * @covers WANObjectCache::processCheckKeys()
1241 public function testGetMultiCheckKeys() {
1242 $cache = $this->cache
;
1244 $checkAll = wfRandomString();
1245 $check1 = wfRandomString();
1246 $check2 = wfRandomString();
1247 $check3 = wfRandomString();
1248 $value1 = wfRandomString();
1249 $value2 = wfRandomString();
1251 $mockWallClock = 1549343530.2053;
1252 $cache->setMockTime( $mockWallClock );
1254 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1255 // several seconds during the test to assert the behaviour.
1256 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1257 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_TTL_NONE
);
1260 $mockWallClock +
= 0.100;
1262 $cache->set( 'key1', $value1, 10 );
1263 $cache->set( 'key2', $value2, 10 );
1266 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1273 [ 'key1' => $value1, 'key2' => $value2 ],
1277 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1278 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1279 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1280 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1282 $mockWallClock +
= 0.100;
1283 $cache->touchCheckKey( $check1 );
1286 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1293 [ 'key1' => $value1, 'key2' => $value2 ],
1295 'key1 expired by check1, but value still provided'
1297 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1298 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1300 $cache->touchCheckKey( $checkAll );
1303 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1309 $this->assertEquals(
1310 [ 'key1' => $value1, 'key2' => $value2 ],
1312 'All keys expired by checkAll, but value still provided'
1314 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1315 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1319 * @covers WANObjectCache::get()
1320 * @covers WANObjectCache::processCheckKeys()
1322 public function testCheckKeyInitHoldoff() {
1323 $cache = $this->cache
;
1325 for ( $i = 0; $i < 500; ++
$i ) {
1326 $key = wfRandomString();
1327 $checkKey = wfRandomString();
1329 $cache->get( $key, $curTTL, [ $checkKey ] );
1330 $cache->set( $key, 'val', 10 );
1332 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1334 $this->assertEquals( 'val', $v );
1335 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1338 for ( $i = 0; $i < 500; ++
$i ) {
1339 $key = wfRandomString();
1340 $checkKey = wfRandomString();
1342 $cache->set( $key, 'val', 10 );
1344 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1346 $this->assertEquals( 'val', $v );
1347 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1352 * @covers WANObjectCache::get()
1353 * @covers WANObjectCache::processCheckKeys()
1355 public function testCheckKeyHoldoff() {
1356 $cache = $this->cache
;
1357 $key = wfRandomString();
1358 $checkKey = wfRandomString();
1360 $mockWallClock = 1549343530.2053;
1361 $cache->setMockTime( $mockWallClock );
1362 $cache->touchCheckKey( $checkKey, 8 );
1364 $mockWallClock +
= 1;
1365 $cache->set( $key, 1, 60 );
1366 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1367 $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
1369 $mockWallClock +
= 3;
1370 $cache->set( $key, 1, 60 );
1371 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1372 $this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" );
1374 $mockWallClock +
= 10;
1375 $cache->set( $key, 1, 60 );
1376 $this->assertEquals( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) );
1377 $this->assertGreaterThan( 0, $curTTL, "Key not in hold-off due to check key" );
1381 * @covers WANObjectCache::delete
1382 * @covers WANObjectCache::relayDelete
1383 * @covers WANObjectCache::relayPurge
1385 public function testDelete() {
1386 $key = wfRandomString();
1387 $value = wfRandomString();
1388 $this->cache
->set( $key, $value );
1391 $v = $this->cache
->get( $key, $curTTL );
1392 $this->assertEquals( $value, $v, "Key was created with value" );
1393 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1395 $this->cache
->delete( $key );
1398 $v = $this->cache
->get( $key, $curTTL );
1399 $this->assertFalse( $v, "Deleted key has false value" );
1400 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1402 $this->cache
->set( $key, $value . 'more' );
1403 $v = $this->cache
->get( $key, $curTTL );
1404 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1405 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1407 $this->cache
->set( $key, $value );
1408 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_TTL_NONE
);
1411 $v = $this->cache
->get( $key, $curTTL );
1412 $this->assertFalse( $v, "Deleted key has false value" );
1413 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1415 $this->cache
->set( $key, $value );
1416 $v = $this->cache
->get( $key, $curTTL );
1417 $this->assertEquals( $value, $v, "Key was created with value" );
1418 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1422 * @dataProvider getWithSetCallback_versions_provider
1423 * @covers WANObjectCache::getWithSetCallback()
1424 * @covers WANObjectCache::fetchOrRegenerate()
1425 * @param array $extOpts
1426 * @param bool $versioned
1428 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1429 $cache = $this->cache
;
1431 $key = wfRandomString();
1432 $valueV1 = wfRandomString();
1433 $valueV2 = [ wfRandomString() ];
1436 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1442 $priorValue = false;
1444 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1445 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1446 $priorValue = $oldValue;
1447 $priorAsOf = $oldAsOf;
1450 return $valueV2; // new array format
1453 // Set the main key (version N if versioned)
1455 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1456 $this->assertEquals( $valueV1, $v, "Value returned" );
1457 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1458 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1459 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1460 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1463 // Set the key for version N+1 format
1464 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1466 // Start versioning now with the unversioned key still there
1467 $verOpts = [ 'version' => 1 ];
1470 // Value goes to secondary key since V1 already used $key
1472 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1473 $this->assertEquals( $valueV2, $v, "Value returned" );
1474 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1475 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1476 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1479 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1480 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1481 $this->assertSame( 0, $wasSet, "Value not regenerated (secondary key)" );
1483 // Clear out the older or unversioned key
1484 $cache->delete( $key, 0 );
1486 // Set the key for next/first versioned format
1488 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1489 $this->assertEquals( $valueV2, $v, "Value returned" );
1490 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1492 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1493 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1494 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1497 public static function getWithSetCallback_versions_provider() {
1500 [ [ 'version' => 1 ], true ]
1505 * @covers WANObjectCache::useInterimHoldOffCaching
1506 * @covers WANObjectCache::getInterimValue
1508 public function testInterimHoldOffCaching() {
1509 $cache = $this->cache
;
1511 $mockWallClock = 1549343530.2053;
1512 $cache->setMockTime( $mockWallClock );
1514 $value = 'CRL-40-940';
1516 $func = function () use ( &$wasCalled, $value ) {
1522 $cache->useInterimHoldOffCaching( true );
1524 $key = wfRandomString( 32 );
1525 $v = $cache->getWithSetCallback( $key, 60, $func );
1526 $v = $cache->getWithSetCallback( $key, 60, $func );
1527 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1529 $cache->delete( $key );
1530 $mockWallClock +
= 0.001; // cached values will be newer than tombstone
1531 $v = $cache->getWithSetCallback( $key, 60, $func );
1532 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1533 $v = $cache->getWithSetCallback( $key, 60, $func );
1534 $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
1536 $mockWallClock +
= 0.2; // interim key not brand new
1537 $v = $cache->getWithSetCallback( $key, 60, $func );
1538 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1539 // Lock up the mutex so interim cache is used
1540 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1541 $v = $cache->getWithSetCallback( $key, 60, $func );
1542 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1543 $this->internalCache
->delete( 'WANCache:m:' . $key );
1545 $cache->useInterimHoldOffCaching( false );
1548 $key = wfRandomString( 32 );
1549 $v = $cache->getWithSetCallback( $key, 60, $func );
1550 $v = $cache->getWithSetCallback( $key, 60, $func );
1551 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1552 $cache->delete( $key );
1553 $v = $cache->getWithSetCallback( $key, 60, $func );
1554 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1555 $v = $cache->getWithSetCallback( $key, 60, $func );
1556 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1557 $v = $cache->getWithSetCallback( $key, 60, $func );
1558 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1559 // Lock up the mutex so interim cache is used
1560 $this->internalCache
->add( 'WANCache:m:' . $key, 1, 0 );
1561 $v = $cache->getWithSetCallback( $key, 60, $func );
1562 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1566 * @covers WANObjectCache::touchCheckKey
1567 * @covers WANObjectCache::resetCheckKey
1568 * @covers WANObjectCache::getCheckKeyTime
1569 * @covers WANObjectCache::getMultiCheckKeyTime
1570 * @covers WANObjectCache::makePurgeValue
1571 * @covers WANObjectCache::parsePurgeValue
1573 public function testTouchKeys() {
1574 $cache = $this->cache
;
1575 $key = wfRandomString();
1577 $mockWallClock = 1549343530.2053;
1578 $priorTime = $mockWallClock; // reference time
1579 $cache->setMockTime( $mockWallClock );
1581 $mockWallClock +
= 0.100;
1582 $t0 = $cache->getCheckKeyTime( $key );
1583 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1585 $priorTime = $mockWallClock;
1586 $mockWallClock +
= 0.100;
1587 $cache->touchCheckKey( $key );
1588 $t1 = $cache->getCheckKeyTime( $key );
1589 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1591 $t2 = $cache->getCheckKeyTime( $key );
1592 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1594 $mockWallClock +
= 0.100;
1595 $cache->touchCheckKey( $key );
1596 $t3 = $cache->getCheckKeyTime( $key );
1597 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1599 $t4 = $cache->getCheckKeyTime( $key );
1600 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1602 $mockWallClock +
= 0.100;
1603 $cache->resetCheckKey( $key );
1604 $t5 = $cache->getCheckKeyTime( $key );
1605 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1607 $t6 = $cache->getCheckKeyTime( $key );
1608 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1612 * @covers WANObjectCache::getMulti()
1614 public function testGetWithSeveralCheckKeys() {
1615 $key = wfRandomString();
1616 $tKey1 = wfRandomString();
1617 $tKey2 = wfRandomString();
1620 $mockWallClock = 1549343530.2053;
1621 $priorTime = $mockWallClock; // reference time
1622 $this->cache
->setMockTime( $mockWallClock );
1624 // Two check keys are newer (given hold-off) than $key, another is older
1625 $this->internalCache
->set(
1626 'WANCache:t:' . $tKey2,
1627 'PURGED:' . ( $priorTime - 3 )
1629 $this->internalCache
->set(
1630 'WANCache:t:' . $tKey2,
1631 'PURGED:' . ( $priorTime - 5 )
1633 $this->internalCache
->set(
1634 'WANCache:t:' . $tKey1,
1635 'PURGED:' . ( $priorTime - 30 )
1637 $this->cache
->set( $key, $value, 30 );
1640 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1641 $this->assertEquals( $value, $v, "Value matches" );
1642 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1643 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1647 * @covers WANObjectCache::reap()
1648 * @covers WANObjectCache::reapCheckKey()
1650 public function testReap() {
1651 $vKey1 = wfRandomString();
1652 $vKey2 = wfRandomString();
1653 $tKey1 = wfRandomString();
1654 $tKey2 = wfRandomString();
1657 $knownPurge = time() - 60;
1658 $goodTime = microtime( true ) - 5;
1659 $badTime = microtime( true ) - 300;
1661 $this->internalCache
->set(
1662 'WANCache:v:' . $vKey1,
1670 $this->internalCache
->set(
1671 'WANCache:v:' . $vKey2,
1679 $this->internalCache
->set(
1680 'WANCache:t:' . $tKey1,
1681 'PURGED:' . $goodTime
1683 $this->internalCache
->set(
1684 'WANCache:t:' . $tKey2,
1685 'PURGED:' . $badTime
1688 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1689 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1690 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1691 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1693 $this->assertFalse( $bad1 );
1694 $this->assertTrue( $bad2 );
1696 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1697 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1698 $this->assertFalse( $tBad1 );
1699 $this->assertTrue( $tBad2 );
1703 * @covers WANObjectCache::reap()
1705 public function testReap_fail() {
1706 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1707 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1708 $backend->expects( $this->once() )->method( 'get' )
1715 $backend->expects( $this->once() )->method( 'changeTTL' )
1716 ->willReturn( false );
1718 $wanCache = new WANObjectCache( [
1723 $ret = $wanCache->reap( 'key', 360, $isStale );
1724 $this->assertTrue( $isStale, 'value was stale' );
1725 $this->assertFalse( $ret, 'changeTTL failed' );
1729 * @covers WANObjectCache::set()
1731 public function testSetWithLag() {
1734 $key = wfRandomString();
1735 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1736 $this->cache
->set( $key, $value, 30, $opts );
1737 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1739 $key = wfRandomString();
1740 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1741 $this->cache
->set( $key, $value, 30, $opts );
1742 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1744 $key = wfRandomString();
1745 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1746 $this->cache
->set( $key, $value, 30, $opts );
1747 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1751 * @covers WANObjectCache::set()
1753 public function testWritePending() {
1756 $key = wfRandomString();
1757 $opts = [ 'pending' => true ];
1758 $this->cache
->set( $key, $value, 30, $opts );
1759 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1762 public function testMcRouterSupport() {
1763 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1764 ->setMethods( [ 'set', 'delete' ] )->getMock();
1765 $localBag->expects( $this->never() )->method( 'set' );
1766 $localBag->expects( $this->never() )->method( 'delete' );
1767 $wanCache = new WANObjectCache( [
1768 'cache' => $localBag,
1769 'mcrouterAware' => true,
1770 'region' => 'pmtpa',
1771 'cluster' => 'mw-wan'
1773 $valFunc = function () {
1777 // None of these should use broadcasting commands (e.g. SET, DELETE)
1778 $wanCache->get( 'x' );
1779 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1780 $wanCache->getMulti( [ 'x', 'y' ] );
1781 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1782 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1783 $wanCache->getCheckKeyTime( 'zzz' );
1784 $wanCache->reap( 'x', time() - 300 );
1785 $wanCache->reap( 'zzz', time() - 300 );
1788 public function testMcRouterSupportBroadcastDelete() {
1789 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1790 ->setMethods( [ 'set' ] )->getMock();
1791 $wanCache = new WANObjectCache( [
1792 'cache' => $localBag,
1793 'mcrouterAware' => true,
1794 'region' => 'pmtpa',
1795 'cluster' => 'mw-wan'
1798 $localBag->expects( $this->once() )->method( 'set' )
1799 ->with( "/*/mw-wan/" . 'WANCache:v:' . "test" );
1801 $wanCache->delete( 'test' );
1804 public function testMcRouterSupportBroadcastTouchCK() {
1805 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1806 ->setMethods( [ 'set' ] )->getMock();
1807 $wanCache = new WANObjectCache( [
1808 'cache' => $localBag,
1809 'mcrouterAware' => true,
1810 'region' => 'pmtpa',
1811 'cluster' => 'mw-wan'
1814 $localBag->expects( $this->once() )->method( 'set' )
1815 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1817 $wanCache->touchCheckKey( 'test' );
1820 public function testMcRouterSupportBroadcastResetCK() {
1821 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1822 ->setMethods( [ 'delete' ] )->getMock();
1823 $wanCache = new WANObjectCache( [
1824 'cache' => $localBag,
1825 'mcrouterAware' => true,
1826 'region' => 'pmtpa',
1827 'cluster' => 'mw-wan'
1830 $localBag->expects( $this->once() )->method( 'delete' )
1831 ->with( "/*/mw-wan/" . 'WANCache:t:' . "test" );
1833 $wanCache->resetCheckKey( 'test' );
1836 public function testEpoch() {
1837 $bag = new HashBagOStuff();
1838 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1839 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1841 $now = microtime( true );
1842 $cache->setMockTime( $now );
1844 $cache->set( $key, 'Do what thou Wilt' );
1845 $cache->touchCheckKey( $key );
1849 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1850 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1852 $cache = new WANObjectCache( [
1854 'epoch' => $now - 3600
1856 $cache->setMockTime( $now );
1858 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1859 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1862 $cache = new WANObjectCache( [
1864 'epoch' => $now +
3600
1866 $cache->setMockTime( $now );
1868 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1869 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1873 * @dataProvider provideAdaptiveTTL
1874 * @covers WANObjectCache::adaptiveTTL()
1875 * @param float|int $ago
1876 * @param int $maxTTL
1877 * @param int $minTTL
1878 * @param float $factor
1879 * @param int $adaptiveTTL
1881 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1882 $mtime = $ago ?
time() - $ago : $ago;
1884 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1886 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1887 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1889 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1891 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1892 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1895 public static function provideAdaptiveTTL() {
1897 [ 3600, 900, 30, 0.2, 720 ],
1898 [ 3600, 500, 30, 0.2, 500 ],
1899 [ 3600, 86400, 800, 0.2, 800 ],
1900 [ false, 86400, 800, 0.2, 800 ],
1901 [ null, 86400, 800, 0.2, 800 ]
1906 * @covers WANObjectCache::__construct
1907 * @covers WANObjectCache::newEmpty
1909 public function testNewEmpty() {
1910 $this->assertInstanceOf(
1911 WANObjectCache
::class,
1912 WANObjectCache
::newEmpty()
1917 * @covers WANObjectCache::setLogger
1919 public function testSetLogger() {
1920 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1924 * @covers WANObjectCache::getQoS
1926 public function testGetQoS() {
1927 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1928 ->setMethods( [ 'getQoS' ] )->getMock();
1929 $backend->expects( $this->once() )->method( 'getQoS' )
1930 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1931 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1934 $wanCache::QOS_UNKNOWN
,
1935 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1940 * @covers WANObjectCache::makeKey
1942 public function testMakeKey() {
1943 if ( defined( 'HHVM_VERSION' ) ) {
1944 $this->markTestSkipped( 'HHVM Reflection buggy' );
1947 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1948 ->setMethods( [ 'makeKey' ] )->getMock();
1949 $backend->expects( $this->once() )->method( 'makeKey' )
1950 ->willReturn( 'special' );
1952 $wanCache = new WANObjectCache( [
1956 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1960 * @covers WANObjectCache::makeGlobalKey
1962 public function testMakeGlobalKey() {
1963 if ( defined( 'HHVM_VERSION' ) ) {
1964 $this->markTestSkipped( 'HHVM Reflection buggy' );
1967 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1968 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1969 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1970 ->willReturn( 'special' );
1972 $wanCache = new WANObjectCache( [
1976 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1979 public static function statsKeyProvider() {
1981 [ 'domain:page:5', 'page' ],
1982 [ 'domain:main-key', 'main-key' ],
1983 [ 'domain:page:history', 'page' ],
1984 // Regression test for T232907
1985 [ 'domain:foo-bar-1.2:abc:v2', 'foo-bar-1_2' ],
1986 [ 'missingdomainkey', 'missingdomainkey' ]
1991 * @dataProvider statsKeyProvider
1992 * @covers WANObjectCache::determineKeyClassForStats
1994 public function testStatsKeyClass( $key, $class ) {
1995 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1996 'cache' => new HashBagOStuff
1999 $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
2003 * @covers WANObjectCache::makeMultiKeys
2005 public function testMakeMultiKeys() {
2006 $cache = $this->cache
;
2008 $ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
2009 $keyCallback = function ( $id, WANObjectCache
$cache ) {
2010 return $cache->makeKey( 'key', $id );
2012 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2023 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2025 $ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
2026 $keyCallback = function ( $id, WANObjectCache
$cache ) {
2027 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
2029 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2032 "global:key:1:a:1:b" => '1',
2033 "global:key:2:a:2:b" => '2',
2034 "global:key:3:a:3:b" => '3',
2035 "global:key:4:a:4:b" => '4',
2036 "global:key:5:a:5:b" => '5',
2037 "global:key:6:a:6:b" => '6',
2038 "global:key:7:a:7:b" => '7'
2040 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2044 * @covers WANObjectCache::makeMultiKeys
2046 public function testMakeMultiKeysIntString() {
2047 $cache = $this->cache
;
2048 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
2049 $keyCallback = function ( $id, WANObjectCache
$cache ) {
2050 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
2053 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
2056 "global:key:1:a:1:b" => 1,
2057 "global:key:2:a:2:b" => 2,
2058 "global:key:3:a:3:b" => 3,
2059 "global:key:4:a:4:b" => 4,
2060 "global:key:5:a:5:b" => 5,
2061 "global:key:6:a:6:b" => 6,
2062 "global:key:7:a:7:b" => 7
2064 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
2068 * @covers WANObjectCache::makeMultiKeys
2069 * @expectedException UnexpectedValueException
2071 public function testMakeMultiKeysCollision() {
2072 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
2074 $this->cache
->makeMultiKeys(
2077 return "keymod:" . $id %
3;
2083 * @covers WANObjectCache::multiRemap
2085 public function testMultiRemap() {
2086 $a = [ 'a', 'b', 'c' ];
2087 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
2089 $this->assertEquals(
2090 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
2091 $this->cache
->multiRemap( $a, $res )
2094 $a = [ 'a', 'b', 'c', 'c', 'd' ];
2095 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3, 'keyD' => 4 ];
2097 $this->assertEquals(
2098 [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ],
2099 $this->cache
->multiRemap( $a, $res )
2104 * @covers WANObjectCache::hash256
2106 public function testHash256() {
2107 $bag = new HashBagOStuff();
2108 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 5 ] );
2109 $this->assertEquals(
2110 'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
2111 $cache->hash256( 'x' )
2114 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 50 ] );
2115 $this->assertEquals(
2116 'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
2117 $cache->hash256( 'x' )
2120 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden' ] );
2121 $this->assertEquals(
2122 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2123 $cache->hash256( 'x' )
2126 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden', 'epoch' => 3 ] );
2127 $this->assertEquals(
2128 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2129 $cache->hash256( 'x' )
2134 class NearExpiringWANObjectCache
extends WANObjectCache
{
2135 const CLOCK_SKEW
= 1;
2137 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
2138 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
2142 class PopularityRefreshingWANObjectCache
extends WANObjectCache
{
2143 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
2144 return ( ( $now - $asOf ) > $timeTillRefresh );