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" );
402 $cache = new PopularityRefreshingWANObjectCache( [
403 'cache' => new HashBagOStuff(),
407 $now = microtime( true ); // reference time
409 $key = wfRandomString();
410 $opts = [ 'hotTTR' => 900 ];
411 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
412 $this->assertEquals( $value, $v, "Value returned" );
413 $this->assertEquals( 1, $wasSet, "Value calculated" );
414 $cache->setTime( $now +
30 );
415 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
416 $this->assertEquals( 1, $wasSet, "Value cached" );
419 $key = wfRandomString();
420 $opts = [ 'hotTTR' => 10 ];
421 $cache->setTime( $now );
422 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
423 $this->assertEquals( $value, $v, "Value returned" );
424 $this->assertEquals( 1, $wasSet, "Value calculated" );
425 $cache->setTime( $now +
30 );
426 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
427 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
431 * @covers WANObjectCache::getWithSetCallback()
432 * @covers WANObjectCache::doGetWithSetCallback()
434 public function testGetWithSetCallback_invalidCallback() {
435 $this->setExpectedException( InvalidArgumentException
::class );
436 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
440 * @dataProvider getMultiWithSetCallback_provider
441 * @covers WANObjectCache::getMultiWithSetCallback
442 * @covers WANObjectCache::makeMultiKeys
443 * @covers WANObjectCache::getMulti
444 * @param array $extOpts
445 * @param bool $versioned
447 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
448 $cache = $this->cache
;
450 $keyA = wfRandomString();
451 $keyB = wfRandomString();
452 $keyC = wfRandomString();
453 $cKey1 = wfRandomString();
454 $cKey2 = wfRandomString();
459 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
460 &$wasSet, &$priorValue, &$priorAsOf
465 $ttl = 20; // override with another value
470 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
472 $v = $cache->getMultiWithSetCallback(
473 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
474 $this->assertEquals( $value, $v[$keyA], "Value returned" );
475 $this->assertEquals( 1, $wasSet, "Value regenerated" );
476 $this->assertFalse( $priorValue, "No prior value" );
477 $this->assertNull( $priorAsOf, "No prior value" );
480 $cache->get( $keyA, $curTTL );
481 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
482 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
486 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
487 $v = $cache->getMultiWithSetCallback(
488 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
489 $this->assertEquals( $value, $v[$keyB], "Value returned" );
490 $this->assertEquals( 1, $wasSet, "Value regenerated" );
491 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
492 $v = $cache->getMultiWithSetCallback(
493 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
494 $this->assertEquals( $value, $v[$keyB], "Value returned" );
495 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
496 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
498 $priorTime = microtime( true ); // reference time
499 $cache->setTime( $priorTime );
501 $cache->addTime( 1 );
504 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
505 $v = $cache->getMultiWithSetCallback(
506 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
508 $this->assertEquals( $value, $v[$keyB], "Value returned" );
509 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
510 $this->assertEquals( $value, $priorValue, "Has prior value" );
511 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
512 $t1 = $cache->getCheckKeyTime( $cKey1 );
513 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
514 $t2 = $cache->getCheckKeyTime( $cKey2 );
515 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
517 $priorTime = $cache->addTime( 0.01 );
520 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
521 $v = $cache->getMultiWithSetCallback(
522 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
524 $this->assertEquals( $value, $v[$keyC], "Value returned" );
525 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
526 $t1 = $cache->getCheckKeyTime( $cKey1 );
527 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
528 $t2 = $cache->getCheckKeyTime( $cKey2 );
529 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
532 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
534 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
536 $this->assertEquals( $value, $v, "Value returned" );
538 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
541 $key = wfRandomString();
542 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
543 $v = $cache->getMultiWithSetCallback(
544 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
545 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
546 $cache->delete( $key );
547 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
548 $v = $cache->getMultiWithSetCallback(
549 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
550 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
551 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
554 $ids = [ 1, 2, 3, 4, 5, 6 ];
555 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
556 return $wanCache->makeKey( 'test', $id );
558 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
559 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
564 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
567 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
568 array_values( $values ),
569 "Correct values in correct order"
572 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
573 array_keys( $values ),
574 "Correct keys in correct order"
576 $this->assertEquals( count( $ids ), $calls );
578 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
579 $this->assertEquals( count( $ids ), $calls, "Values cached" );
581 // Mock the BagOStuff to assure only one getMulti() call given process caching
582 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
583 ->setMethods( [ 'getMulti' ] )->getMock();
584 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
585 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
586 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
588 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
590 // Warm the process cache
591 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
593 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
594 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
596 // Use the process cache
598 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
599 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
603 public static function getMultiWithSetCallback_provider() {
606 [ [ 'version' => 1 ], true ]
611 * @dataProvider getMultiWithUnionSetCallback_provider
612 * @covers WANObjectCache::getMultiWithUnionSetCallback()
613 * @covers WANObjectCache::makeMultiKeys()
614 * @param array $extOpts
615 * @param bool $versioned
617 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
618 $cache = $this->cache
;
620 $keyA = wfRandomString();
621 $keyB = wfRandomString();
622 $keyC = wfRandomString();
623 $cKey1 = wfRandomString();
624 $cKey2 = wfRandomString();
627 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
628 &$wasSet, &$priorValue, &$priorAsOf
631 foreach ( $ids as $id ) {
633 $newValues[$id] = "@$id$";
634 $ttls[$id] = 20; // override with another value
641 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
643 $v = $cache->getMultiWithUnionSetCallback(
644 $keyedIds, 30, $genFunc, $extOpts );
645 $this->assertEquals( $value, $v[$keyA], "Value returned" );
646 $this->assertEquals( 1, $wasSet, "Value regenerated" );
649 $cache->get( $keyA, $curTTL );
650 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
651 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
655 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
656 $v = $cache->getMultiWithUnionSetCallback(
657 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
658 $this->assertEquals( $value, $v[$keyB], "Value returned" );
659 $this->assertEquals( 1, $wasSet, "Value regenerated" );
660 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
661 $v = $cache->getMultiWithUnionSetCallback(
662 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
663 $this->assertEquals( $value, $v[$keyB], "Value returned" );
664 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
665 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
667 $priorTime = microtime( true ); // reference time
668 $cache->setTime( $priorTime );
670 $cache->addTime( 1 );
673 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
674 $v = $cache->getMultiWithUnionSetCallback(
675 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
677 $this->assertEquals( $value, $v[$keyB], "Value returned" );
678 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
679 $t1 = $cache->getCheckKeyTime( $cKey1 );
680 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
681 $t2 = $cache->getCheckKeyTime( $cKey2 );
682 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
684 $priorTime = $cache->addTime( 0.01 );
687 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
688 $v = $cache->getMultiWithUnionSetCallback(
689 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
691 $this->assertEquals( $value, $v[$keyC], "Value returned" );
692 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
693 $t1 = $cache->getCheckKeyTime( $cKey1 );
694 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
695 $t2 = $cache->getCheckKeyTime( $cKey2 );
696 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
699 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
701 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
703 $this->assertEquals( $value, $v, "Value returned" );
705 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
708 $key = wfRandomString();
709 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
710 $v = $cache->getMultiWithUnionSetCallback(
711 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
712 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
713 $cache->delete( $key );
714 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
715 $v = $cache->getMultiWithUnionSetCallback(
716 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
717 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
718 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
721 $ids = [ 1, 2, 3, 4, 5, 6 ];
722 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
723 return $wanCache->makeKey( 'test', $id );
725 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
726 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
728 foreach ( $ids as $id ) {
730 $newValues[$id] = "val-{$id}";
735 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
738 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
739 array_values( $values ),
740 "Correct values in correct order"
743 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
744 array_keys( $values ),
745 "Correct keys in correct order"
747 $this->assertEquals( count( $ids ), $calls );
749 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
750 $this->assertEquals( count( $ids ), $calls, "Values cached" );
753 public static function getMultiWithUnionSetCallback_provider() {
756 [ [ 'version' => 1 ], true ]
761 * @covers WANObjectCache::getWithSetCallback()
762 * @covers WANObjectCache::doGetWithSetCallback()
764 public function testLockTSE() {
765 $cache = $this->cache
;
766 $key = wfRandomString();
767 $value = wfRandomString();
770 $func = function () use ( &$calls, $value, $cache, $key ) {
775 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
776 $this->assertEquals( $value, $ret );
777 $this->assertEquals( 1, $calls, 'Value was populated' );
779 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
780 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
782 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
783 $ret = $cache->getWithSetCallback( $key, 30, $func,
784 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
785 $this->assertEquals( $value, $ret, 'Old value used' );
786 $this->assertEquals( 1, $calls, 'Callback was not used' );
788 $cache->delete( $key );
789 $ret = $cache->getWithSetCallback( $key, 30, $func,
790 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
791 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
792 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
794 $ret = $cache->getWithSetCallback( $key, 30, $func,
795 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
796 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
797 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
801 * @covers WANObjectCache::getWithSetCallback()
802 * @covers WANObjectCache::doGetWithSetCallback()
803 * @covers WANObjectCache::set()
805 public function testLockTSESlow() {
806 $cache = $this->cache
;
807 $key = wfRandomString();
808 $value = wfRandomString();
811 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
813 $setOpts['since'] = microtime( true ) - 10;
814 // Immediately kill any mutex rather than waiting a second
815 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
819 // Value should be marked as stale due to snapshot lag
821 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
822 $this->assertEquals( $value, $ret );
823 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
824 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
825 $this->assertEquals( 1, $calls, 'Value was generated' );
827 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
828 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
829 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
830 $this->assertEquals( $value, $ret );
831 $this->assertEquals( 1, $calls, 'Callback was not used' );
835 * @covers WANObjectCache::getWithSetCallback()
836 * @covers WANObjectCache::doGetWithSetCallback()
838 public function testBusyValue() {
839 $cache = $this->cache
;
840 $key = wfRandomString();
841 $value = wfRandomString();
842 $busyValue = wfRandomString();
845 $func = function () use ( &$calls, $value, $cache, $key ) {
847 // Immediately kill any mutex rather than waiting a second
848 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
852 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
853 $this->assertEquals( $value, $ret );
854 $this->assertEquals( 1, $calls, 'Value was populated' );
856 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
857 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
859 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
860 $ret = $cache->getWithSetCallback( $key, 30, $func,
861 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
862 $this->assertEquals( $value, $ret, 'Callback used' );
863 $this->assertEquals( 2, $calls, 'Callback used' );
865 $ret = $cache->getWithSetCallback( $key, 30, $func,
866 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
867 $this->assertEquals( $value, $ret, 'Old value used' );
868 $this->assertEquals( 2, $calls, 'Callback was not used' );
870 $cache->delete( $key ); // no value at all anymore and still locked
871 $ret = $cache->getWithSetCallback( $key, 30, $func,
872 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
873 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
874 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
876 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
877 $ret = $cache->getWithSetCallback( $key, 30, $func,
878 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
879 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
880 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
882 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
883 $ret = $cache->getWithSetCallback( $key, 30, $func,
884 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
885 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
886 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
890 * @covers WANObjectCache::getMulti()
892 public function testGetMulti() {
893 $cache = $this->cache
;
895 $value1 = [ 'this' => 'is', 'a' => 'test' ];
896 $value2 = [ 'this' => 'is', 'another' => 'test' ];
898 $key1 = wfRandomString();
899 $key2 = wfRandomString();
900 $key3 = wfRandomString();
902 $cache->set( $key1, $value1, 5 );
903 $cache->set( $key2, $value2, 10 );
907 [ $key1 => $value1, $key2 => $value2 ],
908 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
909 'Result array populated'
912 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
913 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
914 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
916 $cKey1 = wfRandomString();
917 $cKey2 = wfRandomString();
919 $priorTime = microtime( true ); // reference time
920 $cache->setTime( $priorTime );
922 $cache->addTime( 1 );
926 [ $key1 => $value1, $key2 => $value2 ],
927 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
928 "Result array populated even with new check keys"
930 $t1 = $cache->getCheckKeyTime( $cKey1 );
931 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
932 $t2 = $cache->getCheckKeyTime( $cKey2 );
933 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
934 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
935 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
936 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
938 $cache->addTime( 1 );
942 [ $key1 => $value1, $key2 => $value2 ],
943 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
944 "Result array still populated even with new check keys"
946 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
947 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
948 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
952 * @covers WANObjectCache::getMulti()
953 * @covers WANObjectCache::processCheckKeys()
955 public function testGetMultiCheckKeys() {
956 $cache = $this->cache
;
958 $checkAll = wfRandomString();
959 $check1 = wfRandomString();
960 $check2 = wfRandomString();
961 $check3 = wfRandomString();
962 $value1 = wfRandomString();
963 $value2 = wfRandomString();
965 $cache->setTime( microtime( true ) );
967 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
968 // several seconds during the test to assert the behaviour.
969 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
970 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
973 $cache->addTime( 0.100 );
975 $cache->set( 'key1', $value1, 10 );
976 $cache->set( 'key2', $value2, 10 );
979 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
986 [ 'key1' => $value1, 'key2' => $value2 ],
990 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
991 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
992 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
993 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
995 $cache->addTime( 0.100 );
996 $cache->touchCheckKey( $check1 );
999 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1005 $this->assertEquals(
1006 [ 'key1' => $value1, 'key2' => $value2 ],
1008 'key1 expired by check1, but value still provided'
1010 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1011 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1013 $cache->touchCheckKey( $checkAll );
1016 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1022 $this->assertEquals(
1023 [ 'key1' => $value1, 'key2' => $value2 ],
1025 'All keys expired by checkAll, but value still provided'
1027 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1028 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1032 * @covers WANObjectCache::get()
1033 * @covers WANObjectCache::processCheckKeys()
1035 public function testCheckKeyInitHoldoff() {
1036 $cache = $this->cache
;
1038 for ( $i = 0; $i < 500; ++
$i ) {
1039 $key = wfRandomString();
1040 $checkKey = wfRandomString();
1042 $cache->get( $key, $curTTL, [ $checkKey ] );
1043 $cache->set( $key, 'val', 10 );
1045 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1047 $this->assertEquals( 'val', $v );
1048 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1051 for ( $i = 0; $i < 500; ++
$i ) {
1052 $key = wfRandomString();
1053 $checkKey = wfRandomString();
1055 $cache->set( $key, 'val', 10 );
1057 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1059 $this->assertEquals( 'val', $v );
1060 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1065 * @covers WANObjectCache::delete
1066 * @covers WANObjectCache::relayDelete
1067 * @covers WANObjectCache::relayPurge
1069 public function testDelete() {
1070 $key = wfRandomString();
1071 $value = wfRandomString();
1072 $this->cache
->set( $key, $value );
1075 $v = $this->cache
->get( $key, $curTTL );
1076 $this->assertEquals( $value, $v, "Key was created with value" );
1077 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1079 $this->cache
->delete( $key );
1082 $v = $this->cache
->get( $key, $curTTL );
1083 $this->assertFalse( $v, "Deleted key has false value" );
1084 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1086 $this->cache
->set( $key, $value . 'more' );
1087 $v = $this->cache
->get( $key, $curTTL );
1088 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1089 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1091 $this->cache
->set( $key, $value );
1092 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1095 $v = $this->cache
->get( $key, $curTTL );
1096 $this->assertFalse( $v, "Deleted key has false value" );
1097 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1099 $this->cache
->set( $key, $value );
1100 $v = $this->cache
->get( $key, $curTTL );
1101 $this->assertEquals( $value, $v, "Key was created with value" );
1102 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1106 * @dataProvider getWithSetCallback_versions_provider
1107 * @covers WANObjectCache::getWithSetCallback()
1108 * @covers WANObjectCache::doGetWithSetCallback()
1109 * @param array $extOpts
1110 * @param bool $versioned
1112 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1113 $cache = $this->cache
;
1115 $key = wfRandomString();
1116 $valueV1 = wfRandomString();
1117 $valueV2 = [ wfRandomString() ];
1120 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1126 $priorValue = false;
1128 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1129 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1130 $priorValue = $oldValue;
1131 $priorAsOf = $oldAsOf;
1134 return $valueV2; // new array format
1137 // Set the main key (version N if versioned)
1139 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1140 $this->assertEquals( $valueV1, $v, "Value returned" );
1141 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1142 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1143 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1144 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1147 // Set the key for version N+1 format
1148 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1150 // Start versioning now with the unversioned key still there
1151 $verOpts = [ 'version' => 1 ];
1154 // Value goes to secondary key since V1 already used $key
1156 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1157 $this->assertEquals( $valueV2, $v, "Value returned" );
1158 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1159 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1160 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1163 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1164 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1165 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1167 // Clear out the older or unversioned key
1168 $cache->delete( $key, 0 );
1170 // Set the key for next/first versioned format
1172 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1173 $this->assertEquals( $valueV2, $v, "Value returned" );
1174 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1176 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1177 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1178 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1181 public static function getWithSetCallback_versions_provider() {
1184 [ [ 'version' => 1 ], true ]
1189 * @covers WANObjectCache::useInterimHoldOffCaching
1190 * @covers WANObjectCache::getInterimValue
1192 public function testInterimHoldOffCaching() {
1193 $cache = $this->cache
;
1195 $value = 'CRL-40-940';
1197 $func = function () use ( &$wasCalled, $value ) {
1203 $cache->useInterimHoldOffCaching( true );
1205 $key = wfRandomString( 32 );
1206 $v = $cache->getWithSetCallback( $key, 60, $func );
1207 $v = $cache->getWithSetCallback( $key, 60, $func );
1208 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1209 $cache->delete( $key );
1210 $v = $cache->getWithSetCallback( $key, 60, $func );
1211 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1212 $v = $cache->getWithSetCallback( $key, 60, $func );
1213 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1214 // Lock up the mutex so interim cache is used
1215 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1216 $v = $cache->getWithSetCallback( $key, 60, $func );
1217 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1218 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
1220 $cache->useInterimHoldOffCaching( false );
1223 $key = wfRandomString( 32 );
1224 $v = $cache->getWithSetCallback( $key, 60, $func );
1225 $v = $cache->getWithSetCallback( $key, 60, $func );
1226 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1227 $cache->delete( $key );
1228 $v = $cache->getWithSetCallback( $key, 60, $func );
1229 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1230 $v = $cache->getWithSetCallback( $key, 60, $func );
1231 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1232 $v = $cache->getWithSetCallback( $key, 60, $func );
1233 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1234 // Lock up the mutex so interim cache is used
1235 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
1236 $v = $cache->getWithSetCallback( $key, 60, $func );
1237 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1241 * @covers WANObjectCache::touchCheckKey
1242 * @covers WANObjectCache::resetCheckKey
1243 * @covers WANObjectCache::getCheckKeyTime
1244 * @covers WANObjectCache::makePurgeValue
1245 * @covers WANObjectCache::parsePurgeValue
1247 public function testTouchKeys() {
1248 $cache = $this->cache
;
1249 $key = wfRandomString();
1251 $priorTime = microtime( true ); // reference time
1252 $cache->setTime( $priorTime );
1254 $newTime = $cache->addTime( 0.100 );
1255 $t0 = $cache->getCheckKeyTime( $key );
1256 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1258 $priorTime = $newTime;
1259 $newTime = $cache->addTime( 0.100 );
1260 $cache->touchCheckKey( $key );
1261 $t1 = $cache->getCheckKeyTime( $key );
1262 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1264 $t2 = $cache->getCheckKeyTime( $key );
1265 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1267 $cache->addTime( 0.100 );
1268 $cache->touchCheckKey( $key );
1269 $t3 = $cache->getCheckKeyTime( $key );
1270 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1272 $t4 = $cache->getCheckKeyTime( $key );
1273 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1275 $cache->addTime( 0.100 );
1276 $cache->resetCheckKey( $key );
1277 $t5 = $cache->getCheckKeyTime( $key );
1278 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1280 $t6 = $cache->getCheckKeyTime( $key );
1281 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1285 * @covers WANObjectCache::getMulti()
1287 public function testGetWithSeveralCheckKeys() {
1288 $key = wfRandomString();
1289 $tKey1 = wfRandomString();
1290 $tKey2 = wfRandomString();
1293 // Two check keys are newer (given hold-off) than $key, another is older
1294 $this->internalCache
->set(
1295 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1296 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1298 $this->internalCache
->set(
1299 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1300 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1302 $this->internalCache
->set(
1303 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1304 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1306 $this->cache
->set( $key, $value, 30 );
1309 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1310 $this->assertEquals( $value, $v, "Value matches" );
1311 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1312 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1316 * @covers WANObjectCache::reap()
1317 * @covers WANObjectCache::reapCheckKey()
1319 public function testReap() {
1320 $vKey1 = wfRandomString();
1321 $vKey2 = wfRandomString();
1322 $tKey1 = wfRandomString();
1323 $tKey2 = wfRandomString();
1326 $knownPurge = time() - 60;
1327 $goodTime = microtime( true ) - 5;
1328 $badTime = microtime( true ) - 300;
1330 $this->internalCache
->set(
1331 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1333 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1334 WANObjectCache
::FLD_VALUE
=> $value,
1335 WANObjectCache
::FLD_TTL
=> 3600,
1336 WANObjectCache
::FLD_TIME
=> $goodTime
1339 $this->internalCache
->set(
1340 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1342 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1343 WANObjectCache
::FLD_VALUE
=> $value,
1344 WANObjectCache
::FLD_TTL
=> 3600,
1345 WANObjectCache
::FLD_TIME
=> $badTime
1348 $this->internalCache
->set(
1349 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1350 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1352 $this->internalCache
->set(
1353 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1354 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1357 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1358 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1359 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1360 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1362 $this->assertFalse( $bad1 );
1363 $this->assertTrue( $bad2 );
1365 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1366 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1367 $this->assertFalse( $tBad1 );
1368 $this->assertTrue( $tBad2 );
1372 * @covers WANObjectCache::reap()
1374 public function testReap_fail() {
1375 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1376 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1377 $backend->expects( $this->once() )->method( 'get' )
1379 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1380 WANObjectCache
::FLD_VALUE
=> 'value',
1381 WANObjectCache
::FLD_TTL
=> 3600,
1382 WANObjectCache
::FLD_TIME
=> 300,
1384 $backend->expects( $this->once() )->method( 'changeTTL' )
1385 ->willReturn( false );
1387 $wanCache = new WANObjectCache( [
1388 'cache' => $backend,
1389 'pool' => 'testcache-hash',
1390 'relayer' => new EventRelayerNull( [] )
1394 $ret = $wanCache->reap( 'key', 360, $isStale );
1395 $this->assertTrue( $isStale, 'value was stale' );
1396 $this->assertFalse( $ret, 'changeTTL failed' );
1400 * @covers WANObjectCache::set()
1402 public function testSetWithLag() {
1405 $key = wfRandomString();
1406 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1407 $this->cache
->set( $key, $value, 30, $opts );
1408 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1410 $key = wfRandomString();
1411 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1412 $this->cache
->set( $key, $value, 30, $opts );
1413 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1415 $key = wfRandomString();
1416 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1417 $this->cache
->set( $key, $value, 30, $opts );
1418 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1422 * @covers WANObjectCache::set()
1424 public function testWritePending() {
1427 $key = wfRandomString();
1428 $opts = [ 'pending' => true ];
1429 $this->cache
->set( $key, $value, 30, $opts );
1430 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1433 public function testMcRouterSupport() {
1434 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1435 ->setMethods( [ 'set', 'delete' ] )->getMock();
1436 $localBag->expects( $this->never() )->method( 'set' );
1437 $localBag->expects( $this->never() )->method( 'delete' );
1438 $wanCache = new WANObjectCache( [
1439 'cache' => $localBag,
1440 'pool' => 'testcache-hash',
1441 'relayer' => new EventRelayerNull( [] )
1443 $valFunc = function () {
1447 // None of these should use broadcasting commands (e.g. SET, DELETE)
1448 $wanCache->get( 'x' );
1449 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1450 $wanCache->getMulti( [ 'x', 'y' ] );
1451 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1452 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1453 $wanCache->getCheckKeyTime( 'zzz' );
1454 $wanCache->reap( 'x', time() - 300 );
1455 $wanCache->reap( 'zzz', time() - 300 );
1459 * @dataProvider provideAdaptiveTTL
1460 * @covers WANObjectCache::adaptiveTTL()
1461 * @param float|int $ago
1462 * @param int $maxTTL
1463 * @param int $minTTL
1464 * @param float $factor
1465 * @param int $adaptiveTTL
1467 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1468 $mtime = $ago ?
time() - $ago : $ago;
1470 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1472 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1473 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1475 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1477 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1478 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1481 public static function provideAdaptiveTTL() {
1483 [ 3600, 900, 30, 0.2, 720 ],
1484 [ 3600, 500, 30, 0.2, 500 ],
1485 [ 3600, 86400, 800, 0.2, 800 ],
1486 [ false, 86400, 800, 0.2, 800 ],
1487 [ null, 86400, 800, 0.2, 800 ]
1492 * @covers WANObjectCache::__construct
1493 * @covers WANObjectCache::newEmpty
1495 public function testNewEmpty() {
1496 $this->assertInstanceOf(
1497 WANObjectCache
::class,
1498 WANObjectCache
::newEmpty()
1503 * @covers WANObjectCache::setLogger
1505 public function testSetLogger() {
1506 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1510 * @covers WANObjectCache::getQoS
1512 public function testGetQoS() {
1513 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1514 ->setMethods( [ 'getQoS' ] )->getMock();
1515 $backend->expects( $this->once() )->method( 'getQoS' )
1516 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1517 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1520 $wanCache::QOS_UNKNOWN
,
1521 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1526 * @covers WANObjectCache::makeKey
1528 public function testMakeKey() {
1529 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1530 ->setMethods( [ 'makeKey' ] )->getMock();
1531 $backend->expects( $this->once() )->method( 'makeKey' )
1532 ->willReturn( 'special' );
1534 $wanCache = new WANObjectCache( [
1535 'cache' => $backend,
1536 'pool' => 'testcache-hash',
1537 'relayer' => new EventRelayerNull( [] )
1540 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1544 * @covers WANObjectCache::makeGlobalKey
1546 public function testMakeGlobalKey() {
1547 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1548 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1549 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1550 ->willReturn( 'special' );
1552 $wanCache = new WANObjectCache( [
1553 'cache' => $backend,
1554 'pool' => 'testcache-hash',
1555 'relayer' => new EventRelayerNull( [] )
1558 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1561 public static function statsKeyProvider() {
1563 [ 'domain:page:5', 'page' ],
1564 [ 'domain:main-key', 'main-key' ],
1565 [ 'domain:page:history', 'page' ],
1566 [ 'missingdomainkey', 'missingdomainkey' ]
1571 * @dataProvider statsKeyProvider
1572 * @covers WANObjectCache::determineKeyClass
1574 public function testStatsKeyClass( $key, $class ) {
1575 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1576 'cache' => new HashBagOStuff
,
1577 'pool' => 'testcache-hash',
1578 'relayer' => new EventRelayerNull( [] )
1581 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1585 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1586 private $timeOverride = 0;
1588 public function setTime( $time ) {
1589 $this->timeOverride
= $time;
1592 protected function getCurrentTime() {
1593 return $this->timeOverride ?
: parent
::getCurrentTime();
1597 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1598 private $timeOverride = 0;
1600 public function setTime( $time ) {
1601 $this->timeOverride
= $time;
1602 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1603 $this->cache
->setTime( $time );
1607 public function addTime( $time ) {
1608 $this->timeOverride +
= $time;
1609 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1610 $this->cache
->setTime( $this->timeOverride
);
1613 return $this->timeOverride
;
1616 protected function getCurrentTime() {
1617 return $this->timeOverride ?
: parent
::getCurrentTime();
1621 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1622 const CLOCK_SKEW
= 1;
1624 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1625 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1629 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1630 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1631 return ( ( $now - $asOf ) > $timeTillRefresh );