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
{
20 /** @var TimeAdjustableWANObjectCache */
23 private $internalCache;
25 protected function setUp() {
28 $this->cache
= new TimeAdjustableWANObjectCache( [
29 'cache' => new TimeAdjustableHashBagOStuff(),
30 'pool' => 'testcache-hash',
31 'relayer' => new EventRelayerNull( [] )
34 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
35 /** @noinspection PhpUndefinedFieldInspection */
36 $this->internalCache
= $wanCache->cache
;
40 * @dataProvider provideSetAndGet
41 * @covers WANObjectCache::set()
42 * @covers WANObjectCache::get()
43 * @covers WANObjectCache::makeKey()
47 public function testSetAndGet( $value, $ttl ) {
50 $key = $this->cache
->makeKey( 'x', wfRandomString() );
52 $this->cache
->get( $key, $curTTL, [], $asOf );
53 $this->assertNull( $curTTL, "Current TTL is null" );
54 $this->assertNull( $asOf, "Current as-of-time is infinite" );
56 $t = microtime( true );
57 $this->cache
->set( $key, $value, $ttl );
59 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
60 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
61 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
63 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
64 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
66 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
67 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
70 public static function provideSetAndGet() {
77 [ (object)[ 'meow' ], 3 ],
85 * @covers WANObjectCache::get()
86 * @covers WANObjectCache::makeGlobalKey()
88 public function testGetNotExists() {
89 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
91 $value = $this->cache
->get( $key, $curTTL );
93 $this->assertFalse( $value, "Non-existing key has false value" );
94 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
98 * @covers WANObjectCache::set()
100 public function testSetOver() {
101 $key = wfRandomString();
102 for ( $i = 0; $i < 3; ++
$i ) {
103 $value = wfRandomString();
104 $this->cache
->set( $key, $value, 3 );
106 $this->assertEquals( $this->cache
->get( $key ), $value );
111 * @covers WANObjectCache::set()
113 public function testStaleSet() {
114 $key = wfRandomString();
115 $value = wfRandomString();
116 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
118 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
121 public function testProcessCache() {
123 $callback = function () use ( &$hit ) {
127 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
128 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
130 foreach ( $keys as $i => $key ) {
131 $this->cache
->getWithSetCallback(
132 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
134 $this->assertEquals( 3, $hit );
136 foreach ( $keys as $i => $key ) {
137 $this->cache
->getWithSetCallback(
138 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
140 $this->assertEquals( 3, $hit, "Values cached" );
142 foreach ( $keys as $i => $key ) {
143 $this->cache
->getWithSetCallback(
144 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
146 $this->assertEquals( 6, $hit );
148 foreach ( $keys as $i => $key ) {
149 $this->cache
->getWithSetCallback(
150 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
152 $this->assertEquals( 6, $hit, "New values cached" );
154 foreach ( $keys as $i => $key ) {
155 $this->cache
->delete( $key );
156 $this->cache
->getWithSetCallback(
157 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
159 $this->assertEquals( 9, $hit, "Values evicted" );
161 $key = reset( $keys );
162 // Get into cache (default process cache group)
163 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
164 $this->assertEquals( 10, $hit, "Value calculated" );
165 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
166 $this->assertEquals( 10, $hit, "Value cached" );
167 $outerCallback = function () use ( &$callback, $key ) {
168 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
172 // Outer key misses and refuses inner key process cache value
173 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
174 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
178 * @dataProvider getWithSetCallback_provider
179 * @covers WANObjectCache::getWithSetCallback()
180 * @covers WANObjectCache::doGetWithSetCallback()
181 * @param array $extOpts
182 * @param bool $versioned
184 public function testGetWithSetCallback( array $extOpts, $versioned ) {
185 $cache = $this->cache
;
187 $key = wfRandomString();
188 $value = wfRandomString();
189 $cKey1 = wfRandomString();
190 $cKey2 = wfRandomString();
195 $func = function ( $old, &$ttl, &$opts, $asOf )
196 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
201 $ttl = 20; // override with another value
206 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
207 $this->assertEquals( $value, $v, "Value returned" );
208 $this->assertEquals( 1, $wasSet, "Value regenerated" );
209 $this->assertFalse( $priorValue, "No prior value" );
210 $this->assertNull( $priorAsOf, "No prior value" );
213 $cache->get( $key, $curTTL );
214 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
215 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
218 $v = $cache->getWithSetCallback(
219 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
220 $this->assertEquals( $value, $v, "Value returned" );
221 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
223 $priorTime = microtime( true ); // reference time
224 $cache->setTime( $priorTime );
226 $cache->addTime( 1 );
229 $v = $cache->getWithSetCallback(
230 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
232 $this->assertEquals( $value, $v, "Value returned" );
233 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
234 $this->assertEquals( $value, $priorValue, "Has prior value" );
235 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
236 $t1 = $cache->getCheckKeyTime( $cKey1 );
237 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
238 $t2 = $cache->getCheckKeyTime( $cKey2 );
239 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
241 $priorTime = $cache->addTime( 0.01 );
244 $v = $cache->getWithSetCallback(
245 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
247 $this->assertEquals( $value, $v, "Value returned" );
248 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
249 $t1 = $cache->getCheckKeyTime( $cKey1 );
250 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
251 $t2 = $cache->getCheckKeyTime( $cKey2 );
252 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
255 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
257 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
259 $this->assertEquals( $value, $v, "Value returned" );
261 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
264 $key = wfRandomString();
265 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
266 $this->assertEquals( $value, $v, "Value returned" );
267 $cache->delete( $key );
268 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
269 $this->assertEquals( $value, $v, "Value still returned after deleted" );
270 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
272 $oldValReceived = -1;
273 $oldAsOfReceived = -1;
274 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
275 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
277 $oldValReceived = $oldVal;
278 $oldAsOfReceived = $oldAsOf;
280 return 'xxx' . $wasSet;
283 $now = microtime( true ); // reference time
286 $key = wfRandomString();
287 $v = $cache->getWithSetCallback(
288 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
289 $this->assertEquals( 'xxx1', $v, "Value returned" );
290 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
291 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
293 $cache->addTime( 40 );
294 $v = $cache->getWithSetCallback(
295 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
296 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
297 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
298 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
299 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
301 $cache->addTime( 260 );
302 $v = $cache->getWithSetCallback(
303 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
304 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
305 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
306 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
307 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
310 $key = wfRandomString();
311 $checkKey = $cache->makeKey( 'template', 'X' );
312 $cache->setTime( $now - $cache::HOLDOFF_TTL
- 1 );
313 $cache->touchCheckKey( $checkKey ); // init check key
314 $cache->setTime( $now );
315 $v = $cache->getWithSetCallback(
317 $cache::TTL_INDEFINITE
,
319 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
321 $this->assertEquals( 'xxx1', $v, "Value returned" );
322 $this->assertEquals( 1, $wasSet, "Value computed" );
323 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
324 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
326 $cache->addTime( $cache::TTL_HOUR
); // some time passes
327 $v = $cache->getWithSetCallback(
329 $cache::TTL_INDEFINITE
,
331 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
333 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
334 $this->assertEquals( 1, $wasSet, "Cached value returned" );
336 $cache->touchCheckKey( $checkKey ); // make key stale
337 $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
339 $v = $cache->getWithSetCallback(
341 $cache::TTL_INDEFINITE
,
343 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
345 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
346 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
348 // Change of refresh increase to unity as staleness approaches graceTTL
350 $cache->addTime( $cache::TTL_WEEK
); // 8 days of being stale
351 $v = $cache->getWithSetCallback(
353 $cache::TTL_INDEFINITE
,
355 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
357 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
358 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
359 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
360 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
363 public static function getWithSetCallback_provider() {
366 [ [ 'version' => 1 ], true ]
370 public function testPreemtiveRefresh() {
373 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
379 $cache = new NearExpiringWANObjectCache( [
380 'cache' => new HashBagOStuff(),
385 $key = wfRandomString();
386 $opts = [ 'lowTTL' => 30 ];
387 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
388 $this->assertEquals( $value, $v, "Value returned" );
389 $this->assertEquals( 1, $wasSet, "Value calculated" );
390 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
391 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
394 $key = wfRandomString();
395 $opts = [ 'lowTTL' => 1 ];
396 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
397 $this->assertEquals( $value, $v, "Value returned" );
398 $this->assertEquals( 1, $wasSet, "Value calculated" );
399 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
400 $this->assertEquals( 1, $wasSet, "Value cached" );
403 $asyncHandler = function ( $callback ) use ( &$asycList ) {
404 $asycList[] = $callback;
406 $cache = new NearExpiringWANObjectCache( [
407 'cache' => new TimeAdjustableHashBagOStuff(),
409 'asyncHandler' => $asyncHandler
412 $now = microtime( true ); // reference time
413 $cache->setTime( $now );
416 $key = wfRandomString();
417 $checkKey = wfRandomString();
418 $opts = [ 'lowTTL' => 100 ];
419 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
420 $this->assertEquals( $value, $v, "Value returned" );
421 $this->assertEquals( 1, $wasSet, "Value calculated" );
422 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
423 $this->assertEquals( 1, $wasSet, "Cached value used" );
424 $this->assertEquals( $v, $value, "Value cached" );
426 $cache->setTime( $now +
250 );
427 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
428 $this->assertEquals( $value, $v, "Value returned" );
429 $this->assertEquals( 1, $wasSet, "Stale value used" );
430 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
431 $value = 'NewCatsInTown'; // change callback return value
432 $asycList[0](); // run the refresh callback
434 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
435 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
436 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
437 $this->assertEquals( $value, $v, "New value stored" );
439 $cache = new PopularityRefreshingWANObjectCache( [
440 'cache' => new TimeAdjustableHashBagOStuff(),
444 $cache->setTime( $now );
447 $key = wfRandomString();
448 $opts = [ 'hotTTR' => 900 ];
449 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
450 $this->assertEquals( $value, $v, "Value returned" );
451 $this->assertEquals( 1, $wasSet, "Value calculated" );
452 $cache->setTime( $now +
30 );
453 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
454 $this->assertEquals( 1, $wasSet, "Value cached" );
457 $key = wfRandomString();
458 $opts = [ 'hotTTR' => 10 ];
459 $cache->setTime( $now );
460 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
461 $this->assertEquals( $value, $v, "Value returned" );
462 $this->assertEquals( 1, $wasSet, "Value calculated" );
463 $cache->setTime( $now +
30 );
464 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
465 $this->assertEquals( $value, $v, "Value returned" );
466 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
470 * @covers WANObjectCache::getWithSetCallback()
471 * @covers WANObjectCache::doGetWithSetCallback()
473 public function testGetWithSetCallback_invalidCallback() {
474 $this->setExpectedException( InvalidArgumentException
::class );
475 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
479 * @dataProvider getMultiWithSetCallback_provider
480 * @covers WANObjectCache::getMultiWithSetCallback
481 * @covers WANObjectCache::makeMultiKeys
482 * @covers WANObjectCache::getMulti
483 * @param array $extOpts
484 * @param bool $versioned
486 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
487 $cache = $this->cache
;
489 $keyA = wfRandomString();
490 $keyB = wfRandomString();
491 $keyC = wfRandomString();
492 $cKey1 = wfRandomString();
493 $cKey2 = wfRandomString();
498 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
499 &$wasSet, &$priorValue, &$priorAsOf
504 $ttl = 20; // override with another value
509 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
511 $v = $cache->getMultiWithSetCallback(
512 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
513 $this->assertEquals( $value, $v[$keyA], "Value returned" );
514 $this->assertEquals( 1, $wasSet, "Value regenerated" );
515 $this->assertFalse( $priorValue, "No prior value" );
516 $this->assertNull( $priorAsOf, "No prior value" );
519 $cache->get( $keyA, $curTTL );
520 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
521 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
525 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
526 $v = $cache->getMultiWithSetCallback(
527 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
528 $this->assertEquals( $value, $v[$keyB], "Value returned" );
529 $this->assertEquals( 1, $wasSet, "Value regenerated" );
530 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
531 $v = $cache->getMultiWithSetCallback(
532 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
533 $this->assertEquals( $value, $v[$keyB], "Value returned" );
534 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
535 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
537 $priorTime = microtime( true ); // reference time
538 $cache->setTime( $priorTime );
540 $cache->addTime( 1 );
543 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
544 $v = $cache->getMultiWithSetCallback(
545 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
547 $this->assertEquals( $value, $v[$keyB], "Value returned" );
548 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
549 $this->assertEquals( $value, $priorValue, "Has prior value" );
550 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
551 $t1 = $cache->getCheckKeyTime( $cKey1 );
552 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
553 $t2 = $cache->getCheckKeyTime( $cKey2 );
554 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
556 $priorTime = $cache->addTime( 0.01 );
559 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
560 $v = $cache->getMultiWithSetCallback(
561 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
563 $this->assertEquals( $value, $v[$keyC], "Value returned" );
564 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
565 $t1 = $cache->getCheckKeyTime( $cKey1 );
566 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
567 $t2 = $cache->getCheckKeyTime( $cKey2 );
568 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
571 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
573 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
575 $this->assertEquals( $value, $v, "Value returned" );
577 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
580 $key = wfRandomString();
581 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
582 $v = $cache->getMultiWithSetCallback(
583 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
584 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
585 $cache->delete( $key );
586 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
587 $v = $cache->getMultiWithSetCallback(
588 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
589 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
590 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
593 $ids = [ 1, 2, 3, 4, 5, 6 ];
594 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
595 return $wanCache->makeKey( 'test', $id );
597 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
598 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
603 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
606 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
607 array_values( $values ),
608 "Correct values in correct order"
611 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
612 array_keys( $values ),
613 "Correct keys in correct order"
615 $this->assertEquals( count( $ids ), $calls );
617 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
618 $this->assertEquals( count( $ids ), $calls, "Values cached" );
620 // Mock the BagOStuff to assure only one getMulti() call given process caching
621 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
622 ->setMethods( [ 'getMulti' ] )->getMock();
623 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
624 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
625 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
627 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
629 // Warm the process cache
630 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
632 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
633 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
635 // Use the process cache
637 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
638 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
642 public static function getMultiWithSetCallback_provider() {
645 [ [ 'version' => 1 ], true ]
650 * @dataProvider getMultiWithUnionSetCallback_provider
651 * @covers WANObjectCache::getMultiWithUnionSetCallback()
652 * @covers WANObjectCache::makeMultiKeys()
653 * @param array $extOpts
654 * @param bool $versioned
656 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
657 $cache = $this->cache
;
659 $keyA = wfRandomString();
660 $keyB = wfRandomString();
661 $keyC = wfRandomString();
662 $cKey1 = wfRandomString();
663 $cKey2 = wfRandomString();
666 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
667 &$wasSet, &$priorValue, &$priorAsOf
670 foreach ( $ids as $id ) {
672 $newValues[$id] = "@$id$";
673 $ttls[$id] = 20; // override with another value
680 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
682 $v = $cache->getMultiWithUnionSetCallback(
683 $keyedIds, 30, $genFunc, $extOpts );
684 $this->assertEquals( $value, $v[$keyA], "Value returned" );
685 $this->assertEquals( 1, $wasSet, "Value regenerated" );
688 $cache->get( $keyA, $curTTL );
689 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
690 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
694 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
695 $v = $cache->getMultiWithUnionSetCallback(
696 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
697 $this->assertEquals( $value, $v[$keyB], "Value returned" );
698 $this->assertEquals( 1, $wasSet, "Value regenerated" );
699 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
700 $v = $cache->getMultiWithUnionSetCallback(
701 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
702 $this->assertEquals( $value, $v[$keyB], "Value returned" );
703 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
704 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
706 $priorTime = microtime( true ); // reference time
707 $cache->setTime( $priorTime );
709 $cache->addTime( 1 );
712 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
713 $v = $cache->getMultiWithUnionSetCallback(
714 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
716 $this->assertEquals( $value, $v[$keyB], "Value returned" );
717 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
718 $t1 = $cache->getCheckKeyTime( $cKey1 );
719 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
720 $t2 = $cache->getCheckKeyTime( $cKey2 );
721 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
723 $priorTime = $cache->addTime( 0.01 );
726 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
727 $v = $cache->getMultiWithUnionSetCallback(
728 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
730 $this->assertEquals( $value, $v[$keyC], "Value returned" );
731 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
732 $t1 = $cache->getCheckKeyTime( $cKey1 );
733 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
734 $t2 = $cache->getCheckKeyTime( $cKey2 );
735 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
738 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
740 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
742 $this->assertEquals( $value, $v, "Value returned" );
744 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
747 $key = wfRandomString();
748 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
749 $v = $cache->getMultiWithUnionSetCallback(
750 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
751 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
752 $cache->delete( $key );
753 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
754 $v = $cache->getMultiWithUnionSetCallback(
755 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
756 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
757 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
760 $ids = [ 1, 2, 3, 4, 5, 6 ];
761 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
762 return $wanCache->makeKey( 'test', $id );
764 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
765 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
767 foreach ( $ids as $id ) {
769 $newValues[$id] = "val-{$id}";
774 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
777 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
778 array_values( $values ),
779 "Correct values in correct order"
782 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
783 array_keys( $values ),
784 "Correct keys in correct order"
786 $this->assertEquals( count( $ids ), $calls );
788 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
789 $this->assertEquals( count( $ids ), $calls, "Values cached" );
792 public static function getMultiWithUnionSetCallback_provider() {
795 [ [ 'version' => 1 ], true ]
800 * @covers WANObjectCache::getWithSetCallback()
801 * @covers WANObjectCache::doGetWithSetCallback()
803 public function testLockTSE() {
804 $cache = $this->cache
;
805 $key = wfRandomString();
806 $value = wfRandomString();
809 $func = function () use ( &$calls, $value, $cache, $key ) {
814 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
815 $this->assertEquals( $value, $ret );
816 $this->assertEquals( 1, $calls, 'Value was populated' );
818 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
819 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
821 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
822 $ret = $cache->getWithSetCallback( $key, 30, $func,
823 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
824 $this->assertEquals( $value, $ret, 'Old value used' );
825 $this->assertEquals( 1, $calls, 'Callback was not used' );
827 $cache->delete( $key );
828 $ret = $cache->getWithSetCallback( $key, 30, $func,
829 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
830 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
831 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
833 $ret = $cache->getWithSetCallback( $key, 30, $func,
834 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
835 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
836 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
840 * @covers WANObjectCache::getWithSetCallback()
841 * @covers WANObjectCache::doGetWithSetCallback()
842 * @covers WANObjectCache::set()
844 public function testLockTSESlow() {
845 $cache = $this->cache
;
846 $key = wfRandomString();
847 $value = wfRandomString();
850 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
852 $setOpts['since'] = microtime( true ) - 10;
853 // Immediately kill any mutex rather than waiting a second
854 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
858 // Value should be marked as stale due to snapshot lag
860 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
861 $this->assertEquals( $value, $ret );
862 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
863 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
864 $this->assertEquals( 1, $calls, 'Value was generated' );
866 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
867 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
868 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
869 $this->assertEquals( $value, $ret );
870 $this->assertEquals( 1, $calls, 'Callback was not used' );
874 * @covers WANObjectCache::getWithSetCallback()
875 * @covers WANObjectCache::doGetWithSetCallback()
877 public function testBusyValue() {
878 $cache = $this->cache
;
879 $key = wfRandomString();
880 $value = wfRandomString();
881 $busyValue = wfRandomString();
884 $func = function () use ( &$calls, $value, $cache, $key ) {
886 // Immediately kill any mutex rather than waiting a second
887 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
891 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
892 $this->assertEquals( $value, $ret );
893 $this->assertEquals( 1, $calls, 'Value was populated' );
895 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
896 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
898 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
899 $ret = $cache->getWithSetCallback( $key, 30, $func,
900 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
901 $this->assertEquals( $value, $ret, 'Callback used' );
902 $this->assertEquals( 2, $calls, 'Callback used' );
904 $ret = $cache->getWithSetCallback( $key, 30, $func,
905 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
906 $this->assertEquals( $value, $ret, 'Old value used' );
907 $this->assertEquals( 2, $calls, 'Callback was not used' );
909 $cache->delete( $key ); // no value at all anymore and still locked
910 $ret = $cache->getWithSetCallback( $key, 30, $func,
911 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
912 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
913 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
915 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
916 $ret = $cache->getWithSetCallback( $key, 30, $func,
917 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
918 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
919 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
921 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
922 $ret = $cache->getWithSetCallback( $key, 30, $func,
923 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
924 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
925 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
929 * @covers WANObjectCache::getMulti()
931 public function testGetMulti() {
932 $cache = $this->cache
;
934 $value1 = [ 'this' => 'is', 'a' => 'test' ];
935 $value2 = [ 'this' => 'is', 'another' => 'test' ];
937 $key1 = wfRandomString();
938 $key2 = wfRandomString();
939 $key3 = wfRandomString();
941 $cache->set( $key1, $value1, 5 );
942 $cache->set( $key2, $value2, 10 );
946 [ $key1 => $value1, $key2 => $value2 ],
947 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
948 'Result array populated'
951 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
952 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
953 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
955 $cKey1 = wfRandomString();
956 $cKey2 = wfRandomString();
958 $priorTime = microtime( true ); // reference time
959 $cache->setTime( $priorTime );
961 $cache->addTime( 1 );
965 [ $key1 => $value1, $key2 => $value2 ],
966 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
967 "Result array populated even with new check keys"
969 $t1 = $cache->getCheckKeyTime( $cKey1 );
970 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
971 $t2 = $cache->getCheckKeyTime( $cKey2 );
972 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
973 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
974 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
975 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
977 $cache->addTime( 1 );
981 [ $key1 => $value1, $key2 => $value2 ],
982 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
983 "Result array still populated even with new check keys"
985 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
986 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
987 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
991 * @covers WANObjectCache::getMulti()
992 * @covers WANObjectCache::processCheckKeys()
994 public function testGetMultiCheckKeys() {
995 $cache = $this->cache
;
997 $checkAll = wfRandomString();
998 $check1 = wfRandomString();
999 $check2 = wfRandomString();
1000 $check3 = wfRandomString();
1001 $value1 = wfRandomString();
1002 $value2 = wfRandomString();
1004 $cache->setTime( microtime( true ) );
1006 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1007 // several seconds during the test to assert the behaviour.
1008 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1009 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
1012 $cache->addTime( 0.100 );
1014 $cache->set( 'key1', $value1, 10 );
1015 $cache->set( 'key2', $value2, 10 );
1018 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1024 $this->assertEquals(
1025 [ 'key1' => $value1, 'key2' => $value2 ],
1029 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1030 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1031 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1032 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1034 $cache->addTime( 0.100 );
1035 $cache->touchCheckKey( $check1 );
1038 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1044 $this->assertEquals(
1045 [ 'key1' => $value1, 'key2' => $value2 ],
1047 'key1 expired by check1, but value still provided'
1049 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1050 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1052 $cache->touchCheckKey( $checkAll );
1055 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1061 $this->assertEquals(
1062 [ 'key1' => $value1, 'key2' => $value2 ],
1064 'All keys expired by checkAll, but value still provided'
1066 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1067 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1071 * @covers WANObjectCache::get()
1072 * @covers WANObjectCache::processCheckKeys()
1074 public function testCheckKeyInitHoldoff() {
1075 $cache = $this->cache
;
1077 for ( $i = 0; $i < 500; ++
$i ) {
1078 $key = wfRandomString();
1079 $checkKey = wfRandomString();
1081 $cache->get( $key, $curTTL, [ $checkKey ] );
1082 $cache->set( $key, 'val', 10 );
1084 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1086 $this->assertEquals( 'val', $v );
1087 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1090 for ( $i = 0; $i < 500; ++
$i ) {
1091 $key = wfRandomString();
1092 $checkKey = wfRandomString();
1094 $cache->set( $key, 'val', 10 );
1096 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1098 $this->assertEquals( 'val', $v );
1099 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1104 * @covers WANObjectCache::delete
1105 * @covers WANObjectCache::relayDelete
1106 * @covers WANObjectCache::relayPurge
1108 public function testDelete() {
1109 $key = wfRandomString();
1110 $value = wfRandomString();
1111 $this->cache
->set( $key, $value );
1114 $v = $this->cache
->get( $key, $curTTL );
1115 $this->assertEquals( $value, $v, "Key was created with value" );
1116 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1118 $this->cache
->delete( $key );
1121 $v = $this->cache
->get( $key, $curTTL );
1122 $this->assertFalse( $v, "Deleted key has false value" );
1123 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1125 $this->cache
->set( $key, $value . 'more' );
1126 $v = $this->cache
->get( $key, $curTTL );
1127 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1128 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1130 $this->cache
->set( $key, $value );
1131 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1134 $v = $this->cache
->get( $key, $curTTL );
1135 $this->assertFalse( $v, "Deleted key has false value" );
1136 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1138 $this->cache
->set( $key, $value );
1139 $v = $this->cache
->get( $key, $curTTL );
1140 $this->assertEquals( $value, $v, "Key was created with value" );
1141 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1145 * @dataProvider getWithSetCallback_versions_provider
1146 * @covers WANObjectCache::getWithSetCallback()
1147 * @covers WANObjectCache::doGetWithSetCallback()
1148 * @param array $extOpts
1149 * @param bool $versioned
1151 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1152 $cache = $this->cache
;
1154 $key = wfRandomString();
1155 $valueV1 = wfRandomString();
1156 $valueV2 = [ wfRandomString() ];
1159 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1165 $priorValue = false;
1167 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1168 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1169 $priorValue = $oldValue;
1170 $priorAsOf = $oldAsOf;
1173 return $valueV2; // new array format
1176 // Set the main key (version N if versioned)
1178 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1179 $this->assertEquals( $valueV1, $v, "Value returned" );
1180 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1181 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1182 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1183 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1186 // Set the key for version N+1 format
1187 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1189 // Start versioning now with the unversioned key still there
1190 $verOpts = [ 'version' => 1 ];
1193 // Value goes to secondary key since V1 already used $key
1195 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1196 $this->assertEquals( $valueV2, $v, "Value returned" );
1197 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1198 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1199 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1202 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1203 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1204 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1206 // Clear out the older or unversioned key
1207 $cache->delete( $key, 0 );
1209 // Set the key for next/first versioned format
1211 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1212 $this->assertEquals( $valueV2, $v, "Value returned" );
1213 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1215 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1216 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1217 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1220 public static function getWithSetCallback_versions_provider() {
1223 [ [ 'version' => 1 ], true ]
1228 * @covers WANObjectCache::useInterimHoldOffCaching
1229 * @covers WANObjectCache::getInterimValue
1231 public function testInterimHoldOffCaching() {
1232 $cache = $this->cache
;
1234 $value = 'CRL-40-940';
1236 $func = function () use ( &$wasCalled, $value ) {
1242 $cache->useInterimHoldOffCaching( true );
1244 $key = wfRandomString( 32 );
1245 $v = $cache->getWithSetCallback( $key, 60, $func );
1246 $v = $cache->getWithSetCallback( $key, 60, $func );
1247 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1248 $cache->delete( $key );
1249 $v = $cache->getWithSetCallback( $key, 60, $func );
1250 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1251 $v = $cache->getWithSetCallback( $key, 60, $func );
1252 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1253 // Lock up the mutex so interim cache is used
1254 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1255 $v = $cache->getWithSetCallback( $key, 60, $func );
1256 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1257 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1259 $cache->useInterimHoldOffCaching( false );
1262 $key = wfRandomString( 32 );
1263 $v = $cache->getWithSetCallback( $key, 60, $func );
1264 $v = $cache->getWithSetCallback( $key, 60, $func );
1265 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1266 $cache->delete( $key );
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1269 $v = $cache->getWithSetCallback( $key, 60, $func );
1270 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1271 $v = $cache->getWithSetCallback( $key, 60, $func );
1272 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1273 // Lock up the mutex so interim cache is used
1274 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1275 $v = $cache->getWithSetCallback( $key, 60, $func );
1276 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1280 * @covers WANObjectCache::touchCheckKey
1281 * @covers WANObjectCache::resetCheckKey
1282 * @covers WANObjectCache::getCheckKeyTime
1283 * @covers WANObjectCache::makePurgeValue
1284 * @covers WANObjectCache::parsePurgeValue
1286 public function testTouchKeys() {
1287 $cache = $this->cache
;
1288 $key = wfRandomString();
1290 $priorTime = microtime( true ); // reference time
1291 $cache->setTime( $priorTime );
1293 $newTime = $cache->addTime( 0.100 );
1294 $t0 = $cache->getCheckKeyTime( $key );
1295 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1297 $priorTime = $newTime;
1298 $newTime = $cache->addTime( 0.100 );
1299 $cache->touchCheckKey( $key );
1300 $t1 = $cache->getCheckKeyTime( $key );
1301 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1303 $t2 = $cache->getCheckKeyTime( $key );
1304 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1306 $cache->addTime( 0.100 );
1307 $cache->touchCheckKey( $key );
1308 $t3 = $cache->getCheckKeyTime( $key );
1309 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1311 $t4 = $cache->getCheckKeyTime( $key );
1312 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1314 $cache->addTime( 0.100 );
1315 $cache->resetCheckKey( $key );
1316 $t5 = $cache->getCheckKeyTime( $key );
1317 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1319 $t6 = $cache->getCheckKeyTime( $key );
1320 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1324 * @covers WANObjectCache::getMulti()
1326 public function testGetWithSeveralCheckKeys() {
1327 $key = wfRandomString();
1328 $tKey1 = wfRandomString();
1329 $tKey2 = wfRandomString();
1332 // Two check keys are newer (given hold-off) than $key, another is older
1333 $this->internalCache
->set(
1334 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1335 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1337 $this->internalCache
->set(
1338 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1339 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1341 $this->internalCache
->set(
1342 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1343 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1345 $this->cache
->set( $key, $value, 30 );
1348 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1349 $this->assertEquals( $value, $v, "Value matches" );
1350 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1351 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1355 * @covers WANObjectCache::reap()
1356 * @covers WANObjectCache::reapCheckKey()
1358 public function testReap() {
1359 $vKey1 = wfRandomString();
1360 $vKey2 = wfRandomString();
1361 $tKey1 = wfRandomString();
1362 $tKey2 = wfRandomString();
1365 $knownPurge = time() - 60;
1366 $goodTime = microtime( true ) - 5;
1367 $badTime = microtime( true ) - 300;
1369 $this->internalCache
->set(
1370 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1372 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1373 WANObjectCache
::FLD_VALUE
=> $value,
1374 WANObjectCache
::FLD_TTL
=> 3600,
1375 WANObjectCache
::FLD_TIME
=> $goodTime
1378 $this->internalCache
->set(
1379 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1381 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1382 WANObjectCache
::FLD_VALUE
=> $value,
1383 WANObjectCache
::FLD_TTL
=> 3600,
1384 WANObjectCache
::FLD_TIME
=> $badTime
1387 $this->internalCache
->set(
1388 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1389 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1391 $this->internalCache
->set(
1392 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1393 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1396 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1397 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1398 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1399 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1401 $this->assertFalse( $bad1 );
1402 $this->assertTrue( $bad2 );
1404 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1405 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1406 $this->assertFalse( $tBad1 );
1407 $this->assertTrue( $tBad2 );
1411 * @covers WANObjectCache::reap()
1413 public function testReap_fail() {
1414 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1415 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1416 $backend->expects( $this->once() )->method( 'get' )
1418 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1419 WANObjectCache
::FLD_VALUE
=> 'value',
1420 WANObjectCache
::FLD_TTL
=> 3600,
1421 WANObjectCache
::FLD_TIME
=> 300,
1423 $backend->expects( $this->once() )->method( 'changeTTL' )
1424 ->willReturn( false );
1426 $wanCache = new WANObjectCache( [
1427 'cache' => $backend,
1428 'pool' => 'testcache-hash',
1429 'relayer' => new EventRelayerNull( [] )
1433 $ret = $wanCache->reap( 'key', 360, $isStale );
1434 $this->assertTrue( $isStale, 'value was stale' );
1435 $this->assertFalse( $ret, 'changeTTL failed' );
1439 * @covers WANObjectCache::set()
1441 public function testSetWithLag() {
1444 $key = wfRandomString();
1445 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1446 $this->cache
->set( $key, $value, 30, $opts );
1447 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1449 $key = wfRandomString();
1450 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1451 $this->cache
->set( $key, $value, 30, $opts );
1452 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1454 $key = wfRandomString();
1455 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1456 $this->cache
->set( $key, $value, 30, $opts );
1457 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1461 * @covers WANObjectCache::set()
1463 public function testWritePending() {
1466 $key = wfRandomString();
1467 $opts = [ 'pending' => true ];
1468 $this->cache
->set( $key, $value, 30, $opts );
1469 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1472 public function testMcRouterSupport() {
1473 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1474 ->setMethods( [ 'set', 'delete' ] )->getMock();
1475 $localBag->expects( $this->never() )->method( 'set' );
1476 $localBag->expects( $this->never() )->method( 'delete' );
1477 $wanCache = new WANObjectCache( [
1478 'cache' => $localBag,
1479 'pool' => 'testcache-hash',
1480 'relayer' => new EventRelayerNull( [] )
1482 $valFunc = function () {
1486 // None of these should use broadcasting commands (e.g. SET, DELETE)
1487 $wanCache->get( 'x' );
1488 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1489 $wanCache->getMulti( [ 'x', 'y' ] );
1490 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1491 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1492 $wanCache->getCheckKeyTime( 'zzz' );
1493 $wanCache->reap( 'x', time() - 300 );
1494 $wanCache->reap( 'zzz', time() - 300 );
1498 * @dataProvider provideAdaptiveTTL
1499 * @covers WANObjectCache::adaptiveTTL()
1500 * @param float|int $ago
1501 * @param int $maxTTL
1502 * @param int $minTTL
1503 * @param float $factor
1504 * @param int $adaptiveTTL
1506 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1507 $mtime = $ago ?
time() - $ago : $ago;
1509 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1511 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1512 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1514 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1516 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1517 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1520 public static function provideAdaptiveTTL() {
1522 [ 3600, 900, 30, 0.2, 720 ],
1523 [ 3600, 500, 30, 0.2, 500 ],
1524 [ 3600, 86400, 800, 0.2, 800 ],
1525 [ false, 86400, 800, 0.2, 800 ],
1526 [ null, 86400, 800, 0.2, 800 ]
1531 * @covers WANObjectCache::__construct
1532 * @covers WANObjectCache::newEmpty
1534 public function testNewEmpty() {
1535 $this->assertInstanceOf(
1536 WANObjectCache
::class,
1537 WANObjectCache
::newEmpty()
1542 * @covers WANObjectCache::setLogger
1544 public function testSetLogger() {
1545 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1549 * @covers WANObjectCache::getQoS
1551 public function testGetQoS() {
1552 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1553 ->setMethods( [ 'getQoS' ] )->getMock();
1554 $backend->expects( $this->once() )->method( 'getQoS' )
1555 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1556 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1559 $wanCache::QOS_UNKNOWN
,
1560 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1565 * @covers WANObjectCache::makeKey
1567 public function testMakeKey() {
1568 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1569 ->setMethods( [ 'makeKey' ] )->getMock();
1570 $backend->expects( $this->once() )->method( 'makeKey' )
1571 ->willReturn( 'special' );
1573 $wanCache = new WANObjectCache( [
1574 'cache' => $backend,
1575 'pool' => 'testcache-hash',
1576 'relayer' => new EventRelayerNull( [] )
1579 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1583 * @covers WANObjectCache::makeGlobalKey
1585 public function testMakeGlobalKey() {
1586 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1587 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1588 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1589 ->willReturn( 'special' );
1591 $wanCache = new WANObjectCache( [
1592 'cache' => $backend,
1593 'pool' => 'testcache-hash',
1594 'relayer' => new EventRelayerNull( [] )
1597 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1600 public static function statsKeyProvider() {
1602 [ 'domain:page:5', 'page' ],
1603 [ 'domain:main-key', 'main-key' ],
1604 [ 'domain:page:history', 'page' ],
1605 [ 'missingdomainkey', 'missingdomainkey' ]
1610 * @dataProvider statsKeyProvider
1611 * @covers WANObjectCache::determineKeyClass
1613 public function testStatsKeyClass( $key, $class ) {
1614 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1615 'cache' => new HashBagOStuff
,
1616 'pool' => 'testcache-hash',
1617 'relayer' => new EventRelayerNull( [] )
1620 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1624 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1625 private $timeOverride = 0;
1627 public function setTime( $time ) {
1628 $this->timeOverride
= $time;
1631 protected function getCurrentTime() {
1632 return $this->timeOverride ?
: parent
::getCurrentTime();
1636 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1637 private $timeOverride = 0;
1639 public function setTime( $time ) {
1640 $this->timeOverride
= $time;
1641 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1642 $this->cache
->setTime( $time );
1646 public function addTime( $time ) {
1647 $this->timeOverride +
= $time;
1648 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1649 $this->cache
->setTime( $this->timeOverride
);
1652 return $this->timeOverride
;
1655 protected function getCurrentTime() {
1656 return $this->timeOverride ?
: parent
::getCurrentTime();
1660 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1661 const CLOCK_SKEW
= 1;
1663 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1664 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1668 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1669 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1670 return ( ( $now - $asOf ) > $timeTillRefresh );