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->assertEquals( 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->assertEquals( 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->assertEquals( 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->assertEquals( 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 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
771 WANObjectCache
::VALUE_KEY_PREFIX
. '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->assertEquals( 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->assertEquals( 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( $cache::MUTEX_KEY_PREFIX
. $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( $cache::MUTEX_KEY_PREFIX
. $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( $cache::MUTEX_KEY_PREFIX
. $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 testBusyValue() {
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, $cache, $key ) {
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( $cache::MUTEX_KEY_PREFIX
. $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( $cache::MUTEX_KEY_PREFIX
. $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( $cache::MUTEX_KEY_PREFIX
. $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' );
1129 * @covers WANObjectCache::getMulti()
1131 public function testGetMulti() {
1132 $cache = $this->cache
;
1134 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1135 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1137 $key1 = wfRandomString();
1138 $key2 = wfRandomString();
1139 $key3 = wfRandomString();
1141 $mockWallClock = 1549343530.2053;
1142 $priorTime = $mockWallClock; // reference time
1143 $cache->setMockTime( $mockWallClock );
1145 $cache->set( $key1, $value1, 5 );
1146 $cache->set( $key2, $value2, 10 );
1150 [ $key1 => $value1, $key2 => $value2 ],
1151 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1152 'Result array populated'
1155 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1156 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1157 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1159 $cKey1 = wfRandomString();
1160 $cKey2 = wfRandomString();
1162 $mockWallClock +
= 1;
1166 [ $key1 => $value1, $key2 => $value2 ],
1167 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1168 "Result array populated even with new check keys"
1170 $t1 = $cache->getCheckKeyTime( $cKey1 );
1171 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1172 $t2 = $cache->getCheckKeyTime( $cKey2 );
1173 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1174 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1175 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1176 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1178 $mockWallClock +
= 1;
1181 $this->assertEquals(
1182 [ $key1 => $value1, $key2 => $value2 ],
1183 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1184 "Result array still populated even with new check keys"
1186 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1187 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1188 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1192 * @covers WANObjectCache::getMulti()
1193 * @covers WANObjectCache::processCheckKeys()
1195 public function testGetMultiCheckKeys() {
1196 $cache = $this->cache
;
1198 $checkAll = wfRandomString();
1199 $check1 = wfRandomString();
1200 $check2 = wfRandomString();
1201 $check3 = wfRandomString();
1202 $value1 = wfRandomString();
1203 $value2 = wfRandomString();
1205 $mockWallClock = 1549343530.2053;
1206 $cache->setMockTime( $mockWallClock );
1208 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1209 // several seconds during the test to assert the behaviour.
1210 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1211 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1214 $mockWallClock +
= 0.100;
1216 $cache->set( 'key1', $value1, 10 );
1217 $cache->set( 'key2', $value2, 10 );
1220 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1227 [ 'key1' => $value1, 'key2' => $value2 ],
1231 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1232 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1233 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1234 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1236 $mockWallClock +
= 0.100;
1237 $cache->touchCheckKey( $check1 );
1240 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1247 [ 'key1' => $value1, 'key2' => $value2 ],
1249 'key1 expired by check1, but value still provided'
1251 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1252 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1254 $cache->touchCheckKey( $checkAll );
1257 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1263 $this->assertEquals(
1264 [ 'key1' => $value1, 'key2' => $value2 ],
1266 'All keys expired by checkAll, but value still provided'
1268 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1269 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1273 * @covers WANObjectCache::get()
1274 * @covers WANObjectCache::processCheckKeys()
1276 public function testCheckKeyInitHoldoff() {
1277 $cache = $this->cache
;
1279 for ( $i = 0; $i < 500; ++
$i ) {
1280 $key = wfRandomString();
1281 $checkKey = wfRandomString();
1283 $cache->get( $key, $curTTL, [ $checkKey ] );
1284 $cache->set( $key, 'val', 10 );
1286 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1288 $this->assertEquals( 'val', $v );
1289 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1292 for ( $i = 0; $i < 500; ++
$i ) {
1293 $key = wfRandomString();
1294 $checkKey = wfRandomString();
1296 $cache->set( $key, 'val', 10 );
1298 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1300 $this->assertEquals( 'val', $v );
1301 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1306 * @covers WANObjectCache::delete
1307 * @covers WANObjectCache::relayDelete
1308 * @covers WANObjectCache::relayPurge
1310 public function testDelete() {
1311 $key = wfRandomString();
1312 $value = wfRandomString();
1313 $this->cache
->set( $key, $value );
1316 $v = $this->cache
->get( $key, $curTTL );
1317 $this->assertEquals( $value, $v, "Key was created with value" );
1318 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1320 $this->cache
->delete( $key );
1323 $v = $this->cache
->get( $key, $curTTL );
1324 $this->assertFalse( $v, "Deleted key has false value" );
1325 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1327 $this->cache
->set( $key, $value . 'more' );
1328 $v = $this->cache
->get( $key, $curTTL );
1329 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1330 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1332 $this->cache
->set( $key, $value );
1333 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1336 $v = $this->cache
->get( $key, $curTTL );
1337 $this->assertFalse( $v, "Deleted key has false value" );
1338 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1340 $this->cache
->set( $key, $value );
1341 $v = $this->cache
->get( $key, $curTTL );
1342 $this->assertEquals( $value, $v, "Key was created with value" );
1343 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1347 * @dataProvider getWithSetCallback_versions_provider
1348 * @covers WANObjectCache::getWithSetCallback()
1349 * @covers WANObjectCache::fetchOrRegenerate()
1350 * @param array $extOpts
1351 * @param bool $versioned
1353 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1354 $cache = $this->cache
;
1356 $key = wfRandomString();
1357 $valueV1 = wfRandomString();
1358 $valueV2 = [ wfRandomString() ];
1361 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1367 $priorValue = false;
1369 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1370 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1371 $priorValue = $oldValue;
1372 $priorAsOf = $oldAsOf;
1375 return $valueV2; // new array format
1378 // Set the main key (version N if versioned)
1380 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1381 $this->assertEquals( $valueV1, $v, "Value returned" );
1382 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1383 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1384 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1385 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1388 // Set the key for version N+1 format
1389 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1391 // Start versioning now with the unversioned key still there
1392 $verOpts = [ 'version' => 1 ];
1395 // Value goes to secondary key since V1 already used $key
1397 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1398 $this->assertEquals( $valueV2, $v, "Value returned" );
1399 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1400 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1401 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1404 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1405 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1406 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1408 // Clear out the older or unversioned key
1409 $cache->delete( $key, 0 );
1411 // Set the key for next/first versioned format
1413 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1414 $this->assertEquals( $valueV2, $v, "Value returned" );
1415 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1417 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1418 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1419 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1422 public static function getWithSetCallback_versions_provider() {
1425 [ [ 'version' => 1 ], true ]
1430 * @covers WANObjectCache::useInterimHoldOffCaching
1431 * @covers WANObjectCache::getInterimValue
1433 public function testInterimHoldOffCaching() {
1434 $cache = $this->cache
;
1436 $mockWallClock = 1549343530.2053;
1437 $cache->setMockTime( $mockWallClock );
1439 $value = 'CRL-40-940';
1441 $func = function () use ( &$wasCalled, $value ) {
1447 $cache->useInterimHoldOffCaching( true );
1449 $key = wfRandomString( 32 );
1450 $v = $cache->getWithSetCallback( $key, 60, $func );
1451 $v = $cache->getWithSetCallback( $key, 60, $func );
1452 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1454 $cache->delete( $key );
1455 $mockWallClock +
= 0.001; // cached values will be newer than tombstone
1456 $v = $cache->getWithSetCallback( $key, 60, $func );
1457 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1458 $v = $cache->getWithSetCallback( $key, 60, $func );
1459 $this->assertEquals( 2, $wasCalled, 'Value interim cached' ); // reuses interim
1461 $mockWallClock +
= 0.2; // interim key not brand new
1462 $v = $cache->getWithSetCallback( $key, 60, $func );
1463 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1464 // Lock up the mutex so interim cache is used
1465 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1466 $v = $cache->getWithSetCallback( $key, 60, $func );
1467 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1468 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1470 $cache->useInterimHoldOffCaching( false );
1473 $key = wfRandomString( 32 );
1474 $v = $cache->getWithSetCallback( $key, 60, $func );
1475 $v = $cache->getWithSetCallback( $key, 60, $func );
1476 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1477 $cache->delete( $key );
1478 $v = $cache->getWithSetCallback( $key, 60, $func );
1479 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1480 $v = $cache->getWithSetCallback( $key, 60, $func );
1481 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1482 $v = $cache->getWithSetCallback( $key, 60, $func );
1483 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1484 // Lock up the mutex so interim cache is used
1485 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1486 $v = $cache->getWithSetCallback( $key, 60, $func );
1487 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1491 * @covers WANObjectCache::touchCheckKey
1492 * @covers WANObjectCache::resetCheckKey
1493 * @covers WANObjectCache::getCheckKeyTime
1494 * @covers WANObjectCache::getMultiCheckKeyTime
1495 * @covers WANObjectCache::makePurgeValue
1496 * @covers WANObjectCache::parsePurgeValue
1498 public function testTouchKeys() {
1499 $cache = $this->cache
;
1500 $key = wfRandomString();
1502 $mockWallClock = 1549343530.2053;
1503 $priorTime = $mockWallClock; // reference time
1504 $cache->setMockTime( $mockWallClock );
1506 $mockWallClock +
= 0.100;
1507 $t0 = $cache->getCheckKeyTime( $key );
1508 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1510 $priorTime = $mockWallClock;
1511 $mockWallClock +
= 0.100;
1512 $cache->touchCheckKey( $key );
1513 $t1 = $cache->getCheckKeyTime( $key );
1514 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1516 $t2 = $cache->getCheckKeyTime( $key );
1517 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1519 $mockWallClock +
= 0.100;
1520 $cache->touchCheckKey( $key );
1521 $t3 = $cache->getCheckKeyTime( $key );
1522 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1524 $t4 = $cache->getCheckKeyTime( $key );
1525 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1527 $mockWallClock +
= 0.100;
1528 $cache->resetCheckKey( $key );
1529 $t5 = $cache->getCheckKeyTime( $key );
1530 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1532 $t6 = $cache->getCheckKeyTime( $key );
1533 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1537 * @covers WANObjectCache::getMulti()
1539 public function testGetWithSeveralCheckKeys() {
1540 $key = wfRandomString();
1541 $tKey1 = wfRandomString();
1542 $tKey2 = wfRandomString();
1545 $mockWallClock = 1549343530.2053;
1546 $priorTime = $mockWallClock; // reference time
1547 $this->cache
->setMockTime( $mockWallClock );
1549 // Two check keys are newer (given hold-off) than $key, another is older
1550 $this->internalCache
->set(
1551 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1552 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 3 )
1554 $this->internalCache
->set(
1555 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1556 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 5 )
1558 $this->internalCache
->set(
1559 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1560 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 30 )
1562 $this->cache
->set( $key, $value, 30 );
1565 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1566 $this->assertEquals( $value, $v, "Value matches" );
1567 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1568 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1572 * @covers WANObjectCache::reap()
1573 * @covers WANObjectCache::reapCheckKey()
1575 public function testReap() {
1576 $vKey1 = wfRandomString();
1577 $vKey2 = wfRandomString();
1578 $tKey1 = wfRandomString();
1579 $tKey2 = wfRandomString();
1582 $knownPurge = time() - 60;
1583 $goodTime = microtime( true ) - 5;
1584 $badTime = microtime( true ) - 300;
1586 $this->internalCache
->set(
1587 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1589 WANObjectCache
::FLD_FORMAT_VERSION
=> WANObjectCache
::VERSION
,
1590 WANObjectCache
::FLD_VALUE
=> $value,
1591 WANObjectCache
::FLD_TTL
=> 3600,
1592 WANObjectCache
::FLD_TIME
=> $goodTime
1595 $this->internalCache
->set(
1596 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1598 WANObjectCache
::FLD_FORMAT_VERSION
=> WANObjectCache
::VERSION
,
1599 WANObjectCache
::FLD_VALUE
=> $value,
1600 WANObjectCache
::FLD_TTL
=> 3600,
1601 WANObjectCache
::FLD_TIME
=> $badTime
1604 $this->internalCache
->set(
1605 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1606 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1608 $this->internalCache
->set(
1609 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1610 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1613 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1614 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1615 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1616 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1618 $this->assertFalse( $bad1 );
1619 $this->assertTrue( $bad2 );
1621 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1622 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1623 $this->assertFalse( $tBad1 );
1624 $this->assertTrue( $tBad2 );
1628 * @covers WANObjectCache::reap()
1630 public function testReap_fail() {
1631 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1632 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1633 $backend->expects( $this->once() )->method( 'get' )
1635 WANObjectCache
::FLD_FORMAT_VERSION
=> WANObjectCache
::VERSION
,
1636 WANObjectCache
::FLD_VALUE
=> 'value',
1637 WANObjectCache
::FLD_TTL
=> 3600,
1638 WANObjectCache
::FLD_TIME
=> 300,
1640 $backend->expects( $this->once() )->method( 'changeTTL' )
1641 ->willReturn( false );
1643 $wanCache = new WANObjectCache( [
1648 $ret = $wanCache->reap( 'key', 360, $isStale );
1649 $this->assertTrue( $isStale, 'value was stale' );
1650 $this->assertFalse( $ret, 'changeTTL failed' );
1654 * @covers WANObjectCache::set()
1656 public function testSetWithLag() {
1659 $key = wfRandomString();
1660 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1661 $this->cache
->set( $key, $value, 30, $opts );
1662 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1664 $key = wfRandomString();
1665 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1666 $this->cache
->set( $key, $value, 30, $opts );
1667 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1669 $key = wfRandomString();
1670 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1671 $this->cache
->set( $key, $value, 30, $opts );
1672 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1676 * @covers WANObjectCache::set()
1678 public function testWritePending() {
1681 $key = wfRandomString();
1682 $opts = [ 'pending' => true ];
1683 $this->cache
->set( $key, $value, 30, $opts );
1684 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1687 public function testMcRouterSupport() {
1688 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1689 ->setMethods( [ 'set', 'delete' ] )->getMock();
1690 $localBag->expects( $this->never() )->method( 'set' );
1691 $localBag->expects( $this->never() )->method( 'delete' );
1692 $wanCache = new WANObjectCache( [
1693 'cache' => $localBag,
1694 'mcrouterAware' => true,
1695 'region' => 'pmtpa',
1696 'cluster' => 'mw-wan'
1698 $valFunc = function () {
1702 // None of these should use broadcasting commands (e.g. SET, DELETE)
1703 $wanCache->get( 'x' );
1704 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1705 $wanCache->getMulti( [ 'x', 'y' ] );
1706 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1707 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1708 $wanCache->getCheckKeyTime( 'zzz' );
1709 $wanCache->reap( 'x', time() - 300 );
1710 $wanCache->reap( 'zzz', time() - 300 );
1713 public function testMcRouterSupportBroadcastDelete() {
1714 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1715 ->setMethods( [ 'set' ] )->getMock();
1716 $wanCache = new WANObjectCache( [
1717 'cache' => $localBag,
1718 'mcrouterAware' => true,
1719 'region' => 'pmtpa',
1720 'cluster' => 'mw-wan'
1723 $localBag->expects( $this->once() )->method( 'set' )
1724 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX
. "test" );
1726 $wanCache->delete( 'test' );
1729 public function testMcRouterSupportBroadcastTouchCK() {
1730 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1731 ->setMethods( [ 'set' ] )->getMock();
1732 $wanCache = new WANObjectCache( [
1733 'cache' => $localBag,
1734 'mcrouterAware' => true,
1735 'region' => 'pmtpa',
1736 'cluster' => 'mw-wan'
1739 $localBag->expects( $this->once() )->method( 'set' )
1740 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1742 $wanCache->touchCheckKey( 'test' );
1745 public function testMcRouterSupportBroadcastResetCK() {
1746 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1747 ->setMethods( [ 'delete' ] )->getMock();
1748 $wanCache = new WANObjectCache( [
1749 'cache' => $localBag,
1750 'mcrouterAware' => true,
1751 'region' => 'pmtpa',
1752 'cluster' => 'mw-wan'
1755 $localBag->expects( $this->once() )->method( 'delete' )
1756 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1758 $wanCache->resetCheckKey( 'test' );
1761 public function testEpoch() {
1762 $bag = new HashBagOStuff();
1763 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1764 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1766 $now = microtime( true );
1767 $cache->setMockTime( $now );
1769 $cache->set( $key, 'Do what thou Wilt' );
1770 $cache->touchCheckKey( $key );
1774 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1775 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1777 $cache = new WANObjectCache( [
1779 'epoch' => $now - 3600
1781 $cache->setMockTime( $now );
1783 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1784 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1787 $cache = new WANObjectCache( [
1789 'epoch' => $now +
3600
1791 $cache->setMockTime( $now );
1793 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1794 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1798 * @dataProvider provideAdaptiveTTL
1799 * @covers WANObjectCache::adaptiveTTL()
1800 * @param float|int $ago
1801 * @param int $maxTTL
1802 * @param int $minTTL
1803 * @param float $factor
1804 * @param int $adaptiveTTL
1806 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1807 $mtime = $ago ?
time() - $ago : $ago;
1809 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1811 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1812 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1814 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1816 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1817 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1820 public static function provideAdaptiveTTL() {
1822 [ 3600, 900, 30, 0.2, 720 ],
1823 [ 3600, 500, 30, 0.2, 500 ],
1824 [ 3600, 86400, 800, 0.2, 800 ],
1825 [ false, 86400, 800, 0.2, 800 ],
1826 [ null, 86400, 800, 0.2, 800 ]
1831 * @covers WANObjectCache::__construct
1832 * @covers WANObjectCache::newEmpty
1834 public function testNewEmpty() {
1835 $this->assertInstanceOf(
1836 WANObjectCache
::class,
1837 WANObjectCache
::newEmpty()
1842 * @covers WANObjectCache::setLogger
1844 public function testSetLogger() {
1845 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1849 * @covers WANObjectCache::getQoS
1851 public function testGetQoS() {
1852 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1853 ->setMethods( [ 'getQoS' ] )->getMock();
1854 $backend->expects( $this->once() )->method( 'getQoS' )
1855 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1856 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1859 $wanCache::QOS_UNKNOWN
,
1860 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1865 * @covers WANObjectCache::makeKey
1867 public function testMakeKey() {
1868 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1869 ->setMethods( [ 'makeKey' ] )->getMock();
1870 $backend->expects( $this->once() )->method( 'makeKey' )
1871 ->willReturn( 'special' );
1873 $wanCache = new WANObjectCache( [
1877 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1881 * @covers WANObjectCache::makeGlobalKey
1883 public function testMakeGlobalKey() {
1884 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1885 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1886 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1887 ->willReturn( 'special' );
1889 $wanCache = new WANObjectCache( [
1893 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1896 public static function statsKeyProvider() {
1898 [ 'domain:page:5', 'page' ],
1899 [ 'domain:main-key', 'main-key' ],
1900 [ 'domain:page:history', 'page' ],
1901 [ 'missingdomainkey', 'missingdomainkey' ]
1906 * @dataProvider statsKeyProvider
1907 * @covers WANObjectCache::determineKeyClassForStats
1909 public function testStatsKeyClass( $key, $class ) {
1910 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1911 'cache' => new HashBagOStuff
1914 $this->assertEquals( $class, $wanCache->determineKeyClassForStats( $key ) );
1918 * @covers WANObjectCache::makeMultiKeys
1920 public function testMakeMultiKeys() {
1921 $cache = $this->cache
;
1923 $ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ];
1924 $keyCallback = function ( $id, WANObjectCache
$cache ) {
1925 return $cache->makeKey( 'key', $id );
1927 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1938 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1940 $ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ];
1941 $keyCallback = function ( $id, WANObjectCache
$cache ) {
1942 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
1944 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1947 "global:key:1:a:1:b" => '1',
1948 "global:key:2:a:2:b" => '2',
1949 "global:key:3:a:3:b" => '3',
1950 "global:key:4:a:4:b" => '4',
1951 "global:key:5:a:5:b" => '5',
1952 "global:key:6:a:6:b" => '6',
1953 "global:key:7:a:7:b" => '7'
1955 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1959 * @covers WANObjectCache::makeMultiKeys
1961 public function testMakeMultiKeysIntString() {
1962 $cache = $this->cache
;
1963 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ];
1964 $keyCallback = function ( $id, WANObjectCache
$cache ) {
1965 return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' );
1968 $keyedIds = $cache->makeMultiKeys( $ids, $keyCallback );
1971 "global:key:1:a:1:b" => 1,
1972 "global:key:2:a:2:b" => 2,
1973 "global:key:3:a:3:b" => 3,
1974 "global:key:4:a:4:b" => 4,
1975 "global:key:5:a:5:b" => 5,
1976 "global:key:6:a:6:b" => 6,
1977 "global:key:7:a:7:b" => 7
1979 $this->assertSame( $expected, iterator_to_array( $keyedIds ) );
1983 * @covers WANObjectCache::makeMultiKeys
1984 * @expectedException UnexpectedValueException
1986 public function testMakeMultiKeysCollision() {
1987 $ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ];
1989 $this->cache
->makeMultiKeys(
1992 return "keymod:" . $id %
3;
1998 * @covers WANObjectCache::multiRemap
2000 public function testMultiRemap() {
2001 $a = [ 'a', 'b', 'c' ];
2002 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ];
2004 $this->assertEquals(
2005 [ 'a' => 1, 'b' => 2, 'c' => 3 ],
2006 $this->cache
->multiRemap( $a, $res )
2009 $a = [ 'a', 'b', 'c', 'c', 'd' ];
2010 $res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3, 'keyD' => 4 ];
2012 $this->assertEquals(
2013 [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ],
2014 $this->cache
->multiRemap( $a, $res )
2019 * @covers WANObjectCache::hash256
2021 public function testHash256() {
2022 $bag = new HashBagOStuff();
2023 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 5 ] );
2024 $this->assertEquals(
2025 'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c',
2026 $cache->hash256( 'x' )
2029 $cache = new WANObjectCache( [ 'cache' => $bag, 'epoch' => 50 ] );
2030 $this->assertEquals(
2031 'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e',
2032 $cache->hash256( 'x' )
2035 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden' ] );
2036 $this->assertEquals(
2037 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2038 $cache->hash256( 'x' )
2041 $cache = new WANObjectCache( [ 'cache' => $bag, 'secret' => 'garden', 'epoch' => 3 ] );
2042 $this->assertEquals(
2043 '48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093',
2044 $cache->hash256( 'x' )
2049 class NearExpiringWANObjectCache
extends WANObjectCache
{
2050 const CLOCK_SKEW
= 1;
2052 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
2053 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
2057 class PopularityRefreshingWANObjectCache
extends WANObjectCache
{
2058 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
2059 return ( ( $now - $asOf ) > $timeTillRefresh );