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 TimeAdjustableWANObjectCache */
27 private $internalCache;
29 protected function setUp() {
32 $this->cache
= new TimeAdjustableWANObjectCache( [
33 'cache' => new TimeAdjustableHashBagOStuff(),
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 $priorTime = microtime( true ); // reference time
228 $cache->setTime( $priorTime );
230 $cache->addTime( 1 );
233 $v = $cache->getWithSetCallback(
234 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
236 $this->assertEquals( $value, $v, "Value returned" );
237 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
238 $this->assertEquals( $value, $priorValue, "Has prior value" );
239 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
240 $t1 = $cache->getCheckKeyTime( $cKey1 );
241 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
242 $t2 = $cache->getCheckKeyTime( $cKey2 );
243 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
245 $priorTime = $cache->addTime( 0.01 );
248 $v = $cache->getWithSetCallback(
249 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
251 $this->assertEquals( $value, $v, "Value returned" );
252 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
253 $t1 = $cache->getCheckKeyTime( $cKey1 );
254 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
255 $t2 = $cache->getCheckKeyTime( $cKey2 );
256 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
259 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
261 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
263 $this->assertEquals( $value, $v, "Value returned" );
265 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
268 $key = wfRandomString();
269 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
270 $this->assertEquals( $value, $v, "Value returned" );
271 $cache->delete( $key );
272 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
273 $this->assertEquals( $value, $v, "Value still returned after deleted" );
274 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
276 $oldValReceived = -1;
277 $oldAsOfReceived = -1;
278 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
279 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
281 $oldValReceived = $oldVal;
282 $oldAsOfReceived = $oldAsOf;
284 return 'xxx' . $wasSet;
287 $now = microtime( true ); // reference time
290 $key = wfRandomString();
291 $v = $cache->getWithSetCallback(
292 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
293 $this->assertEquals( 'xxx1', $v, "Value returned" );
294 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
295 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
297 $cache->addTime( 40 );
298 $v = $cache->getWithSetCallback(
299 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
300 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
301 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
302 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
303 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
305 $cache->addTime( 260 );
306 $v = $cache->getWithSetCallback(
307 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
308 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
309 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
310 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
311 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
314 $key = wfRandomString();
315 $checkKey = $cache->makeKey( 'template', 'X' );
316 $cache->setTime( $now - $cache::HOLDOFF_TTL
- 1 );
317 $cache->touchCheckKey( $checkKey ); // init check key
318 $cache->setTime( $now );
319 $v = $cache->getWithSetCallback(
321 $cache::TTL_INDEFINITE
,
323 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
325 $this->assertEquals( 'xxx1', $v, "Value returned" );
326 $this->assertEquals( 1, $wasSet, "Value computed" );
327 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
328 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
330 $cache->addTime( $cache::TTL_HOUR
); // some time passes
331 $v = $cache->getWithSetCallback(
333 $cache::TTL_INDEFINITE
,
335 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
337 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
338 $this->assertEquals( 1, $wasSet, "Cached value returned" );
340 $cache->touchCheckKey( $checkKey ); // make key stale
341 $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
343 $v = $cache->getWithSetCallback(
345 $cache::TTL_INDEFINITE
,
347 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
349 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
350 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
352 // Change of refresh increase to unity as staleness approaches graceTTL
354 $cache->addTime( $cache::TTL_WEEK
); // 8 days of being stale
355 $v = $cache->getWithSetCallback(
357 $cache::TTL_INDEFINITE
,
359 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
361 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
362 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
363 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
364 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
367 public static function getWithSetCallback_provider() {
370 [ [ 'version' => 1 ], true ]
374 public function testPreemtiveRefresh() {
377 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
383 $cache = new NearExpiringWANObjectCache( [
384 'cache' => new HashBagOStuff(),
389 $key = wfRandomString();
390 $opts = [ 'lowTTL' => 30 ];
391 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
392 $this->assertEquals( $value, $v, "Value returned" );
393 $this->assertEquals( 1, $wasSet, "Value calculated" );
394 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
395 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
398 $key = wfRandomString();
399 $opts = [ 'lowTTL' => 1 ];
400 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
401 $this->assertEquals( $value, $v, "Value returned" );
402 $this->assertEquals( 1, $wasSet, "Value calculated" );
403 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
404 $this->assertEquals( 1, $wasSet, "Value cached" );
407 $asyncHandler = function ( $callback ) use ( &$asycList ) {
408 $asycList[] = $callback;
410 $cache = new NearExpiringWANObjectCache( [
411 'cache' => new TimeAdjustableHashBagOStuff(),
413 'asyncHandler' => $asyncHandler
416 $now = microtime( true ); // reference time
417 $cache->setTime( $now );
420 $key = wfRandomString();
421 $checkKey = wfRandomString();
422 $opts = [ 'lowTTL' => 100 ];
423 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
424 $this->assertEquals( $value, $v, "Value returned" );
425 $this->assertEquals( 1, $wasSet, "Value calculated" );
426 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
427 $this->assertEquals( 1, $wasSet, "Cached value used" );
428 $this->assertEquals( $v, $value, "Value cached" );
430 $cache->setTime( $now +
250 );
431 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
432 $this->assertEquals( $value, $v, "Value returned" );
433 $this->assertEquals( 1, $wasSet, "Stale value used" );
434 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
435 $value = 'NewCatsInTown'; // change callback return value
436 $asycList[0](); // run the refresh callback
438 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
439 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
440 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
441 $this->assertEquals( $value, $v, "New value stored" );
443 $cache = new PopularityRefreshingWANObjectCache( [
444 'cache' => new TimeAdjustableHashBagOStuff(),
448 $cache->setTime( $now );
451 $key = wfRandomString();
452 $opts = [ 'hotTTR' => 900 ];
453 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
454 $this->assertEquals( $value, $v, "Value returned" );
455 $this->assertEquals( 1, $wasSet, "Value calculated" );
456 $cache->setTime( $now +
30 );
457 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
458 $this->assertEquals( 1, $wasSet, "Value cached" );
461 $key = wfRandomString();
462 $opts = [ 'hotTTR' => 10 ];
463 $cache->setTime( $now );
464 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
465 $this->assertEquals( $value, $v, "Value returned" );
466 $this->assertEquals( 1, $wasSet, "Value calculated" );
467 $cache->setTime( $now +
30 );
468 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
469 $this->assertEquals( $value, $v, "Value returned" );
470 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
474 * @covers WANObjectCache::getWithSetCallback()
475 * @covers WANObjectCache::doGetWithSetCallback()
477 public function testGetWithSetCallback_invalidCallback() {
478 $this->setExpectedException( InvalidArgumentException
::class );
479 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
483 * @dataProvider getMultiWithSetCallback_provider
484 * @covers WANObjectCache::getMultiWithSetCallback
485 * @covers WANObjectCache::makeMultiKeys
486 * @covers WANObjectCache::getMulti
487 * @param array $extOpts
488 * @param bool $versioned
490 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
491 $cache = $this->cache
;
493 $keyA = wfRandomString();
494 $keyB = wfRandomString();
495 $keyC = wfRandomString();
496 $cKey1 = wfRandomString();
497 $cKey2 = wfRandomString();
502 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
503 &$wasSet, &$priorValue, &$priorAsOf
508 $ttl = 20; // override with another value
513 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
515 $v = $cache->getMultiWithSetCallback(
516 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
517 $this->assertEquals( $value, $v[$keyA], "Value returned" );
518 $this->assertEquals( 1, $wasSet, "Value regenerated" );
519 $this->assertFalse( $priorValue, "No prior value" );
520 $this->assertNull( $priorAsOf, "No prior value" );
523 $cache->get( $keyA, $curTTL );
524 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
525 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
529 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
530 $v = $cache->getMultiWithSetCallback(
531 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
532 $this->assertEquals( $value, $v[$keyB], "Value returned" );
533 $this->assertEquals( 1, $wasSet, "Value regenerated" );
534 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
535 $v = $cache->getMultiWithSetCallback(
536 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
537 $this->assertEquals( $value, $v[$keyB], "Value returned" );
538 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
539 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
541 $priorTime = microtime( true ); // reference time
542 $cache->setTime( $priorTime );
544 $cache->addTime( 1 );
547 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
548 $v = $cache->getMultiWithSetCallback(
549 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
551 $this->assertEquals( $value, $v[$keyB], "Value returned" );
552 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
553 $this->assertEquals( $value, $priorValue, "Has prior value" );
554 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
555 $t1 = $cache->getCheckKeyTime( $cKey1 );
556 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
557 $t2 = $cache->getCheckKeyTime( $cKey2 );
558 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
560 $priorTime = $cache->addTime( 0.01 );
563 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
564 $v = $cache->getMultiWithSetCallback(
565 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
567 $this->assertEquals( $value, $v[$keyC], "Value returned" );
568 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
569 $t1 = $cache->getCheckKeyTime( $cKey1 );
570 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
571 $t2 = $cache->getCheckKeyTime( $cKey2 );
572 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
575 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
577 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
579 $this->assertEquals( $value, $v, "Value returned" );
581 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
584 $key = wfRandomString();
585 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
586 $v = $cache->getMultiWithSetCallback(
587 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
588 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
589 $cache->delete( $key );
590 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
591 $v = $cache->getMultiWithSetCallback(
592 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
593 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
594 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
597 $ids = [ 1, 2, 3, 4, 5, 6 ];
598 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
599 return $wanCache->makeKey( 'test', $id );
601 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
602 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
607 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
610 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
611 array_values( $values ),
612 "Correct values in correct order"
615 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
616 array_keys( $values ),
617 "Correct keys in correct order"
619 $this->assertEquals( count( $ids ), $calls );
621 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
622 $this->assertEquals( count( $ids ), $calls, "Values cached" );
624 // Mock the BagOStuff to assure only one getMulti() call given process caching
625 $localBag = $this->getMockBuilder( HashBagOStuff
::class )
626 ->setMethods( [ 'getMulti' ] )->getMock();
627 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
628 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
629 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
631 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
633 // Warm the process cache
634 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
636 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
637 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
639 // Use the process cache
641 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
642 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
646 public static function getMultiWithSetCallback_provider() {
649 [ [ 'version' => 1 ], true ]
654 * @dataProvider getMultiWithUnionSetCallback_provider
655 * @covers WANObjectCache::getMultiWithUnionSetCallback()
656 * @covers WANObjectCache::makeMultiKeys()
657 * @param array $extOpts
658 * @param bool $versioned
660 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
661 $cache = $this->cache
;
663 $keyA = wfRandomString();
664 $keyB = wfRandomString();
665 $keyC = wfRandomString();
666 $cKey1 = wfRandomString();
667 $cKey2 = wfRandomString();
670 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
671 &$wasSet, &$priorValue, &$priorAsOf
674 foreach ( $ids as $id ) {
676 $newValues[$id] = "@$id$";
677 $ttls[$id] = 20; // override with another value
684 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
686 $v = $cache->getMultiWithUnionSetCallback(
687 $keyedIds, 30, $genFunc, $extOpts );
688 $this->assertEquals( $value, $v[$keyA], "Value returned" );
689 $this->assertEquals( 1, $wasSet, "Value regenerated" );
692 $cache->get( $keyA, $curTTL );
693 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
694 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
698 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
699 $v = $cache->getMultiWithUnionSetCallback(
700 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
701 $this->assertEquals( $value, $v[$keyB], "Value returned" );
702 $this->assertEquals( 1, $wasSet, "Value regenerated" );
703 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
704 $v = $cache->getMultiWithUnionSetCallback(
705 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
706 $this->assertEquals( $value, $v[$keyB], "Value returned" );
707 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
708 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
710 $priorTime = microtime( true ); // reference time
711 $cache->setTime( $priorTime );
713 $cache->addTime( 1 );
716 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
717 $v = $cache->getMultiWithUnionSetCallback(
718 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
720 $this->assertEquals( $value, $v[$keyB], "Value returned" );
721 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
722 $t1 = $cache->getCheckKeyTime( $cKey1 );
723 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
724 $t2 = $cache->getCheckKeyTime( $cKey2 );
725 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
727 $priorTime = $cache->addTime( 0.01 );
730 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
731 $v = $cache->getMultiWithUnionSetCallback(
732 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
734 $this->assertEquals( $value, $v[$keyC], "Value returned" );
735 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
736 $t1 = $cache->getCheckKeyTime( $cKey1 );
737 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
738 $t2 = $cache->getCheckKeyTime( $cKey2 );
739 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
742 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
744 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
746 $this->assertEquals( $value, $v, "Value returned" );
748 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
751 $key = wfRandomString();
752 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
753 $v = $cache->getMultiWithUnionSetCallback(
754 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
755 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
756 $cache->delete( $key );
757 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
758 $v = $cache->getMultiWithUnionSetCallback(
759 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
760 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
761 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
764 $ids = [ 1, 2, 3, 4, 5, 6 ];
765 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
766 return $wanCache->makeKey( 'test', $id );
768 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
769 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
771 foreach ( $ids as $id ) {
773 $newValues[$id] = "val-{$id}";
778 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
781 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
782 array_values( $values ),
783 "Correct values in correct order"
786 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
787 array_keys( $values ),
788 "Correct keys in correct order"
790 $this->assertEquals( count( $ids ), $calls );
792 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
793 $this->assertEquals( count( $ids ), $calls, "Values cached" );
796 public static function getMultiWithUnionSetCallback_provider() {
799 [ [ 'version' => 1 ], true ]
804 * @covers WANObjectCache::getWithSetCallback()
805 * @covers WANObjectCache::doGetWithSetCallback()
807 public function testLockTSE() {
808 $cache = $this->cache
;
809 $key = wfRandomString();
810 $value = wfRandomString();
813 $func = function () use ( &$calls, $value, $cache, $key ) {
818 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
819 $this->assertEquals( $value, $ret );
820 $this->assertEquals( 1, $calls, 'Value was populated' );
822 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
823 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
825 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
826 $ret = $cache->getWithSetCallback( $key, 30, $func,
827 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
828 $this->assertEquals( $value, $ret, 'Old value used' );
829 $this->assertEquals( 1, $calls, 'Callback was not used' );
831 $cache->delete( $key );
832 $ret = $cache->getWithSetCallback( $key, 30, $func,
833 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
834 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
835 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
837 $ret = $cache->getWithSetCallback( $key, 30, $func,
838 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
839 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
840 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
844 * @covers WANObjectCache::getWithSetCallback()
845 * @covers WANObjectCache::doGetWithSetCallback()
846 * @covers WANObjectCache::set()
848 public function testLockTSESlow() {
849 $cache = $this->cache
;
850 $key = wfRandomString();
851 $value = wfRandomString();
854 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
856 $setOpts['since'] = microtime( true ) - 10;
857 // Immediately kill any mutex rather than waiting a second
858 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
862 // Value should be marked as stale due to snapshot lag
864 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
865 $this->assertEquals( $value, $ret );
866 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
867 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
868 $this->assertEquals( 1, $calls, 'Value was generated' );
870 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
871 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
872 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
873 $this->assertEquals( $value, $ret );
874 $this->assertEquals( 1, $calls, 'Callback was not used' );
878 * @covers WANObjectCache::getWithSetCallback()
879 * @covers WANObjectCache::doGetWithSetCallback()
881 public function testBusyValue() {
882 $cache = $this->cache
;
883 $key = wfRandomString();
884 $value = wfRandomString();
885 $busyValue = wfRandomString();
888 $func = function () use ( &$calls, $value, $cache, $key ) {
890 // Immediately kill any mutex rather than waiting a second
891 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
895 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
896 $this->assertEquals( $value, $ret );
897 $this->assertEquals( 1, $calls, 'Value was populated' );
899 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
900 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
902 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
903 $ret = $cache->getWithSetCallback( $key, 30, $func,
904 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
905 $this->assertEquals( $value, $ret, 'Callback used' );
906 $this->assertEquals( 2, $calls, 'Callback used' );
908 $ret = $cache->getWithSetCallback( $key, 30, $func,
909 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
910 $this->assertEquals( $value, $ret, 'Old value used' );
911 $this->assertEquals( 2, $calls, 'Callback was not used' );
913 $cache->delete( $key ); // no value at all anymore and still locked
914 $ret = $cache->getWithSetCallback( $key, 30, $func,
915 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
916 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
917 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
919 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
920 $ret = $cache->getWithSetCallback( $key, 30, $func,
921 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
922 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
923 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
925 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
926 $ret = $cache->getWithSetCallback( $key, 30, $func,
927 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
928 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
929 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
933 * @covers WANObjectCache::getMulti()
935 public function testGetMulti() {
936 $cache = $this->cache
;
938 $value1 = [ 'this' => 'is', 'a' => 'test' ];
939 $value2 = [ 'this' => 'is', 'another' => 'test' ];
941 $key1 = wfRandomString();
942 $key2 = wfRandomString();
943 $key3 = wfRandomString();
945 $cache->set( $key1, $value1, 5 );
946 $cache->set( $key2, $value2, 10 );
950 [ $key1 => $value1, $key2 => $value2 ],
951 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
952 'Result array populated'
955 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
956 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
957 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
959 $cKey1 = wfRandomString();
960 $cKey2 = wfRandomString();
962 $priorTime = microtime( true ); // reference time
963 $cache->setTime( $priorTime );
965 $cache->addTime( 1 );
969 [ $key1 => $value1, $key2 => $value2 ],
970 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
971 "Result array populated even with new check keys"
973 $t1 = $cache->getCheckKeyTime( $cKey1 );
974 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
975 $t2 = $cache->getCheckKeyTime( $cKey2 );
976 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
977 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
978 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
979 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
981 $cache->addTime( 1 );
985 [ $key1 => $value1, $key2 => $value2 ],
986 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
987 "Result array still populated even with new check keys"
989 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
990 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
991 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
995 * @covers WANObjectCache::getMulti()
996 * @covers WANObjectCache::processCheckKeys()
998 public function testGetMultiCheckKeys() {
999 $cache = $this->cache
;
1001 $checkAll = wfRandomString();
1002 $check1 = wfRandomString();
1003 $check2 = wfRandomString();
1004 $check3 = wfRandomString();
1005 $value1 = wfRandomString();
1006 $value2 = wfRandomString();
1008 $cache->setTime( microtime( true ) );
1010 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1011 // several seconds during the test to assert the behaviour.
1012 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1013 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1016 $cache->addTime( 0.100 );
1018 $cache->set( 'key1', $value1, 10 );
1019 $cache->set( 'key2', $value2, 10 );
1022 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1028 $this->assertEquals(
1029 [ 'key1' => $value1, 'key2' => $value2 ],
1033 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1034 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1035 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1036 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1038 $cache->addTime( 0.100 );
1039 $cache->touchCheckKey( $check1 );
1042 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1048 $this->assertEquals(
1049 [ 'key1' => $value1, 'key2' => $value2 ],
1051 'key1 expired by check1, but value still provided'
1053 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1054 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1056 $cache->touchCheckKey( $checkAll );
1059 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1065 $this->assertEquals(
1066 [ 'key1' => $value1, 'key2' => $value2 ],
1068 'All keys expired by checkAll, but value still provided'
1070 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1071 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1075 * @covers WANObjectCache::get()
1076 * @covers WANObjectCache::processCheckKeys()
1078 public function testCheckKeyInitHoldoff() {
1079 $cache = $this->cache
;
1081 for ( $i = 0; $i < 500; ++
$i ) {
1082 $key = wfRandomString();
1083 $checkKey = wfRandomString();
1085 $cache->get( $key, $curTTL, [ $checkKey ] );
1086 $cache->set( $key, 'val', 10 );
1088 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1090 $this->assertEquals( 'val', $v );
1091 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1094 for ( $i = 0; $i < 500; ++
$i ) {
1095 $key = wfRandomString();
1096 $checkKey = wfRandomString();
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 (set/hit)" );
1108 * @covers WANObjectCache::delete
1109 * @covers WANObjectCache::relayDelete
1110 * @covers WANObjectCache::relayPurge
1112 public function testDelete() {
1113 $key = wfRandomString();
1114 $value = wfRandomString();
1115 $this->cache
->set( $key, $value );
1118 $v = $this->cache
->get( $key, $curTTL );
1119 $this->assertEquals( $value, $v, "Key was created with value" );
1120 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1122 $this->cache
->delete( $key );
1125 $v = $this->cache
->get( $key, $curTTL );
1126 $this->assertFalse( $v, "Deleted key has false value" );
1127 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1129 $this->cache
->set( $key, $value . 'more' );
1130 $v = $this->cache
->get( $key, $curTTL );
1131 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1132 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1134 $this->cache
->set( $key, $value );
1135 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1138 $v = $this->cache
->get( $key, $curTTL );
1139 $this->assertFalse( $v, "Deleted key has false value" );
1140 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1142 $this->cache
->set( $key, $value );
1143 $v = $this->cache
->get( $key, $curTTL );
1144 $this->assertEquals( $value, $v, "Key was created with value" );
1145 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1149 * @dataProvider getWithSetCallback_versions_provider
1150 * @covers WANObjectCache::getWithSetCallback()
1151 * @covers WANObjectCache::doGetWithSetCallback()
1152 * @param array $extOpts
1153 * @param bool $versioned
1155 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1156 $cache = $this->cache
;
1158 $key = wfRandomString();
1159 $valueV1 = wfRandomString();
1160 $valueV2 = [ wfRandomString() ];
1163 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1169 $priorValue = false;
1171 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1172 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1173 $priorValue = $oldValue;
1174 $priorAsOf = $oldAsOf;
1177 return $valueV2; // new array format
1180 // Set the main key (version N if versioned)
1182 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1183 $this->assertEquals( $valueV1, $v, "Value returned" );
1184 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1185 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1186 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1187 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1190 // Set the key for version N+1 format
1191 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1193 // Start versioning now with the unversioned key still there
1194 $verOpts = [ 'version' => 1 ];
1197 // Value goes to secondary key since V1 already used $key
1199 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1200 $this->assertEquals( $valueV2, $v, "Value returned" );
1201 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1202 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1203 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1206 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1207 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1208 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1210 // Clear out the older or unversioned key
1211 $cache->delete( $key, 0 );
1213 // Set the key for next/first versioned format
1215 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1216 $this->assertEquals( $valueV2, $v, "Value returned" );
1217 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1219 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1220 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1221 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1224 public static function getWithSetCallback_versions_provider() {
1227 [ [ 'version' => 1 ], true ]
1232 * @covers WANObjectCache::useInterimHoldOffCaching
1233 * @covers WANObjectCache::getInterimValue
1235 public function testInterimHoldOffCaching() {
1236 $cache = $this->cache
;
1238 $value = 'CRL-40-940';
1240 $func = function () use ( &$wasCalled, $value ) {
1246 $cache->useInterimHoldOffCaching( true );
1248 $key = wfRandomString( 32 );
1249 $v = $cache->getWithSetCallback( $key, 60, $func );
1250 $v = $cache->getWithSetCallback( $key, 60, $func );
1251 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1252 $cache->delete( $key );
1253 $v = $cache->getWithSetCallback( $key, 60, $func );
1254 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1255 $v = $cache->getWithSetCallback( $key, 60, $func );
1256 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1257 // Lock up the mutex so interim cache is used
1258 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1259 $v = $cache->getWithSetCallback( $key, 60, $func );
1260 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1261 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1263 $cache->useInterimHoldOffCaching( false );
1266 $key = wfRandomString( 32 );
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $v = $cache->getWithSetCallback( $key, 60, $func );
1269 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1270 $cache->delete( $key );
1271 $v = $cache->getWithSetCallback( $key, 60, $func );
1272 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1273 $v = $cache->getWithSetCallback( $key, 60, $func );
1274 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1275 $v = $cache->getWithSetCallback( $key, 60, $func );
1276 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1277 // Lock up the mutex so interim cache is used
1278 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1279 $v = $cache->getWithSetCallback( $key, 60, $func );
1280 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1284 * @covers WANObjectCache::touchCheckKey
1285 * @covers WANObjectCache::resetCheckKey
1286 * @covers WANObjectCache::getCheckKeyTime
1287 * @covers WANObjectCache::getMultiCheckKeyTime
1288 * @covers WANObjectCache::makePurgeValue
1289 * @covers WANObjectCache::parsePurgeValue
1291 public function testTouchKeys() {
1292 $cache = $this->cache
;
1293 $key = wfRandomString();
1295 $priorTime = microtime( true ); // reference time
1296 $cache->setTime( $priorTime );
1298 $newTime = $cache->addTime( 0.100 );
1299 $t0 = $cache->getCheckKeyTime( $key );
1300 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1302 $priorTime = $newTime;
1303 $newTime = $cache->addTime( 0.100 );
1304 $cache->touchCheckKey( $key );
1305 $t1 = $cache->getCheckKeyTime( $key );
1306 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1308 $t2 = $cache->getCheckKeyTime( $key );
1309 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1311 $cache->addTime( 0.100 );
1312 $cache->touchCheckKey( $key );
1313 $t3 = $cache->getCheckKeyTime( $key );
1314 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1316 $t4 = $cache->getCheckKeyTime( $key );
1317 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1319 $cache->addTime( 0.100 );
1320 $cache->resetCheckKey( $key );
1321 $t5 = $cache->getCheckKeyTime( $key );
1322 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1324 $t6 = $cache->getCheckKeyTime( $key );
1325 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1329 * @covers WANObjectCache::getMulti()
1331 public function testGetWithSeveralCheckKeys() {
1332 $key = wfRandomString();
1333 $tKey1 = wfRandomString();
1334 $tKey2 = wfRandomString();
1337 // Two check keys are newer (given hold-off) than $key, another is older
1338 $this->internalCache
->set(
1339 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1340 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1342 $this->internalCache
->set(
1343 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1344 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1346 $this->internalCache
->set(
1347 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1348 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1350 $this->cache
->set( $key, $value, 30 );
1353 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1354 $this->assertEquals( $value, $v, "Value matches" );
1355 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1356 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1360 * @covers WANObjectCache::reap()
1361 * @covers WANObjectCache::reapCheckKey()
1363 public function testReap() {
1364 $vKey1 = wfRandomString();
1365 $vKey2 = wfRandomString();
1366 $tKey1 = wfRandomString();
1367 $tKey2 = wfRandomString();
1370 $knownPurge = time() - 60;
1371 $goodTime = microtime( true ) - 5;
1372 $badTime = microtime( true ) - 300;
1374 $this->internalCache
->set(
1375 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1377 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1378 WANObjectCache
::FLD_VALUE
=> $value,
1379 WANObjectCache
::FLD_TTL
=> 3600,
1380 WANObjectCache
::FLD_TIME
=> $goodTime
1383 $this->internalCache
->set(
1384 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1386 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1387 WANObjectCache
::FLD_VALUE
=> $value,
1388 WANObjectCache
::FLD_TTL
=> 3600,
1389 WANObjectCache
::FLD_TIME
=> $badTime
1392 $this->internalCache
->set(
1393 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1394 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1396 $this->internalCache
->set(
1397 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1398 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1401 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1402 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1403 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1404 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1406 $this->assertFalse( $bad1 );
1407 $this->assertTrue( $bad2 );
1409 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1410 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1411 $this->assertFalse( $tBad1 );
1412 $this->assertTrue( $tBad2 );
1416 * @covers WANObjectCache::reap()
1418 public function testReap_fail() {
1419 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1420 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1421 $backend->expects( $this->once() )->method( 'get' )
1423 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1424 WANObjectCache
::FLD_VALUE
=> 'value',
1425 WANObjectCache
::FLD_TTL
=> 3600,
1426 WANObjectCache
::FLD_TIME
=> 300,
1428 $backend->expects( $this->once() )->method( 'changeTTL' )
1429 ->willReturn( false );
1431 $wanCache = new WANObjectCache( [
1432 'cache' => $backend,
1433 'pool' => 'testcache-hash',
1434 'relayer' => new EventRelayerNull( [] )
1438 $ret = $wanCache->reap( 'key', 360, $isStale );
1439 $this->assertTrue( $isStale, 'value was stale' );
1440 $this->assertFalse( $ret, 'changeTTL failed' );
1444 * @covers WANObjectCache::set()
1446 public function testSetWithLag() {
1449 $key = wfRandomString();
1450 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1451 $this->cache
->set( $key, $value, 30, $opts );
1452 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1454 $key = wfRandomString();
1455 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1456 $this->cache
->set( $key, $value, 30, $opts );
1457 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1459 $key = wfRandomString();
1460 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1461 $this->cache
->set( $key, $value, 30, $opts );
1462 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1466 * @covers WANObjectCache::set()
1468 public function testWritePending() {
1471 $key = wfRandomString();
1472 $opts = [ 'pending' => true ];
1473 $this->cache
->set( $key, $value, 30, $opts );
1474 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1477 public function testMcRouterSupport() {
1478 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1479 ->setMethods( [ 'set', 'delete' ] )->getMock();
1480 $localBag->expects( $this->never() )->method( 'set' );
1481 $localBag->expects( $this->never() )->method( 'delete' );
1482 $wanCache = new WANObjectCache( [
1483 'cache' => $localBag,
1484 'pool' => 'testcache-hash',
1485 'relayer' => new EventRelayerNull( [] ),
1486 'mcrouterAware' => true,
1487 'region' => 'pmtpa',
1488 'cluster' => 'mw-wan'
1490 $valFunc = function () {
1494 // None of these should use broadcasting commands (e.g. SET, DELETE)
1495 $wanCache->get( 'x' );
1496 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1497 $wanCache->getMulti( [ 'x', 'y' ] );
1498 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1499 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1500 $wanCache->getCheckKeyTime( 'zzz' );
1501 $wanCache->reap( 'x', time() - 300 );
1502 $wanCache->reap( 'zzz', time() - 300 );
1505 public function testMcRouterSupportBroadcastDelete() {
1506 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1507 ->setMethods( [ 'set' ] )->getMock();
1508 $wanCache = new WANObjectCache( [
1509 'cache' => $localBag,
1510 'pool' => 'testcache-hash',
1511 'relayer' => new EventRelayerNull( [] ),
1512 'mcrouterAware' => true,
1513 'region' => 'pmtpa',
1514 'cluster' => 'mw-wan'
1517 $localBag->expects( $this->once() )->method( 'set' )
1518 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX
. "test" );
1520 $wanCache->delete( 'test' );
1523 public function testMcRouterSupportBroadcastTouchCK() {
1524 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1525 ->setMethods( [ 'set' ] )->getMock();
1526 $wanCache = new WANObjectCache( [
1527 'cache' => $localBag,
1528 'pool' => 'testcache-hash',
1529 'relayer' => new EventRelayerNull( [] ),
1530 'mcrouterAware' => true,
1531 'region' => 'pmtpa',
1532 'cluster' => 'mw-wan'
1535 $localBag->expects( $this->once() )->method( 'set' )
1536 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1538 $wanCache->touchCheckKey( 'test' );
1541 public function testMcRouterSupportBroadcastResetCK() {
1542 $localBag = $this->getMockBuilder( EmptyBagOStuff
::class )
1543 ->setMethods( [ 'delete' ] )->getMock();
1544 $wanCache = new WANObjectCache( [
1545 'cache' => $localBag,
1546 'pool' => 'testcache-hash',
1547 'relayer' => new EventRelayerNull( [] ),
1548 'mcrouterAware' => true,
1549 'region' => 'pmtpa',
1550 'cluster' => 'mw-wan'
1553 $localBag->expects( $this->once() )->method( 'delete' )
1554 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX
. "test" );
1556 $wanCache->resetCheckKey( 'test' );
1560 * @dataProvider provideAdaptiveTTL
1561 * @covers WANObjectCache::adaptiveTTL()
1562 * @param float|int $ago
1563 * @param int $maxTTL
1564 * @param int $minTTL
1565 * @param float $factor
1566 * @param int $adaptiveTTL
1568 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1569 $mtime = $ago ?
time() - $ago : $ago;
1571 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1573 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1574 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1576 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1578 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1579 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1582 public static function provideAdaptiveTTL() {
1584 [ 3600, 900, 30, 0.2, 720 ],
1585 [ 3600, 500, 30, 0.2, 500 ],
1586 [ 3600, 86400, 800, 0.2, 800 ],
1587 [ false, 86400, 800, 0.2, 800 ],
1588 [ null, 86400, 800, 0.2, 800 ]
1593 * @covers WANObjectCache::__construct
1594 * @covers WANObjectCache::newEmpty
1596 public function testNewEmpty() {
1597 $this->assertInstanceOf(
1598 WANObjectCache
::class,
1599 WANObjectCache
::newEmpty()
1604 * @covers WANObjectCache::setLogger
1606 public function testSetLogger() {
1607 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1611 * @covers WANObjectCache::getQoS
1613 public function testGetQoS() {
1614 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1615 ->setMethods( [ 'getQoS' ] )->getMock();
1616 $backend->expects( $this->once() )->method( 'getQoS' )
1617 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1618 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1621 $wanCache::QOS_UNKNOWN
,
1622 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1627 * @covers WANObjectCache::makeKey
1629 public function testMakeKey() {
1630 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1631 ->setMethods( [ 'makeKey' ] )->getMock();
1632 $backend->expects( $this->once() )->method( 'makeKey' )
1633 ->willReturn( 'special' );
1635 $wanCache = new WANObjectCache( [
1636 'cache' => $backend,
1637 'pool' => 'testcache-hash',
1638 'relayer' => new EventRelayerNull( [] )
1641 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1645 * @covers WANObjectCache::makeGlobalKey
1647 public function testMakeGlobalKey() {
1648 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1649 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1650 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1651 ->willReturn( 'special' );
1653 $wanCache = new WANObjectCache( [
1654 'cache' => $backend,
1655 'pool' => 'testcache-hash',
1656 'relayer' => new EventRelayerNull( [] )
1659 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1662 public static function statsKeyProvider() {
1664 [ 'domain:page:5', 'page' ],
1665 [ 'domain:main-key', 'main-key' ],
1666 [ 'domain:page:history', 'page' ],
1667 [ 'missingdomainkey', 'missingdomainkey' ]
1672 * @dataProvider statsKeyProvider
1673 * @covers WANObjectCache::determineKeyClass
1675 public function testStatsKeyClass( $key, $class ) {
1676 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1677 'cache' => new HashBagOStuff
,
1678 'pool' => 'testcache-hash',
1679 'relayer' => new EventRelayerNull( [] )
1682 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1686 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1687 private $timeOverride = 0;
1689 public function setTime( $time ) {
1690 $this->timeOverride
= $time;
1693 protected function getCurrentTime() {
1694 return $this->timeOverride ?
: parent
::getCurrentTime();
1698 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1699 private $timeOverride = 0;
1701 public function setTime( $time ) {
1702 $this->timeOverride
= $time;
1703 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1704 $this->cache
->setTime( $time );
1708 public function addTime( $time ) {
1709 $this->timeOverride +
= $time;
1710 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1711 $this->cache
->setTime( $this->timeOverride
);
1714 return $this->timeOverride
;
1717 protected function getCurrentTime() {
1718 return $this->timeOverride ?
: parent
::getCurrentTime();
1722 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1723 const CLOCK_SKEW
= 1;
1725 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1726 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1730 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1731 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1732 return ( ( $now - $asOf ) > $timeTillRefresh );