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(),
34 'pool' => 'testcache-hash',
35 'relayer' => new EventRelayerNull( [] )
38 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
39 /** @noinspection PhpUndefinedFieldInspection */
40 $this->internalCache
= $wanCache->cache
;
44 * @dataProvider provideSetAndGet
45 * @covers WANObjectCache::set()
46 * @covers WANObjectCache::get()
47 * @covers WANObjectCache::makeKey()
51 public function testSetAndGet( $value, $ttl ) {
54 $key = $this->cache
->makeKey( 'x', wfRandomString() );
56 $this->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 );
61 $this->cache
->set( $key, $value, $ttl );
63 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
64 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
65 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
67 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
68 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
70 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
71 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
74 public static function provideSetAndGet() {
81 [ (object)[ 'meow' ], 3 ],
89 * @covers WANObjectCache::get()
90 * @covers WANObjectCache::makeGlobalKey()
92 public function testGetNotExists() {
93 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
95 $value = $this->cache
->get( $key, $curTTL );
97 $this->assertFalse( $value, "Non-existing key has false value" );
98 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
102 * @covers WANObjectCache::set()
104 public function testSetOver() {
105 $key = wfRandomString();
106 for ( $i = 0; $i < 3; ++
$i ) {
107 $value = wfRandomString();
108 $this->cache
->set( $key, $value, 3 );
110 $this->assertEquals( $this->cache
->get( $key ), $value );
115 * @covers WANObjectCache::set()
117 public function testStaleSet() {
118 $key = wfRandomString();
119 $value = wfRandomString();
120 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
122 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
125 public function testProcessCache() {
127 $callback = function () use ( &$hit ) {
131 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
132 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
134 foreach ( $keys as $i => $key ) {
135 $this->cache
->getWithSetCallback(
136 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
138 $this->assertEquals( 3, $hit );
140 foreach ( $keys as $i => $key ) {
141 $this->cache
->getWithSetCallback(
142 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
144 $this->assertEquals( 3, $hit, "Values cached" );
146 foreach ( $keys as $i => $key ) {
147 $this->cache
->getWithSetCallback(
148 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
150 $this->assertEquals( 6, $hit );
152 foreach ( $keys as $i => $key ) {
153 $this->cache
->getWithSetCallback(
154 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
156 $this->assertEquals( 6, $hit, "New values cached" );
158 foreach ( $keys as $i => $key ) {
159 $this->cache
->delete( $key );
160 $this->cache
->getWithSetCallback(
161 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
163 $this->assertEquals( 9, $hit, "Values evicted" );
165 $key = reset( $keys );
166 // Get into cache (default process cache group)
167 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
168 $this->assertEquals( 10, $hit, "Value calculated" );
169 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
170 $this->assertEquals( 10, $hit, "Value cached" );
171 $outerCallback = function () use ( &$callback, $key ) {
172 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
176 // Outer key misses and refuses inner key process cache value
177 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
178 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
182 * @dataProvider getWithSetCallback_provider
183 * @covers WANObjectCache::getWithSetCallback()
184 * @covers WANObjectCache::doGetWithSetCallback()
185 * @param array $extOpts
186 * @param bool $versioned
188 public function testGetWithSetCallback( array $extOpts, $versioned ) {
189 $cache = $this->cache
;
191 $key = wfRandomString();
192 $value = wfRandomString();
193 $cKey1 = wfRandomString();
194 $cKey2 = wfRandomString();
199 $func = function ( $old, &$ttl, &$opts, $asOf )
200 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
205 $ttl = 20; // override with another value
210 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
211 $this->assertEquals( $value, $v, "Value returned" );
212 $this->assertEquals( 1, $wasSet, "Value regenerated" );
213 $this->assertFalse( $priorValue, "No prior value" );
214 $this->assertNull( $priorAsOf, "No prior value" );
217 $cache->get( $key, $curTTL );
218 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
219 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
222 $v = $cache->getWithSetCallback(
223 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
224 $this->assertEquals( $value, $v, "Value returned" );
225 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
227 $mockWallClock = microtime( true );
228 $priorTime = $mockWallClock; // reference time
229 $cache->setMockTime( $mockWallClock );
234 $v = $cache->getWithSetCallback(
235 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
237 $this->assertEquals( $value, $v, "Value returned" );
238 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
239 $this->assertEquals( $value, $priorValue, "Has prior value" );
240 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
241 $t1 = $cache->getCheckKeyTime( $cKey1 );
242 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
243 $t2 = $cache->getCheckKeyTime( $cKey2 );
244 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
246 $mockWallClock +
= 0.01;
247 $priorTime = $mockWallClock; // reference time
249 $v = $cache->getWithSetCallback(
250 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
252 $this->assertEquals( $value, $v, "Value returned" );
253 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
254 $t1 = $cache->getCheckKeyTime( $cKey1 );
255 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
256 $t2 = $cache->getCheckKeyTime( $cKey2 );
257 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
260 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
262 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
264 $this->assertEquals( $value, $v, "Value returned" );
266 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
269 $key = wfRandomString();
270 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
271 $this->assertEquals( $value, $v, "Value returned" );
272 $cache->delete( $key );
273 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
274 $this->assertEquals( $value, $v, "Value still returned after deleted" );
275 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
277 $oldValReceived = -1;
278 $oldAsOfReceived = -1;
279 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
280 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
282 $oldValReceived = $oldVal;
283 $oldAsOfReceived = $oldAsOf;
285 return 'xxx' . $wasSet;
288 $mockWallClock = microtime( true );
289 $priorTime = $mockWallClock; // reference time
292 $key = wfRandomString();
293 $v = $cache->getWithSetCallback(
294 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
295 $this->assertEquals( 'xxx1', $v, "Value returned" );
296 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
297 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
299 $mockWallClock +
= 40;
300 $v = $cache->getWithSetCallback(
301 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
302 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
303 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
304 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
305 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
307 $mockWallClock +
= 260;
308 $v = $cache->getWithSetCallback(
309 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
310 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
311 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
312 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
313 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
315 $mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL
- 1 );
317 $key = wfRandomString();
318 $checkKey = $cache->makeKey( 'template', 'X' );
319 $cache->touchCheckKey( $checkKey ); // init check key
320 $mockWallClock = $priorTime;
321 $v = $cache->getWithSetCallback(
323 $cache::TTL_INDEFINITE
,
325 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
327 $this->assertEquals( 'xxx1', $v, "Value returned" );
328 $this->assertEquals( 1, $wasSet, "Value computed" );
329 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
330 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
332 $mockWallClock +
= $cache::TTL_HOUR
; // some time passes
333 $v = $cache->getWithSetCallback(
335 $cache::TTL_INDEFINITE
,
337 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
339 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
340 $this->assertEquals( 1, $wasSet, "Cached value returned" );
342 $cache->touchCheckKey( $checkKey ); // make key stale
343 $mockWallClock +
= 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
345 $v = $cache->getWithSetCallback(
347 $cache::TTL_INDEFINITE
,
349 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
351 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
352 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
354 // Change of refresh increase to unity as staleness approaches graceTTL
355 $mockWallClock +
= $cache::TTL_WEEK
; // 8 days of being stale
356 $v = $cache->getWithSetCallback(
358 $cache::TTL_INDEFINITE
,
360 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
362 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
363 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
364 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
365 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
368 public static function getWithSetCallback_provider() {
371 [ [ 'version' => 1 ], true ]
375 public function testPreemtiveRefresh() {
378 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
384 $cache = new NearExpiringWANObjectCache( [
385 'cache' => new HashBagOStuff(),
390 $key = wfRandomString();
391 $opts = [ 'lowTTL' => 30 ];
392 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
393 $this->assertEquals( $value, $v, "Value returned" );
394 $this->assertEquals( 1, $wasSet, "Value calculated" );
395 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
396 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
399 $key = wfRandomString();
400 $opts = [ 'lowTTL' => 1 ];
401 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
402 $this->assertEquals( $value, $v, "Value returned" );
403 $this->assertEquals( 1, $wasSet, "Value calculated" );
404 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
405 $this->assertEquals( 1, $wasSet, "Value cached" );
408 $asyncHandler = function ( $callback ) use ( &$asycList ) {
409 $asycList[] = $callback;
411 $cache = new NearExpiringWANObjectCache( [
412 'cache' => new HashBagOStuff(),
414 'asyncHandler' => $asyncHandler
417 $mockWallClock = microtime( true );
418 $priorTime = $mockWallClock; // reference time
419 $cache->setMockTime( $mockWallClock );
422 $key = wfRandomString();
423 $opts = [ 'lowTTL' => 100 ];
424 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
425 $this->assertEquals( $value, $v, "Value returned" );
426 $this->assertEquals( 1, $wasSet, "Value calculated" );
427 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
428 $this->assertEquals( 1, $wasSet, "Cached value used" );
429 $this->assertEquals( $v, $value, "Value cached" );
431 $mockWallClock +
= 250;
432 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
433 $this->assertEquals( $value, $v, "Value returned" );
434 $this->assertEquals( 1, $wasSet, "Stale value used" );
435 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
436 $value = 'NewCatsInTown'; // change callback return value
437 $asycList[0](); // run the refresh callback
439 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
440 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
441 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
442 $this->assertEquals( $value, $v, "New value stored" );
444 $cache = new PopularityRefreshingWANObjectCache( [
445 'cache' => new HashBagOStuff(),
449 $mockWallClock = $priorTime;
450 $cache->setMockTime( $mockWallClock );
453 $key = wfRandomString();
454 $opts = [ 'hotTTR' => 900 ];
455 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
456 $this->assertEquals( $value, $v, "Value returned" );
457 $this->assertEquals( 1, $wasSet, "Value calculated" );
459 $mockWallClock +
= 30;
461 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
462 $this->assertEquals( 1, $wasSet, "Value cached" );
464 $mockWallClock = $priorTime;
466 $key = wfRandomString();
467 $opts = [ 'hotTTR' => 10 ];
468 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
469 $this->assertEquals( $value, $v, "Value returned" );
470 $this->assertEquals( 1, $wasSet, "Value calculated" );
472 $mockWallClock +
= 30;
474 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
475 $this->assertEquals( $value, $v, "Value returned" );
476 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
480 * @covers WANObjectCache::getWithSetCallback()
481 * @covers WANObjectCache::doGetWithSetCallback()
483 public function testGetWithSetCallback_invalidCallback() {
484 $this->setExpectedException( InvalidArgumentException
::class );
485 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
489 * @dataProvider getMultiWithSetCallback_provider
490 * @covers WANObjectCache::getMultiWithSetCallback
491 * @covers WANObjectCache::makeMultiKeys
492 * @covers WANObjectCache::getMulti
493 * @param array $extOpts
494 * @param bool $versioned
496 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
497 $cache = $this->cache
;
499 $keyA = wfRandomString();
500 $keyB = wfRandomString();
501 $keyC = wfRandomString();
502 $cKey1 = wfRandomString();
503 $cKey2 = wfRandomString();
508 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
509 &$wasSet, &$priorValue, &$priorAsOf
514 $ttl = 20; // override with another value
519 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
521 $v = $cache->getMultiWithSetCallback(
522 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
523 $this->assertEquals( $value, $v[$keyA], "Value returned" );
524 $this->assertEquals( 1, $wasSet, "Value regenerated" );
525 $this->assertFalse( $priorValue, "No prior value" );
526 $this->assertNull( $priorAsOf, "No prior value" );
529 $cache->get( $keyA, $curTTL );
530 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
531 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
535 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
536 $v = $cache->getMultiWithSetCallback(
537 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
538 $this->assertEquals( $value, $v[$keyB], "Value returned" );
539 $this->assertEquals( 1, $wasSet, "Value regenerated" );
540 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
541 $v = $cache->getMultiWithSetCallback(
542 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
543 $this->assertEquals( $value, $v[$keyB], "Value returned" );
544 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
545 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
547 $mockWallClock = microtime( true );
548 $priorTime = $mockWallClock; // reference time
549 $cache->setMockTime( $mockWallClock );
554 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
555 $v = $cache->getMultiWithSetCallback(
556 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
558 $this->assertEquals( $value, $v[$keyB], "Value returned" );
559 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
560 $this->assertEquals( $value, $priorValue, "Has prior value" );
561 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
562 $t1 = $cache->getCheckKeyTime( $cKey1 );
563 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
564 $t2 = $cache->getCheckKeyTime( $cKey2 );
565 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
567 $mockWallClock +
= 0.01;
568 $priorTime = $mockWallClock;
571 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
572 $v = $cache->getMultiWithSetCallback(
573 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
575 $this->assertEquals( $value, $v[$keyC], "Value returned" );
576 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
577 $t1 = $cache->getCheckKeyTime( $cKey1 );
578 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
579 $t2 = $cache->getCheckKeyTime( $cKey2 );
580 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
583 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
585 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
587 $this->assertEquals( $value, $v, "Value returned" );
589 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
592 $key = wfRandomString();
593 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
594 $v = $cache->getMultiWithSetCallback(
595 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
596 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
597 $cache->delete( $key );
598 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
599 $v = $cache->getMultiWithSetCallback(
600 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
601 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
602 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
605 $ids = [ 1, 2, 3, 4, 5, 6 ];
606 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
607 return $wanCache->makeKey( 'test', $id );
609 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
610 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
615 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
618 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
619 array_values( $values ),
620 "Correct values in correct order"
623 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
624 array_keys( $values ),
625 "Correct keys in correct order"
627 $this->assertEquals( count( $ids ), $calls );
629 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
630 $this->assertEquals( count( $ids ), $calls, "Values cached" );
632 // Mock the BagOStuff to assure only one getMulti() call given process caching
633 $localBag = $this->getMockBuilder( HashBagOStuff
::class )
634 ->setMethods( [ 'getMulti' ] )->getMock();
635 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
636 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
637 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
639 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
641 // Warm the process cache
642 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
644 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
645 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
647 // Use the process cache
649 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
650 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
654 public static function getMultiWithSetCallback_provider() {
657 [ [ 'version' => 1 ], true ]
662 * @dataProvider getMultiWithUnionSetCallback_provider
663 * @covers WANObjectCache::getMultiWithUnionSetCallback()
664 * @covers WANObjectCache::makeMultiKeys()
665 * @param array $extOpts
666 * @param bool $versioned
668 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
669 $cache = $this->cache
;
671 $keyA = wfRandomString();
672 $keyB = wfRandomString();
673 $keyC = wfRandomString();
674 $cKey1 = wfRandomString();
675 $cKey2 = wfRandomString();
678 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
679 &$wasSet, &$priorValue, &$priorAsOf
682 foreach ( $ids as $id ) {
684 $newValues[$id] = "@$id$";
685 $ttls[$id] = 20; // override with another value
692 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
694 $v = $cache->getMultiWithUnionSetCallback(
695 $keyedIds, 30, $genFunc, $extOpts );
696 $this->assertEquals( $value, $v[$keyA], "Value returned" );
697 $this->assertEquals( 1, $wasSet, "Value regenerated" );
700 $cache->get( $keyA, $curTTL );
701 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
702 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
706 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
707 $v = $cache->getMultiWithUnionSetCallback(
708 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
709 $this->assertEquals( $value, $v[$keyB], "Value returned" );
710 $this->assertEquals( 1, $wasSet, "Value regenerated" );
711 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
712 $v = $cache->getMultiWithUnionSetCallback(
713 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
714 $this->assertEquals( $value, $v[$keyB], "Value returned" );
715 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
716 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
718 $mockWallClock = microtime( true );
719 $priorTime = $mockWallClock; // reference time
720 $cache->setMockTime( $mockWallClock );
725 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
726 $v = $cache->getMultiWithUnionSetCallback(
727 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
729 $this->assertEquals( $value, $v[$keyB], "Value returned" );
730 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
731 $t1 = $cache->getCheckKeyTime( $cKey1 );
732 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
733 $t2 = $cache->getCheckKeyTime( $cKey2 );
734 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
736 $mockWallClock +
= 0.01;
737 $priorTime = $mockWallClock;
740 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
741 $v = $cache->getMultiWithUnionSetCallback(
742 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
744 $this->assertEquals( $value, $v[$keyC], "Value returned" );
745 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
746 $t1 = $cache->getCheckKeyTime( $cKey1 );
747 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
748 $t2 = $cache->getCheckKeyTime( $cKey2 );
749 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
752 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
754 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
756 $this->assertEquals( $value, $v, "Value returned" );
758 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
761 $key = wfRandomString();
762 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
763 $v = $cache->getMultiWithUnionSetCallback(
764 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
765 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
766 $cache->delete( $key );
767 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
768 $v = $cache->getMultiWithUnionSetCallback(
769 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
770 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
771 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
774 $ids = [ 1, 2, 3, 4, 5, 6 ];
775 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
776 return $wanCache->makeKey( 'test', $id );
778 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
779 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
781 foreach ( $ids as $id ) {
783 $newValues[$id] = "val-{$id}";
788 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
791 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
792 array_values( $values ),
793 "Correct values in correct order"
796 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
797 array_keys( $values ),
798 "Correct keys in correct order"
800 $this->assertEquals( count( $ids ), $calls );
802 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
803 $this->assertEquals( count( $ids ), $calls, "Values cached" );
806 public static function getMultiWithUnionSetCallback_provider() {
809 [ [ 'version' => 1 ], true ]
814 * @covers WANObjectCache::getWithSetCallback()
815 * @covers WANObjectCache::doGetWithSetCallback()
817 public function testLockTSE() {
818 $cache = $this->cache
;
819 $key = wfRandomString();
820 $value = wfRandomString();
823 $func = function () use ( &$calls, $value, $cache, $key ) {
828 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
829 $this->assertEquals( $value, $ret );
830 $this->assertEquals( 1, $calls, 'Value was populated' );
832 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
833 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
835 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
836 $ret = $cache->getWithSetCallback( $key, 30, $func,
837 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
838 $this->assertEquals( $value, $ret, 'Old value used' );
839 $this->assertEquals( 1, $calls, 'Callback was not used' );
841 $cache->delete( $key );
842 $ret = $cache->getWithSetCallback( $key, 30, $func,
843 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
844 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
845 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
847 $ret = $cache->getWithSetCallback( $key, 30, $func,
848 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
849 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
850 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
854 * @covers WANObjectCache::getWithSetCallback()
855 * @covers WANObjectCache::doGetWithSetCallback()
856 * @covers WANObjectCache::set()
858 public function testLockTSESlow() {
859 $cache = $this->cache
;
860 $key = wfRandomString();
861 $value = wfRandomString();
864 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
866 $setOpts['since'] = microtime( true ) - 10;
867 // Immediately kill any mutex rather than waiting a second
868 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
872 // Value should be marked as stale due to snapshot lag
874 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
875 $this->assertEquals( $value, $ret );
876 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
877 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
878 $this->assertEquals( 1, $calls, 'Value was generated' );
880 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
881 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
882 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
883 $this->assertEquals( $value, $ret );
884 $this->assertEquals( 1, $calls, 'Callback was not used' );
888 * @covers WANObjectCache::getWithSetCallback()
889 * @covers WANObjectCache::doGetWithSetCallback()
891 public function testBusyValue() {
892 $cache = $this->cache
;
893 $key = wfRandomString();
894 $value = wfRandomString();
895 $busyValue = wfRandomString();
898 $func = function () use ( &$calls, $value, $cache, $key ) {
900 // Immediately kill any mutex rather than waiting a second
901 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
905 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
906 $this->assertEquals( $value, $ret );
907 $this->assertEquals( 1, $calls, 'Value was populated' );
909 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
910 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
912 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
913 $ret = $cache->getWithSetCallback( $key, 30, $func,
914 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
915 $this->assertEquals( $value, $ret, 'Callback used' );
916 $this->assertEquals( 2, $calls, 'Callback used' );
918 $ret = $cache->getWithSetCallback( $key, 30, $func,
919 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
920 $this->assertEquals( $value, $ret, 'Old value used' );
921 $this->assertEquals( 2, $calls, 'Callback was not used' );
923 $cache->delete( $key ); // no value at all anymore and still locked
924 $ret = $cache->getWithSetCallback( $key, 30, $func,
925 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
926 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
927 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
929 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
930 $ret = $cache->getWithSetCallback( $key, 30, $func,
931 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
932 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
933 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
935 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
936 $ret = $cache->getWithSetCallback( $key, 30, $func,
937 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
938 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
939 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
943 * @covers WANObjectCache::getMulti()
945 public function testGetMulti() {
946 $cache = $this->cache
;
948 $value1 = [ 'this' => 'is', 'a' => 'test' ];
949 $value2 = [ 'this' => 'is', 'another' => 'test' ];
951 $key1 = wfRandomString();
952 $key2 = wfRandomString();
953 $key3 = wfRandomString();
955 $cache->set( $key1, $value1, 5 );
956 $cache->set( $key2, $value2, 10 );
960 [ $key1 => $value1, $key2 => $value2 ],
961 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
962 'Result array populated'
965 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
966 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
967 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
969 $cKey1 = wfRandomString();
970 $cKey2 = wfRandomString();
972 $mockWallClock = microtime( true );
973 $priorTime = $mockWallClock; // reference time
974 $cache->setMockTime( $mockWallClock );
980 [ $key1 => $value1, $key2 => $value2 ],
981 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
982 "Result array populated even with new check keys"
984 $t1 = $cache->getCheckKeyTime( $cKey1 );
985 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
986 $t2 = $cache->getCheckKeyTime( $cKey2 );
987 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
988 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
989 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
990 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
996 [ $key1 => $value1, $key2 => $value2 ],
997 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
998 "Result array still populated even with new check keys"
1000 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1001 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1002 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1006 * @covers WANObjectCache::getMulti()
1007 * @covers WANObjectCache::processCheckKeys()
1009 public function testGetMultiCheckKeys() {
1010 $cache = $this->cache
;
1012 $checkAll = wfRandomString();
1013 $check1 = wfRandomString();
1014 $check2 = wfRandomString();
1015 $check3 = wfRandomString();
1016 $value1 = wfRandomString();
1017 $value2 = wfRandomString();
1019 $mockWallClock = microtime( true );
1020 $cache->setMockTime( $mockWallClock );
1022 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1023 // several seconds during the test to assert the behaviour.
1024 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1025 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1028 $mockWallClock +
= 0.100;
1030 $cache->set( 'key1', $value1, 10 );
1031 $cache->set( 'key2', $value2, 10 );
1034 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1040 $this->assertEquals(
1041 [ 'key1' => $value1, 'key2' => $value2 ],
1045 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1046 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1047 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1048 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1050 $mockWallClock +
= 0.100;
1051 $cache->touchCheckKey( $check1 );
1054 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1060 $this->assertEquals(
1061 [ 'key1' => $value1, 'key2' => $value2 ],
1063 'key1 expired by check1, but value still provided'
1065 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1066 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1068 $cache->touchCheckKey( $checkAll );
1071 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1077 $this->assertEquals(
1078 [ 'key1' => $value1, 'key2' => $value2 ],
1080 'All keys expired by checkAll, but value still provided'
1082 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1083 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1087 * @covers WANObjectCache::get()
1088 * @covers WANObjectCache::processCheckKeys()
1090 public function testCheckKeyInitHoldoff() {
1091 $cache = $this->cache
;
1093 for ( $i = 0; $i < 500; ++
$i ) {
1094 $key = wfRandomString();
1095 $checkKey = wfRandomString();
1097 $cache->get( $key, $curTTL, [ $checkKey ] );
1098 $cache->set( $key, 'val', 10 );
1100 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1102 $this->assertEquals( 'val', $v );
1103 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1106 for ( $i = 0; $i < 500; ++
$i ) {
1107 $key = wfRandomString();
1108 $checkKey = wfRandomString();
1110 $cache->set( $key, 'val', 10 );
1112 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1114 $this->assertEquals( 'val', $v );
1115 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1120 * @covers WANObjectCache::delete
1121 * @covers WANObjectCache::relayDelete
1122 * @covers WANObjectCache::relayPurge
1124 public function testDelete() {
1125 $key = wfRandomString();
1126 $value = wfRandomString();
1127 $this->cache
->set( $key, $value );
1130 $v = $this->cache
->get( $key, $curTTL );
1131 $this->assertEquals( $value, $v, "Key was created with value" );
1132 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1134 $this->cache
->delete( $key );
1137 $v = $this->cache
->get( $key, $curTTL );
1138 $this->assertFalse( $v, "Deleted key has false value" );
1139 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1141 $this->cache
->set( $key, $value . 'more' );
1142 $v = $this->cache
->get( $key, $curTTL );
1143 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1144 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1146 $this->cache
->set( $key, $value );
1147 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1150 $v = $this->cache
->get( $key, $curTTL );
1151 $this->assertFalse( $v, "Deleted key has false value" );
1152 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1154 $this->cache
->set( $key, $value );
1155 $v = $this->cache
->get( $key, $curTTL );
1156 $this->assertEquals( $value, $v, "Key was created with value" );
1157 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1161 * @dataProvider getWithSetCallback_versions_provider
1162 * @covers WANObjectCache::getWithSetCallback()
1163 * @covers WANObjectCache::doGetWithSetCallback()
1164 * @param array $extOpts
1165 * @param bool $versioned
1167 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1168 $cache = $this->cache
;
1170 $key = wfRandomString();
1171 $valueV1 = wfRandomString();
1172 $valueV2 = [ wfRandomString() ];
1175 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1181 $priorValue = false;
1183 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1184 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1185 $priorValue = $oldValue;
1186 $priorAsOf = $oldAsOf;
1189 return $valueV2; // new array format
1192 // Set the main key (version N if versioned)
1194 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1195 $this->assertEquals( $valueV1, $v, "Value returned" );
1196 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1197 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1198 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1199 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1202 // Set the key for version N+1 format
1203 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1205 // Start versioning now with the unversioned key still there
1206 $verOpts = [ 'version' => 1 ];
1209 // Value goes to secondary key since V1 already used $key
1211 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1212 $this->assertEquals( $valueV2, $v, "Value returned" );
1213 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1214 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1215 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1218 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1219 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1220 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1222 // Clear out the older or unversioned key
1223 $cache->delete( $key, 0 );
1225 // Set the key for next/first versioned format
1227 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1228 $this->assertEquals( $valueV2, $v, "Value returned" );
1229 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1231 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1232 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1233 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1236 public static function getWithSetCallback_versions_provider() {
1239 [ [ 'version' => 1 ], true ]
1244 * @covers WANObjectCache::useInterimHoldOffCaching
1245 * @covers WANObjectCache::getInterimValue
1247 public function testInterimHoldOffCaching() {
1248 $cache = $this->cache
;
1250 $value = 'CRL-40-940';
1252 $func = function () use ( &$wasCalled, $value ) {
1258 $cache->useInterimHoldOffCaching( true );
1260 $key = wfRandomString( 32 );
1261 $v = $cache->getWithSetCallback( $key, 60, $func );
1262 $v = $cache->getWithSetCallback( $key, 60, $func );
1263 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1264 $cache->delete( $key );
1265 $v = $cache->getWithSetCallback( $key, 60, $func );
1266 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1269 // Lock up the mutex so interim cache is used
1270 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1271 $v = $cache->getWithSetCallback( $key, 60, $func );
1272 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1273 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1275 $cache->useInterimHoldOffCaching( false );
1278 $key = wfRandomString( 32 );
1279 $v = $cache->getWithSetCallback( $key, 60, $func );
1280 $v = $cache->getWithSetCallback( $key, 60, $func );
1281 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1282 $cache->delete( $key );
1283 $v = $cache->getWithSetCallback( $key, 60, $func );
1284 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1285 $v = $cache->getWithSetCallback( $key, 60, $func );
1286 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1287 $v = $cache->getWithSetCallback( $key, 60, $func );
1288 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1289 // Lock up the mutex so interim cache is used
1290 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1291 $v = $cache->getWithSetCallback( $key, 60, $func );
1292 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1296 * @covers WANObjectCache::touchCheckKey
1297 * @covers WANObjectCache::resetCheckKey
1298 * @covers WANObjectCache::getCheckKeyTime
1299 * @covers WANObjectCache::getMultiCheckKeyTime
1300 * @covers WANObjectCache::makePurgeValue
1301 * @covers WANObjectCache::parsePurgeValue
1303 public function testTouchKeys() {
1304 $cache = $this->cache
;
1305 $key = wfRandomString();
1307 $mockWallClock = microtime( true );
1308 $priorTime = $mockWallClock; // reference time
1309 $cache->setMockTime( $mockWallClock );
1311 $mockWallClock +
= 0.100;
1312 $t0 = $cache->getCheckKeyTime( $key );
1313 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1315 $priorTime = $mockWallClock;
1316 $mockWallClock +
= 0.100;
1317 $cache->touchCheckKey( $key );
1318 $t1 = $cache->getCheckKeyTime( $key );
1319 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1321 $t2 = $cache->getCheckKeyTime( $key );
1322 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1324 $mockWallClock +
= 0.100;
1325 $cache->touchCheckKey( $key );
1326 $t3 = $cache->getCheckKeyTime( $key );
1327 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1329 $t4 = $cache->getCheckKeyTime( $key );
1330 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1332 $mockWallClock +
= 0.100;
1333 $cache->resetCheckKey( $key );
1334 $t5 = $cache->getCheckKeyTime( $key );
1335 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1337 $t6 = $cache->getCheckKeyTime( $key );
1338 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1342 * @covers WANObjectCache::getMulti()
1344 public function testGetWithSeveralCheckKeys() {
1345 $key = wfRandomString();
1346 $tKey1 = wfRandomString();
1347 $tKey2 = wfRandomString();
1350 // Two check keys are newer (given hold-off) than $key, another is older
1351 $this->internalCache
->set(
1352 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1353 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1355 $this->internalCache
->set(
1356 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1357 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1359 $this->internalCache
->set(
1360 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1361 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1363 $this->cache
->set( $key, $value, 30 );
1366 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1367 $this->assertEquals( $value, $v, "Value matches" );
1368 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1369 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1373 * @covers WANObjectCache::reap()
1374 * @covers WANObjectCache::reapCheckKey()
1376 public function testReap() {
1377 $vKey1 = wfRandomString();
1378 $vKey2 = wfRandomString();
1379 $tKey1 = wfRandomString();
1380 $tKey2 = wfRandomString();
1383 $knownPurge = time() - 60;
1384 $goodTime = microtime( true ) - 5;
1385 $badTime = microtime( true ) - 300;
1387 $this->internalCache
->set(
1388 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1390 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1391 WANObjectCache
::FLD_VALUE
=> $value,
1392 WANObjectCache
::FLD_TTL
=> 3600,
1393 WANObjectCache
::FLD_TIME
=> $goodTime
1396 $this->internalCache
->set(
1397 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1399 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1400 WANObjectCache
::FLD_VALUE
=> $value,
1401 WANObjectCache
::FLD_TTL
=> 3600,
1402 WANObjectCache
::FLD_TIME
=> $badTime
1405 $this->internalCache
->set(
1406 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1407 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1409 $this->internalCache
->set(
1410 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1411 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1414 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1415 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1416 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1417 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1419 $this->assertFalse( $bad1 );
1420 $this->assertTrue( $bad2 );
1422 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1423 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1424 $this->assertFalse( $tBad1 );
1425 $this->assertTrue( $tBad2 );
1429 * @covers WANObjectCache::reap()
1431 public function testReap_fail() {
1432 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1433 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1434 $backend->expects( $this->once() )->method( 'get' )
1436 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1437 WANObjectCache
::FLD_VALUE
=> 'value',
1438 WANObjectCache
::FLD_TTL
=> 3600,
1439 WANObjectCache
::FLD_TIME
=> 300,
1441 $backend->expects( $this->once() )->method( 'changeTTL' )
1442 ->willReturn( false );
1444 $wanCache = new WANObjectCache( [
1445 'cache' => $backend,
1446 'pool' => 'testcache-hash',
1447 'relayer' => new EventRelayerNull( [] )
1451 $ret = $wanCache->reap( 'key', 360, $isStale );
1452 $this->assertTrue( $isStale, 'value was stale' );
1453 $this->assertFalse( $ret, 'changeTTL failed' );
1457 * @covers WANObjectCache::set()
1459 public function testSetWithLag() {
1462 $key = wfRandomString();
1463 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1464 $this->cache
->set( $key, $value, 30, $opts );
1465 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1467 $key = wfRandomString();
1468 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1469 $this->cache
->set( $key, $value, 30, $opts );
1470 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1472 $key = wfRandomString();
1473 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1474 $this->cache
->set( $key, $value, 30, $opts );
1475 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1479 * @covers WANObjectCache::set()
1481 public function testWritePending() {
1484 $key = wfRandomString();
1485 $opts = [ 'pending' => true ];
1486 $this->cache
->set( $key, $value, 30, $opts );
1487 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1490 public function testMcRouterSupport() {
1491 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1492 ->setMethods( [ 'set', 'delete' ] )->getMock();
1493 $localBag->expects( $this->never() )->method( 'set' );
1494 $localBag->expects( $this->never() )->method( 'delete' );
1495 $wanCache = new WANObjectCache( [
1496 'cache' => $localBag,
1497 'pool' => 'testcache-hash',
1498 'relayer' => new EventRelayerNull( [] ),
1499 'mcrouterAware' => true,
1500 'region' => 'pmtpa',
1501 'cluster' => 'mw-wan'
1503 $valFunc = function () {
1507 // None of these should use broadcasting commands (e.g. SET, DELETE)
1508 $wanCache->get( 'x' );
1509 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1510 $wanCache->getMulti( [ 'x', 'y' ] );
1511 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1512 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1513 $wanCache->getCheckKeyTime( 'zzz' );
1514 $wanCache->reap( 'x', time() - 300 );
1515 $wanCache->reap( 'zzz', time() - 300 );
1518 public function testMcRouterSupportBroadcastDelete() {
1519 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1520 ->setMethods( [ 'set' ] )->getMock();
1521 $wanCache = new WANObjectCache( [
1522 'cache' => $localBag,
1523 'pool' => 'testcache-hash',
1524 'relayer' => new EventRelayerNull( [] ),
1525 'mcrouterAware' => true,
1526 'region' => 'pmtpa',
1527 'cluster' => 'mw-wan'
1530 $localBag->expects( $this->once() )->method( 'set' )
1531 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX
. "test" );
1533 $wanCache->delete( 'test' );
1536 public function testMcRouterSupportBroadcastTouchCK() {
1537 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1538 ->setMethods( [ 'set' ] )->getMock();
1539 $wanCache = new WANObjectCache( [
1540 'cache' => $localBag,
1541 'pool' => 'testcache-hash',
1542 'relayer' => new EventRelayerNull( [] ),
1543 'mcrouterAware' => true,
1544 'region' => 'pmtpa',
1545 'cluster' => 'mw-wan'
1548 $localBag->expects( $this->once() )->method( 'set' )
1549 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1551 $wanCache->touchCheckKey( 'test' );
1554 public function testMcRouterSupportBroadcastResetCK() {
1555 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1556 ->setMethods( [ 'delete' ] )->getMock();
1557 $wanCache = new WANObjectCache( [
1558 'cache' => $localBag,
1559 'pool' => 'testcache-hash',
1560 'relayer' => new EventRelayerNull( [] ),
1561 'mcrouterAware' => true,
1562 'region' => 'pmtpa',
1563 'cluster' => 'mw-wan'
1566 $localBag->expects( $this->once() )->method( 'delete' )
1567 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1569 $wanCache->resetCheckKey( 'test' );
1572 public function testEpoch() {
1573 $bag = new HashBagOStuff();
1574 $cache = new WANObjectCache( [ 'cache' => $bag, 'pool' => 'testcache-hash' ] );
1575 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1577 $now = microtime( true );
1578 $cache->setMockTime( $now );
1580 $cache->set( $key, 'Do what thou Wilt' );
1581 $cache->touchCheckKey( $key );
1585 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1586 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1588 $cache = new WANObjectCache( [
1590 'pool' => 'testcache-hash',
1591 'epoch' => $now - 3600
1593 $cache->setMockTime( $now );
1595 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1596 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1599 $cache = new WANObjectCache( [
1601 'pool' => 'testcache-hash',
1602 'epoch' => $now +
3600
1604 $cache->setMockTime( $now );
1606 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1607 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1611 * @dataProvider provideAdaptiveTTL
1612 * @covers WANObjectCache::adaptiveTTL()
1613 * @param float|int $ago
1614 * @param int $maxTTL
1615 * @param int $minTTL
1616 * @param float $factor
1617 * @param int $adaptiveTTL
1619 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1620 $mtime = $ago ?
time() - $ago : $ago;
1622 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1624 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1625 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1627 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1629 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1630 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1633 public static function provideAdaptiveTTL() {
1635 [ 3600, 900, 30, 0.2, 720 ],
1636 [ 3600, 500, 30, 0.2, 500 ],
1637 [ 3600, 86400, 800, 0.2, 800 ],
1638 [ false, 86400, 800, 0.2, 800 ],
1639 [ null, 86400, 800, 0.2, 800 ]
1644 * @covers WANObjectCache::__construct
1645 * @covers WANObjectCache::newEmpty
1647 public function testNewEmpty() {
1648 $this->assertInstanceOf(
1649 WANObjectCache
::class,
1650 WANObjectCache
::newEmpty()
1655 * @covers WANObjectCache::setLogger
1657 public function testSetLogger() {
1658 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1662 * @covers WANObjectCache::getQoS
1664 public function testGetQoS() {
1665 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1666 ->setMethods( [ 'getQoS' ] )->getMock();
1667 $backend->expects( $this->once() )->method( 'getQoS' )
1668 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1669 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1672 $wanCache::QOS_UNKNOWN
,
1673 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1678 * @covers WANObjectCache::makeKey
1680 public function testMakeKey() {
1681 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1682 ->setMethods( [ 'makeKey' ] )->getMock();
1683 $backend->expects( $this->once() )->method( 'makeKey' )
1684 ->willReturn( 'special' );
1686 $wanCache = new WANObjectCache( [
1687 'cache' => $backend,
1688 'pool' => 'testcache-hash',
1689 'relayer' => new EventRelayerNull( [] )
1692 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1696 * @covers WANObjectCache::makeGlobalKey
1698 public function testMakeGlobalKey() {
1699 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1700 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1701 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1702 ->willReturn( 'special' );
1704 $wanCache = new WANObjectCache( [
1705 'cache' => $backend,
1706 'pool' => 'testcache-hash',
1707 'relayer' => new EventRelayerNull( [] )
1710 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1713 public static function statsKeyProvider() {
1715 [ 'domain:page:5', 'page' ],
1716 [ 'domain:main-key', 'main-key' ],
1717 [ 'domain:page:history', 'page' ],
1718 [ 'missingdomainkey', 'missingdomainkey' ]
1723 * @dataProvider statsKeyProvider
1724 * @covers WANObjectCache::determineKeyClass
1726 public function testStatsKeyClass( $key, $class ) {
1727 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1728 'cache' => new HashBagOStuff
,
1729 'pool' => 'testcache-hash',
1730 'relayer' => new EventRelayerNull( [] )
1733 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1737 class NearExpiringWANObjectCache
extends WANObjectCache
{
1738 const CLOCK_SKEW
= 1;
1740 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1741 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1745 class PopularityRefreshingWANObjectCache
extends WANObjectCache
{
1746 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1747 return ( ( $now - $asOf ) > $timeTillRefresh );