3 use Wikimedia\TestingAccessWrapper
;
6 * @covers WANObjectCache::wrap
7 * @covers WANObjectCache::unwrap
8 * @covers WANObjectCache::worthRefreshExpiring
9 * @covers WANObjectCache::worthRefreshPopular
10 * @covers WANObjectCache::isValid
11 * @covers WANObjectCache::getWarmupKeyMisses
12 * @covers WANObjectCache::prefixCacheKeys
13 * @covers WANObjectCache::getProcessCache
14 * @covers WANObjectCache::getNonProcessCachedKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
19 class WANObjectCacheTest
extends PHPUnit\Framework\TestCase
{
21 use MediaWikiCoversValidator
;
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 ) {
52 $key = $this->cache
->makeKey( 'x', wfRandomString() );
54 $this->cache
->get( $key, $curTTL, [], $asOf );
55 $this->assertNull( $curTTL, "Current TTL is null" );
56 $this->assertNull( $asOf, "Current as-of-time is infinite" );
58 $t = microtime( true );
59 $this->cache
->set( $key, $value, $ttl );
61 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
62 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
63 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
65 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
66 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
68 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
69 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
72 public static function provideSetAndGet() {
79 [ (object)[ 'meow' ], 3 ],
87 * @covers WANObjectCache::get()
88 * @covers WANObjectCache::makeGlobalKey()
90 public function testGetNotExists() {
91 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
93 $value = $this->cache
->get( $key, $curTTL );
95 $this->assertFalse( $value, "Non-existing key has false value" );
96 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
100 * @covers WANObjectCache::set()
102 public function testSetOver() {
103 $key = wfRandomString();
104 for ( $i = 0; $i < 3; ++
$i ) {
105 $value = wfRandomString();
106 $this->cache
->set( $key, $value, 3 );
108 $this->assertEquals( $this->cache
->get( $key ), $value );
113 * @covers WANObjectCache::set()
115 public function testStaleSet() {
116 $key = wfRandomString();
117 $value = wfRandomString();
118 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
120 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
123 public function testProcessCache() {
125 $callback = function () use ( &$hit ) {
129 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
130 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
132 foreach ( $keys as $i => $key ) {
133 $this->cache
->getWithSetCallback(
134 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
136 $this->assertEquals( 3, $hit );
138 foreach ( $keys as $i => $key ) {
139 $this->cache
->getWithSetCallback(
140 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
142 $this->assertEquals( 3, $hit, "Values cached" );
144 foreach ( $keys as $i => $key ) {
145 $this->cache
->getWithSetCallback(
146 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
148 $this->assertEquals( 6, $hit );
150 foreach ( $keys as $i => $key ) {
151 $this->cache
->getWithSetCallback(
152 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
154 $this->assertEquals( 6, $hit, "New values cached" );
156 foreach ( $keys as $i => $key ) {
157 $this->cache
->delete( $key );
158 $this->cache
->getWithSetCallback(
159 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
161 $this->assertEquals( 9, $hit, "Values evicted" );
163 $key = reset( $keys );
164 // Get into cache (default process cache group)
165 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
166 $this->assertEquals( 10, $hit, "Value calculated" );
167 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
168 $this->assertEquals( 10, $hit, "Value cached" );
169 $outerCallback = function () use ( &$callback, $key ) {
170 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
174 // Outer key misses and refuses inner key process cache value
175 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
176 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
180 * @dataProvider getWithSetCallback_provider
181 * @covers WANObjectCache::getWithSetCallback()
182 * @covers WANObjectCache::doGetWithSetCallback()
183 * @param array $extOpts
184 * @param bool $versioned
186 public function testGetWithSetCallback( array $extOpts, $versioned ) {
187 $cache = $this->cache
;
189 $key = wfRandomString();
190 $value = wfRandomString();
191 $cKey1 = wfRandomString();
192 $cKey2 = wfRandomString();
197 $func = function ( $old, &$ttl, &$opts, $asOf )
198 use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) {
202 $ttl = 20; // override with another value
206 $mockWallClock = 1549343530.2053;
207 $priorTime = $mockWallClock; // reference time
208 $cache->setMockTime( $mockWallClock );
211 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
212 $this->assertEquals( $value, $v, "Value returned" );
213 $this->assertEquals( 1, $wasSet, "Value regenerated" );
214 $this->assertFalse( $priorValue, "No prior value" );
215 $this->assertNull( $priorAsOf, "No prior value" );
218 $cache->get( $key, $curTTL );
219 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
220 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
223 $v = $cache->getWithSetCallback(
224 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
225 $this->assertEquals( $value, $v, "Value returned" );
226 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
231 $v = $cache->getWithSetCallback(
232 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
234 $this->assertEquals( $value, $v, "Value returned" );
235 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
236 $this->assertEquals( $value, $priorValue, "Has prior value" );
237 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
238 $t1 = $cache->getCheckKeyTime( $cKey1 );
239 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
240 $t2 = $cache->getCheckKeyTime( $cKey2 );
241 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
243 $mockWallClock +
= 0.01;
244 $priorTime = $mockWallClock; // reference time
246 $v = $cache->getWithSetCallback(
247 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
249 $this->assertEquals( $value, $v, "Value returned" );
250 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
251 $t1 = $cache->getCheckKeyTime( $cKey1 );
252 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
253 $t2 = $cache->getCheckKeyTime( $cKey2 );
254 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
257 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
259 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
261 $this->assertEquals( $value, $v, "Value returned" );
263 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
266 $key = wfRandomString();
267 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
268 $this->assertEquals( $value, $v, "Value returned" );
269 $cache->delete( $key );
270 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
271 $this->assertEquals( $value, $v, "Value still returned after deleted" );
272 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
274 $oldValReceived = -1;
275 $oldAsOfReceived = -1;
276 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
277 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
279 $oldValReceived = $oldVal;
280 $oldAsOfReceived = $oldAsOf;
282 return 'xxx' . $wasSet;
285 $mockWallClock = 1549343530.2053;
286 $priorTime = $mockWallClock; // reference time
289 $key = wfRandomString();
290 $v = $cache->getWithSetCallback(
291 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
292 $this->assertEquals( 'xxx1', $v, "Value returned" );
293 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
294 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
296 $mockWallClock +
= 40;
297 $v = $cache->getWithSetCallback(
298 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
299 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
300 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
301 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
302 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
304 $mockWallClock +
= 260;
305 $v = $cache->getWithSetCallback(
306 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
307 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
308 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
309 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
310 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
312 $mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL
- 1 );
314 $key = wfRandomString();
315 $checkKey = $cache->makeKey( 'template', 'X' );
316 $cache->touchCheckKey( $checkKey ); // init check key
317 $mockWallClock = $priorTime;
318 $v = $cache->getWithSetCallback(
320 $cache::TTL_INDEFINITE
,
322 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
324 $this->assertEquals( 'xxx1', $v, "Value returned" );
325 $this->assertEquals( 1, $wasSet, "Value computed" );
326 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
327 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
329 $mockWallClock +
= $cache::TTL_HOUR
; // some time passes
330 $v = $cache->getWithSetCallback(
332 $cache::TTL_INDEFINITE
,
334 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
336 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
337 $this->assertEquals( 1, $wasSet, "Cached value returned" );
339 $cache->touchCheckKey( $checkKey ); // make key stale
340 $mockWallClock +
= 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
342 $v = $cache->getWithSetCallback(
344 $cache::TTL_INDEFINITE
,
346 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
348 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
349 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
351 // Chance of refresh increase to unity as staleness approaches graceTTL
352 $mockWallClock +
= $cache::TTL_WEEK
; // 8 days of being stale
353 $v = $cache->getWithSetCallback(
355 $cache::TTL_INDEFINITE
,
357 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
359 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
360 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
361 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
362 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
366 * @dataProvider getWithSetCallback_provider
367 * @covers WANObjectCache::getWithSetCallback()
368 * @covers WANObjectCache::doGetWithSetCallback()
369 * @param array $extOpts
370 * @param bool $versioned
372 function testGetWithSetcallback_touched( array $extOpts, $versioned ) {
373 $cache = $this->cache
;
375 $mockWallClock = 1549343530.2053;
376 $cache->setMockTime( $mockWallClock );
378 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
382 return 'xxx' . $wasSet;
385 $key = wfRandomString();
388 $touchedCallback = function () use ( &$touched ) {
391 $v = $cache->getWithSetCallback(
393 $cache::TTL_INDEFINITE
,
395 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
397 $mockWallClock +
= 60;
398 $v = $cache->getWithSetCallback(
400 $cache::TTL_INDEFINITE
,
402 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
404 $this->assertEquals( 'xxx1', $v, "Value was computed once" );
405 $this->assertEquals( 1, $wasSet, "Value was computed once" );
407 $touched = $mockWallClock - 10;
408 $v = $cache->getWithSetCallback(
410 $cache::TTL_INDEFINITE
,
412 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
414 $v = $cache->getWithSetCallback(
416 $cache::TTL_INDEFINITE
,
418 [ 'touchedCallback' => $touchedCallback ] +
$extOpts
420 $this->assertEquals( 'xxx2', $v, "Value was recomputed once" );
421 $this->assertEquals( 2, $wasSet, "Value was recomputed once" );
424 public static function getWithSetCallback_provider() {
427 [ [ 'version' => 1 ], true ]
431 public function testPreemtiveRefresh() {
434 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
440 $cache = new NearExpiringWANObjectCache( [
441 'cache' => new HashBagOStuff()
445 $key = wfRandomString();
446 $opts = [ 'lowTTL' => 30 ];
447 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
448 $this->assertEquals( $value, $v, "Value returned" );
449 $this->assertEquals( 1, $wasSet, "Value calculated" );
450 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
451 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
454 $key = wfRandomString();
455 $opts = [ 'lowTTL' => 1 ];
456 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
457 $this->assertEquals( $value, $v, "Value returned" );
458 $this->assertEquals( 1, $wasSet, "Value calculated" );
459 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
460 $this->assertEquals( 1, $wasSet, "Value cached" );
463 $asyncHandler = function ( $callback ) use ( &$asycList ) {
464 $asycList[] = $callback;
466 $cache = new NearExpiringWANObjectCache( [
467 'cache' => new HashBagOStuff(),
468 'asyncHandler' => $asyncHandler
471 $mockWallClock = 1549343530.2053;
472 $priorTime = $mockWallClock; // reference time
473 $cache->setMockTime( $mockWallClock );
476 $key = wfRandomString();
477 $opts = [ 'lowTTL' => 100 ];
478 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
479 $this->assertEquals( $value, $v, "Value returned" );
480 $this->assertEquals( 1, $wasSet, "Value calculated" );
481 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
482 $this->assertEquals( 1, $wasSet, "Cached value used" );
483 $this->assertEquals( $v, $value, "Value cached" );
485 $mockWallClock +
= 250;
486 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
487 $this->assertEquals( $value, $v, "Value returned" );
488 $this->assertEquals( 1, $wasSet, "Stale value used" );
489 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
490 $value = 'NewCatsInTown'; // change callback return value
491 $asycList[0](); // run the refresh callback
493 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
494 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
495 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
496 $this->assertEquals( $value, $v, "New value stored" );
498 $cache = new PopularityRefreshingWANObjectCache( [
499 'cache' => new HashBagOStuff()
502 $mockWallClock = $priorTime;
503 $cache->setMockTime( $mockWallClock );
506 $key = wfRandomString();
507 $opts = [ 'hotTTR' => 900 ];
508 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
509 $this->assertEquals( $value, $v, "Value returned" );
510 $this->assertEquals( 1, $wasSet, "Value calculated" );
512 $mockWallClock +
= 30;
514 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
515 $this->assertEquals( 1, $wasSet, "Value cached" );
517 $mockWallClock = $priorTime;
519 $key = wfRandomString();
520 $opts = [ 'hotTTR' => 10 ];
521 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
522 $this->assertEquals( $value, $v, "Value returned" );
523 $this->assertEquals( 1, $wasSet, "Value calculated" );
525 $mockWallClock +
= 30;
527 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
528 $this->assertEquals( $value, $v, "Value returned" );
529 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
533 * @covers WANObjectCache::getWithSetCallback()
534 * @covers WANObjectCache::doGetWithSetCallback()
536 public function testGetWithSetCallback_invalidCallback() {
537 $this->setExpectedException( InvalidArgumentException
::class );
538 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
542 * @dataProvider getMultiWithSetCallback_provider
543 * @covers WANObjectCache::getMultiWithSetCallback
544 * @covers WANObjectCache::makeMultiKeys
545 * @covers WANObjectCache::getMulti
546 * @param array $extOpts
547 * @param bool $versioned
549 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
550 $cache = $this->cache
;
552 $keyA = wfRandomString();
553 $keyB = wfRandomString();
554 $keyC = wfRandomString();
555 $cKey1 = wfRandomString();
556 $cKey2 = wfRandomString();
561 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
562 &$wasSet, &$priorValue, &$priorAsOf
567 $ttl = 20; // override with another value
571 $mockWallClock = 1549343530.2053;
572 $priorTime = $mockWallClock; // reference time
573 $cache->setMockTime( $mockWallClock );
576 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
578 $v = $cache->getMultiWithSetCallback(
579 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
580 $this->assertEquals( $value, $v[$keyA], "Value returned" );
581 $this->assertEquals( 1, $wasSet, "Value regenerated" );
582 $this->assertFalse( $priorValue, "No prior value" );
583 $this->assertNull( $priorAsOf, "No prior value" );
586 $cache->get( $keyA, $curTTL );
587 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
588 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
592 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
593 $v = $cache->getMultiWithSetCallback(
594 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
595 $this->assertEquals( $value, $v[$keyB], "Value returned" );
596 $this->assertEquals( 1, $wasSet, "Value regenerated" );
597 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
598 $v = $cache->getMultiWithSetCallback(
599 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
600 $this->assertEquals( $value, $v[$keyB], "Value returned" );
601 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
602 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
607 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
608 $v = $cache->getMultiWithSetCallback(
609 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
611 $this->assertEquals( $value, $v[$keyB], "Value returned" );
612 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
613 $this->assertEquals( $value, $priorValue, "Has prior value" );
614 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
615 $t1 = $cache->getCheckKeyTime( $cKey1 );
616 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
617 $t2 = $cache->getCheckKeyTime( $cKey2 );
618 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
620 $mockWallClock +
= 0.01;
621 $priorTime = $mockWallClock;
624 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
625 $v = $cache->getMultiWithSetCallback(
626 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
628 $this->assertEquals( $value, $v[$keyC], "Value returned" );
629 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
630 $t1 = $cache->getCheckKeyTime( $cKey1 );
631 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
632 $t2 = $cache->getCheckKeyTime( $cKey2 );
633 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
636 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
638 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
640 $this->assertEquals( $value, $v, "Value returned" );
642 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
645 $key = wfRandomString();
646 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
647 $v = $cache->getMultiWithSetCallback(
648 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
649 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
650 $cache->delete( $key );
651 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
652 $v = $cache->getMultiWithSetCallback(
653 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
654 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
655 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
658 $ids = [ 1, 2, 3, 4, 5, 6 ];
659 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
660 return $wanCache->makeKey( 'test', $id );
662 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
663 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
668 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
671 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
672 array_values( $values ),
673 "Correct values in correct order"
676 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
677 array_keys( $values ),
678 "Correct keys in correct order"
680 $this->assertEquals( count( $ids ), $calls );
682 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
683 $this->assertEquals( count( $ids ), $calls, "Values cached" );
685 // Mock the BagOStuff to assure only one getMulti() call given process caching
686 $localBag = $this->getMockBuilder( HashBagOStuff
::class )
687 ->setMethods( [ 'getMulti' ] )->getMock();
688 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
689 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
690 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
692 $wanCache = new WANObjectCache( [ 'cache' => $localBag ] );
694 // Warm the process cache
695 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
697 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
698 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
700 // Use the process cache
702 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
703 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
707 public static function getMultiWithSetCallback_provider() {
710 [ [ 'version' => 1 ], true ]
715 * @dataProvider getMultiWithUnionSetCallback_provider
716 * @covers WANObjectCache::getMultiWithUnionSetCallback()
717 * @covers WANObjectCache::makeMultiKeys()
718 * @param array $extOpts
719 * @param bool $versioned
721 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
722 $cache = $this->cache
;
724 $keyA = wfRandomString();
725 $keyB = wfRandomString();
726 $keyC = wfRandomString();
727 $cKey1 = wfRandomString();
728 $cKey2 = wfRandomString();
731 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
732 &$wasSet, &$priorValue, &$priorAsOf
735 foreach ( $ids as $id ) {
737 $newValues[$id] = "@$id$";
738 $ttls[$id] = 20; // override with another value
744 $mockWallClock = 1549343530.2053;
745 $priorTime = $mockWallClock; // reference time
746 $cache->setMockTime( $mockWallClock );
749 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
751 $v = $cache->getMultiWithUnionSetCallback(
752 $keyedIds, 30, $genFunc, $extOpts );
753 $this->assertEquals( $value, $v[$keyA], "Value returned" );
754 $this->assertEquals( 1, $wasSet, "Value regenerated" );
757 $cache->get( $keyA, $curTTL );
758 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
759 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
763 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
764 $v = $cache->getMultiWithUnionSetCallback(
765 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
766 $this->assertEquals( $value, $v[$keyB], "Value returned" );
767 $this->assertEquals( 1, $wasSet, "Value regenerated" );
768 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
769 $v = $cache->getMultiWithUnionSetCallback(
770 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
771 $this->assertEquals( $value, $v[$keyB], "Value returned" );
772 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
773 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
778 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
779 $v = $cache->getMultiWithUnionSetCallback(
780 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
782 $this->assertEquals( $value, $v[$keyB], "Value returned" );
783 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
784 $t1 = $cache->getCheckKeyTime( $cKey1 );
785 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
786 $t2 = $cache->getCheckKeyTime( $cKey2 );
787 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
789 $mockWallClock +
= 0.01;
790 $priorTime = $mockWallClock;
793 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
794 $v = $cache->getMultiWithUnionSetCallback(
795 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
797 $this->assertEquals( $value, $v[$keyC], "Value returned" );
798 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
799 $t1 = $cache->getCheckKeyTime( $cKey1 );
800 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
801 $t2 = $cache->getCheckKeyTime( $cKey2 );
802 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
805 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
807 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
809 $this->assertEquals( $value, $v, "Value returned" );
811 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
814 $key = wfRandomString();
815 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
816 $v = $cache->getMultiWithUnionSetCallback(
817 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
818 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
819 $cache->delete( $key );
820 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
821 $v = $cache->getMultiWithUnionSetCallback(
822 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
823 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
824 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
827 $ids = [ 1, 2, 3, 4, 5, 6 ];
828 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
829 return $wanCache->makeKey( 'test', $id );
831 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
832 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
834 foreach ( $ids as $id ) {
836 $newValues[$id] = "val-{$id}";
841 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
844 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
845 array_values( $values ),
846 "Correct values in correct order"
849 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
850 array_keys( $values ),
851 "Correct keys in correct order"
853 $this->assertEquals( count( $ids ), $calls );
855 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
856 $this->assertEquals( count( $ids ), $calls, "Values cached" );
859 public static function getMultiWithUnionSetCallback_provider() {
862 [ [ 'version' => 1 ], true ]
867 * @covers WANObjectCache::getWithSetCallback()
868 * @covers WANObjectCache::doGetWithSetCallback()
870 public function testLockTSE() {
871 $cache = $this->cache
;
872 $key = wfRandomString();
873 $value = wfRandomString();
876 $func = function () use ( &$calls, $value, $cache, $key ) {
881 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
882 $this->assertEquals( $value, $ret );
883 $this->assertEquals( 1, $calls, 'Value was populated' );
885 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
886 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
888 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
889 $ret = $cache->getWithSetCallback( $key, 30, $func,
890 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
891 $this->assertEquals( $value, $ret, 'Old value used' );
892 $this->assertEquals( 1, $calls, 'Callback was not used' );
894 $cache->delete( $key );
895 $ret = $cache->getWithSetCallback( $key, 30, $func,
896 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
897 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
898 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
900 $ret = $cache->getWithSetCallback( $key, 30, $func,
901 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
902 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
903 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
907 * @covers WANObjectCache::getWithSetCallback()
908 * @covers WANObjectCache::doGetWithSetCallback()
909 * @covers WANObjectCache::set()
911 public function testLockTSESlow() {
912 $cache = $this->cache
;
913 $key = wfRandomString();
914 $key2 = wfRandomString();
915 $value = wfRandomString();
917 $mockWallClock = 1549343530.2053;
918 $priorTime = $mockWallClock;
919 $cache->setMockTime( $mockWallClock );
922 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
924 $setOpts['since'] = $priorTime - 10;
928 // Value should be given a low logical TTL due to snapshot lag
930 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
931 $this->assertEquals( $value, $ret );
932 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
933 $this->assertEquals( 1, $curTTL, 'Value has reduced logical TTL', 0.01 );
934 $this->assertEquals( 1, $calls, 'Value was generated' );
938 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
939 $this->assertEquals( $value, $ret );
940 $this->assertEquals( 2, $calls, 'Callback used (mutex acquired)' );
942 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
943 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
945 $ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] );
946 $this->assertEquals( $value, $ret );
947 $this->assertEquals( 3, $calls, 'Callback was not used (mutex not acquired)' );
950 $func2 = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $priorTime ) {
952 $setOpts['lag'] = 15;
956 // Value should be given a low logical TTL due to replication lag
958 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
959 $this->assertEquals( $value, $ret );
960 $this->assertEquals( $value, $cache->get( $key2, $curTTL ), 'Value was populated' );
961 $this->assertEquals( 30, $curTTL, 'Value has reduced logical TTL', 0.01 );
962 $this->assertEquals( 1, $calls, 'Value was generated' );
964 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
965 $this->assertEquals( $value, $ret );
966 $this->assertEquals( 1, $calls, 'Callback was used (not expired)' );
968 $mockWallClock +
= 31;
970 $ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] );
971 $this->assertEquals( $value, $ret );
972 $this->assertEquals( 2, $calls, 'Callback was used (mutex acquired)' );
976 * @covers WANObjectCache::getWithSetCallback()
977 * @covers WANObjectCache::doGetWithSetCallback()
979 public function testBusyValue() {
980 $cache = $this->cache
;
981 $key = wfRandomString();
982 $value = wfRandomString();
983 $busyValue = wfRandomString();
986 $func = function () use ( &$calls, $value, $cache, $key ) {
991 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
992 $this->assertEquals( $value, $ret );
993 $this->assertEquals( 1, $calls, 'Value was populated' );
995 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
996 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
998 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
999 $ret = $cache->getWithSetCallback( $key, 30, $func,
1000 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1001 $this->assertEquals( $value, $ret, 'Callback used' );
1002 $this->assertEquals( 2, $calls, 'Callback used' );
1004 $ret = $cache->getWithSetCallback( $key, 30, $func,
1005 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1006 $this->assertEquals( $value, $ret, 'Old value used' );
1007 $this->assertEquals( 2, $calls, 'Callback was not used' );
1009 $cache->delete( $key ); // no value at all anymore and still locked
1010 $ret = $cache->getWithSetCallback( $key, 30, $func,
1011 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1012 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
1013 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
1015 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1016 $ret = $cache->getWithSetCallback( $key, 30, $func,
1017 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1018 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
1019 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
1021 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1022 $ret = $cache->getWithSetCallback( $key, 30, $func,
1023 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
1024 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
1025 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
1029 * @covers WANObjectCache::getMulti()
1031 public function testGetMulti() {
1032 $cache = $this->cache
;
1034 $value1 = [ 'this' => 'is', 'a' => 'test' ];
1035 $value2 = [ 'this' => 'is', 'another' => 'test' ];
1037 $key1 = wfRandomString();
1038 $key2 = wfRandomString();
1039 $key3 = wfRandomString();
1041 $mockWallClock = 1549343530.2053;
1042 $priorTime = $mockWallClock; // reference time
1043 $cache->setMockTime( $mockWallClock );
1045 $cache->set( $key1, $value1, 5 );
1046 $cache->set( $key2, $value2, 10 );
1049 $this->assertEquals(
1050 [ $key1 => $value1, $key2 => $value2 ],
1051 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
1052 'Result array populated'
1055 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
1056 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
1057 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
1059 $cKey1 = wfRandomString();
1060 $cKey2 = wfRandomString();
1062 $mockWallClock +
= 1;
1065 $this->assertEquals(
1066 [ $key1 => $value1, $key2 => $value2 ],
1067 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1068 "Result array populated even with new check keys"
1070 $t1 = $cache->getCheckKeyTime( $cKey1 );
1071 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
1072 $t2 = $cache->getCheckKeyTime( $cKey2 );
1073 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
1074 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
1075 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
1076 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
1078 $mockWallClock +
= 1;
1081 $this->assertEquals(
1082 [ $key1 => $value1, $key2 => $value2 ],
1083 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
1084 "Result array still populated even with new check keys"
1086 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1087 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1088 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1092 * @covers WANObjectCache::getMulti()
1093 * @covers WANObjectCache::processCheckKeys()
1095 public function testGetMultiCheckKeys() {
1096 $cache = $this->cache
;
1098 $checkAll = wfRandomString();
1099 $check1 = wfRandomString();
1100 $check2 = wfRandomString();
1101 $check3 = wfRandomString();
1102 $value1 = wfRandomString();
1103 $value2 = wfRandomString();
1105 $mockWallClock = 1549343530.2053;
1106 $cache->setMockTime( $mockWallClock );
1108 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1109 // several seconds during the test to assert the behaviour.
1110 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1111 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1114 $mockWallClock +
= 0.100;
1116 $cache->set( 'key1', $value1, 10 );
1117 $cache->set( 'key2', $value2, 10 );
1120 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1126 $this->assertEquals(
1127 [ 'key1' => $value1, 'key2' => $value2 ],
1131 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1132 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1133 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1134 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1136 $mockWallClock +
= 0.100;
1137 $cache->touchCheckKey( $check1 );
1140 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1146 $this->assertEquals(
1147 [ 'key1' => $value1, 'key2' => $value2 ],
1149 'key1 expired by check1, but value still provided'
1151 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1152 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1154 $cache->touchCheckKey( $checkAll );
1157 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1163 $this->assertEquals(
1164 [ 'key1' => $value1, 'key2' => $value2 ],
1166 'All keys expired by checkAll, but value still provided'
1168 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1169 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1173 * @covers WANObjectCache::get()
1174 * @covers WANObjectCache::processCheckKeys()
1176 public function testCheckKeyInitHoldoff() {
1177 $cache = $this->cache
;
1179 for ( $i = 0; $i < 500; ++
$i ) {
1180 $key = wfRandomString();
1181 $checkKey = wfRandomString();
1183 $cache->get( $key, $curTTL, [ $checkKey ] );
1184 $cache->set( $key, 'val', 10 );
1186 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1188 $this->assertEquals( 'val', $v );
1189 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1192 for ( $i = 0; $i < 500; ++
$i ) {
1193 $key = wfRandomString();
1194 $checkKey = wfRandomString();
1196 $cache->set( $key, 'val', 10 );
1198 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1200 $this->assertEquals( 'val', $v );
1201 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1206 * @covers WANObjectCache::delete
1207 * @covers WANObjectCache::relayDelete
1208 * @covers WANObjectCache::relayPurge
1210 public function testDelete() {
1211 $key = wfRandomString();
1212 $value = wfRandomString();
1213 $this->cache
->set( $key, $value );
1216 $v = $this->cache
->get( $key, $curTTL );
1217 $this->assertEquals( $value, $v, "Key was created with value" );
1218 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1220 $this->cache
->delete( $key );
1223 $v = $this->cache
->get( $key, $curTTL );
1224 $this->assertFalse( $v, "Deleted key has false value" );
1225 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1227 $this->cache
->set( $key, $value . 'more' );
1228 $v = $this->cache
->get( $key, $curTTL );
1229 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1230 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1232 $this->cache
->set( $key, $value );
1233 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1236 $v = $this->cache
->get( $key, $curTTL );
1237 $this->assertFalse( $v, "Deleted key has false value" );
1238 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1240 $this->cache
->set( $key, $value );
1241 $v = $this->cache
->get( $key, $curTTL );
1242 $this->assertEquals( $value, $v, "Key was created with value" );
1243 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1247 * @dataProvider getWithSetCallback_versions_provider
1248 * @covers WANObjectCache::getWithSetCallback()
1249 * @covers WANObjectCache::doGetWithSetCallback()
1250 * @param array $extOpts
1251 * @param bool $versioned
1253 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1254 $cache = $this->cache
;
1256 $key = wfRandomString();
1257 $valueV1 = wfRandomString();
1258 $valueV2 = [ wfRandomString() ];
1261 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1267 $priorValue = false;
1269 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1270 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1271 $priorValue = $oldValue;
1272 $priorAsOf = $oldAsOf;
1275 return $valueV2; // new array format
1278 // Set the main key (version N if versioned)
1280 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1281 $this->assertEquals( $valueV1, $v, "Value returned" );
1282 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1283 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1284 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1285 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1288 // Set the key for version N+1 format
1289 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1291 // Start versioning now with the unversioned key still there
1292 $verOpts = [ 'version' => 1 ];
1295 // Value goes to secondary key since V1 already used $key
1297 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1298 $this->assertEquals( $valueV2, $v, "Value returned" );
1299 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1300 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1301 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1304 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1305 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1306 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1308 // Clear out the older or unversioned key
1309 $cache->delete( $key, 0 );
1311 // Set the key for next/first versioned format
1313 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1314 $this->assertEquals( $valueV2, $v, "Value returned" );
1315 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1317 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1318 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1319 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1322 public static function getWithSetCallback_versions_provider() {
1325 [ [ 'version' => 1 ], true ]
1330 * @covers WANObjectCache::useInterimHoldOffCaching
1331 * @covers WANObjectCache::getInterimValue
1333 public function testInterimHoldOffCaching() {
1334 $cache = $this->cache
;
1336 $value = 'CRL-40-940';
1338 $func = function () use ( &$wasCalled, $value ) {
1344 $cache->useInterimHoldOffCaching( true );
1346 $key = wfRandomString( 32 );
1347 $v = $cache->getWithSetCallback( $key, 60, $func );
1348 $v = $cache->getWithSetCallback( $key, 60, $func );
1349 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1350 $cache->delete( $key );
1351 $v = $cache->getWithSetCallback( $key, 60, $func );
1352 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1353 $v = $cache->getWithSetCallback( $key, 60, $func );
1354 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1355 // Lock up the mutex so interim cache is used
1356 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1357 $v = $cache->getWithSetCallback( $key, 60, $func );
1358 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1359 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1361 $cache->useInterimHoldOffCaching( false );
1364 $key = wfRandomString( 32 );
1365 $v = $cache->getWithSetCallback( $key, 60, $func );
1366 $v = $cache->getWithSetCallback( $key, 60, $func );
1367 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1368 $cache->delete( $key );
1369 $v = $cache->getWithSetCallback( $key, 60, $func );
1370 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1371 $v = $cache->getWithSetCallback( $key, 60, $func );
1372 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1373 $v = $cache->getWithSetCallback( $key, 60, $func );
1374 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1375 // Lock up the mutex so interim cache is used
1376 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1377 $v = $cache->getWithSetCallback( $key, 60, $func );
1378 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1382 * @covers WANObjectCache::touchCheckKey
1383 * @covers WANObjectCache::resetCheckKey
1384 * @covers WANObjectCache::getCheckKeyTime
1385 * @covers WANObjectCache::getMultiCheckKeyTime
1386 * @covers WANObjectCache::makePurgeValue
1387 * @covers WANObjectCache::parsePurgeValue
1389 public function testTouchKeys() {
1390 $cache = $this->cache
;
1391 $key = wfRandomString();
1393 $mockWallClock = 1549343530.2053;
1394 $priorTime = $mockWallClock; // reference time
1395 $cache->setMockTime( $mockWallClock );
1397 $mockWallClock +
= 0.100;
1398 $t0 = $cache->getCheckKeyTime( $key );
1399 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1401 $priorTime = $mockWallClock;
1402 $mockWallClock +
= 0.100;
1403 $cache->touchCheckKey( $key );
1404 $t1 = $cache->getCheckKeyTime( $key );
1405 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1407 $t2 = $cache->getCheckKeyTime( $key );
1408 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1410 $mockWallClock +
= 0.100;
1411 $cache->touchCheckKey( $key );
1412 $t3 = $cache->getCheckKeyTime( $key );
1413 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1415 $t4 = $cache->getCheckKeyTime( $key );
1416 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1418 $mockWallClock +
= 0.100;
1419 $cache->resetCheckKey( $key );
1420 $t5 = $cache->getCheckKeyTime( $key );
1421 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1423 $t6 = $cache->getCheckKeyTime( $key );
1424 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1428 * @covers WANObjectCache::getMulti()
1430 public function testGetWithSeveralCheckKeys() {
1431 $key = wfRandomString();
1432 $tKey1 = wfRandomString();
1433 $tKey2 = wfRandomString();
1436 $mockWallClock = 1549343530.2053;
1437 $priorTime = $mockWallClock; // reference time
1438 $this->cache
->setMockTime( $mockWallClock );
1440 // Two check keys are newer (given hold-off) than $key, another is older
1441 $this->internalCache
->set(
1442 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1443 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 3 )
1445 $this->internalCache
->set(
1446 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1447 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 5 )
1449 $this->internalCache
->set(
1450 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1451 WANObjectCache
::PURGE_VAL_PREFIX
. ( $priorTime - 30 )
1453 $this->cache
->set( $key, $value, 30 );
1456 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1457 $this->assertEquals( $value, $v, "Value matches" );
1458 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1459 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1463 * @covers WANObjectCache::reap()
1464 * @covers WANObjectCache::reapCheckKey()
1466 public function testReap() {
1467 $vKey1 = wfRandomString();
1468 $vKey2 = wfRandomString();
1469 $tKey1 = wfRandomString();
1470 $tKey2 = wfRandomString();
1473 $knownPurge = time() - 60;
1474 $goodTime = microtime( true ) - 5;
1475 $badTime = microtime( true ) - 300;
1477 $this->internalCache
->set(
1478 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1480 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1481 WANObjectCache
::FLD_VALUE
=> $value,
1482 WANObjectCache
::FLD_TTL
=> 3600,
1483 WANObjectCache
::FLD_TIME
=> $goodTime
1486 $this->internalCache
->set(
1487 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1489 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1490 WANObjectCache
::FLD_VALUE
=> $value,
1491 WANObjectCache
::FLD_TTL
=> 3600,
1492 WANObjectCache
::FLD_TIME
=> $badTime
1495 $this->internalCache
->set(
1496 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1497 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1499 $this->internalCache
->set(
1500 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1501 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1504 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1505 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1506 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1507 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1509 $this->assertFalse( $bad1 );
1510 $this->assertTrue( $bad2 );
1512 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1513 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1514 $this->assertFalse( $tBad1 );
1515 $this->assertTrue( $tBad2 );
1519 * @covers WANObjectCache::reap()
1521 public function testReap_fail() {
1522 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1523 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1524 $backend->expects( $this->once() )->method( 'get' )
1526 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1527 WANObjectCache
::FLD_VALUE
=> 'value',
1528 WANObjectCache
::FLD_TTL
=> 3600,
1529 WANObjectCache
::FLD_TIME
=> 300,
1531 $backend->expects( $this->once() )->method( 'changeTTL' )
1532 ->willReturn( false );
1534 $wanCache = new WANObjectCache( [
1539 $ret = $wanCache->reap( 'key', 360, $isStale );
1540 $this->assertTrue( $isStale, 'value was stale' );
1541 $this->assertFalse( $ret, 'changeTTL failed' );
1545 * @covers WANObjectCache::set()
1547 public function testSetWithLag() {
1550 $key = wfRandomString();
1551 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1552 $this->cache
->set( $key, $value, 30, $opts );
1553 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1555 $key = wfRandomString();
1556 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1557 $this->cache
->set( $key, $value, 30, $opts );
1558 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1560 $key = wfRandomString();
1561 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1562 $this->cache
->set( $key, $value, 30, $opts );
1563 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1567 * @covers WANObjectCache::set()
1569 public function testWritePending() {
1572 $key = wfRandomString();
1573 $opts = [ 'pending' => true ];
1574 $this->cache
->set( $key, $value, 30, $opts );
1575 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1578 public function testMcRouterSupport() {
1579 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1580 ->setMethods( [ 'set', 'delete' ] )->getMock();
1581 $localBag->expects( $this->never() )->method( 'set' );
1582 $localBag->expects( $this->never() )->method( 'delete' );
1583 $wanCache = new WANObjectCache( [
1584 'cache' => $localBag,
1585 'mcrouterAware' => true,
1586 'region' => 'pmtpa',
1587 'cluster' => 'mw-wan'
1589 $valFunc = function () {
1593 // None of these should use broadcasting commands (e.g. SET, DELETE)
1594 $wanCache->get( 'x' );
1595 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1596 $wanCache->getMulti( [ 'x', 'y' ] );
1597 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1598 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1599 $wanCache->getCheckKeyTime( 'zzz' );
1600 $wanCache->reap( 'x', time() - 300 );
1601 $wanCache->reap( 'zzz', time() - 300 );
1604 public function testMcRouterSupportBroadcastDelete() {
1605 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1606 ->setMethods( [ 'set' ] )->getMock();
1607 $wanCache = new WANObjectCache( [
1608 'cache' => $localBag,
1609 'mcrouterAware' => true,
1610 'region' => 'pmtpa',
1611 'cluster' => 'mw-wan'
1614 $localBag->expects( $this->once() )->method( 'set' )
1615 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX
. "test" );
1617 $wanCache->delete( 'test' );
1620 public function testMcRouterSupportBroadcastTouchCK() {
1621 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1622 ->setMethods( [ 'set' ] )->getMock();
1623 $wanCache = new WANObjectCache( [
1624 'cache' => $localBag,
1625 'mcrouterAware' => true,
1626 'region' => 'pmtpa',
1627 'cluster' => 'mw-wan'
1630 $localBag->expects( $this->once() )->method( 'set' )
1631 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1633 $wanCache->touchCheckKey( 'test' );
1636 public function testMcRouterSupportBroadcastResetCK() {
1637 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1638 ->setMethods( [ 'delete' ] )->getMock();
1639 $wanCache = new WANObjectCache( [
1640 'cache' => $localBag,
1641 'mcrouterAware' => true,
1642 'region' => 'pmtpa',
1643 'cluster' => 'mw-wan'
1646 $localBag->expects( $this->once() )->method( 'delete' )
1647 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1649 $wanCache->resetCheckKey( 'test' );
1652 public function testEpoch() {
1653 $bag = new HashBagOStuff();
1654 $cache = new WANObjectCache( [ 'cache' => $bag ] );
1655 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1657 $now = microtime( true );
1658 $cache->setMockTime( $now );
1660 $cache->set( $key, 'Do what thou Wilt' );
1661 $cache->touchCheckKey( $key );
1665 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1666 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1668 $cache = new WANObjectCache( [
1670 'epoch' => $now - 3600
1672 $cache->setMockTime( $now );
1674 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1675 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1678 $cache = new WANObjectCache( [
1680 'epoch' => $now +
3600
1682 $cache->setMockTime( $now );
1684 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1685 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1689 * @dataProvider provideAdaptiveTTL
1690 * @covers WANObjectCache::adaptiveTTL()
1691 * @param float|int $ago
1692 * @param int $maxTTL
1693 * @param int $minTTL
1694 * @param float $factor
1695 * @param int $adaptiveTTL
1697 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1698 $mtime = $ago ?
time() - $ago : $ago;
1700 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1702 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1703 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1705 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1707 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1708 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1711 public static function provideAdaptiveTTL() {
1713 [ 3600, 900, 30, 0.2, 720 ],
1714 [ 3600, 500, 30, 0.2, 500 ],
1715 [ 3600, 86400, 800, 0.2, 800 ],
1716 [ false, 86400, 800, 0.2, 800 ],
1717 [ null, 86400, 800, 0.2, 800 ]
1722 * @covers WANObjectCache::__construct
1723 * @covers WANObjectCache::newEmpty
1725 public function testNewEmpty() {
1726 $this->assertInstanceOf(
1727 WANObjectCache
::class,
1728 WANObjectCache
::newEmpty()
1733 * @covers WANObjectCache::setLogger
1735 public function testSetLogger() {
1736 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1740 * @covers WANObjectCache::getQoS
1742 public function testGetQoS() {
1743 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1744 ->setMethods( [ 'getQoS' ] )->getMock();
1745 $backend->expects( $this->once() )->method( 'getQoS' )
1746 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1747 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1750 $wanCache::QOS_UNKNOWN
,
1751 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1756 * @covers WANObjectCache::makeKey
1758 public function testMakeKey() {
1759 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1760 ->setMethods( [ 'makeKey' ] )->getMock();
1761 $backend->expects( $this->once() )->method( 'makeKey' )
1762 ->willReturn( 'special' );
1764 $wanCache = new WANObjectCache( [
1768 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1772 * @covers WANObjectCache::makeGlobalKey
1774 public function testMakeGlobalKey() {
1775 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1776 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1777 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1778 ->willReturn( 'special' );
1780 $wanCache = new WANObjectCache( [
1784 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1787 public static function statsKeyProvider() {
1789 [ 'domain:page:5', 'page' ],
1790 [ 'domain:main-key', 'main-key' ],
1791 [ 'domain:page:history', 'page' ],
1792 [ 'missingdomainkey', 'missingdomainkey' ]
1797 * @dataProvider statsKeyProvider
1798 * @covers WANObjectCache::determineKeyClass
1800 public function testStatsKeyClass( $key, $class ) {
1801 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1802 'cache' => new HashBagOStuff
1805 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1809 class NearExpiringWANObjectCache
extends WANObjectCache
{
1810 const CLOCK_SKEW
= 1;
1812 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1813 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1817 class PopularityRefreshingWANObjectCache
extends WANObjectCache
{
1818 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1819 return ( ( $now - $asOf ) > $timeTillRefresh );