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 WANObjectCache */
23 private $internalCache;
25 protected function setUp() {
28 $this->cache
= new WANObjectCache( [
29 'cache' => new HashBagOStuff(),
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( $key, 30, $func, [
222 $this->assertEquals( $value, $v, "Value returned" );
223 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
225 $priorTime = microtime( true );
228 $v = $cache->getWithSetCallback(
229 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
231 $this->assertEquals( $value, $v, "Value returned" );
232 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
233 $this->assertEquals( $value, $priorValue, "Has prior value" );
234 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
235 $t1 = $cache->getCheckKeyTime( $cKey1 );
236 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
237 $t2 = $cache->getCheckKeyTime( $cKey2 );
238 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
240 $priorTime = microtime( true );
242 $v = $cache->getWithSetCallback(
243 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
245 $this->assertEquals( $value, $v, "Value returned" );
246 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
247 $t1 = $cache->getCheckKeyTime( $cKey1 );
248 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
249 $t2 = $cache->getCheckKeyTime( $cKey2 );
250 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
253 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
255 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
257 $this->assertEquals( $value, $v, "Value returned" );
259 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
262 $key = wfRandomString();
263 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
264 $this->assertEquals( $value, $v, "Value returned" );
265 $cache->delete( $key );
266 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
267 $this->assertEquals( $value, $v, "Value still returned after deleted" );
268 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
270 $timeyCache = new TimeAdjustableWANObjectCache( [
271 'cache' => new TimeAdjustableHashBagOStuff(),
275 $oldValReceived = -1;
276 $oldAsOfReceived = -1;
277 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
278 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
280 $oldValReceived = $oldVal;
281 $oldAsOfReceived = $oldAsOf;
283 return 'xxx' . $wasSet;
286 $now = microtime( true ); // reference time
289 $key = wfRandomString();
290 $timeyCache->setTime( $now );
291 $v = $timeyCache->getWithSetCallback(
292 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
293 $this->assertEquals( 'xxx1', $v, "Value returned" );
294 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
295 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
297 $timeyCache->setTime( $now +
40 );
298 $v = $timeyCache->getWithSetCallback(
299 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
300 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
301 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
302 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
303 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
305 $timeyCache->setTime( $now +
300 );
306 $v = $timeyCache->getWithSetCallback(
307 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
308 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
309 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
310 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
311 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
314 $key = wfRandomString();
315 $checkKey = $timeyCache->makeKey( 'template', 'X' );
316 $timeyCache->setTime( $now - $timeyCache::HOLDOFF_TTL
- 1 );
317 $timeyCache->touchCheckKey( $checkKey ); // init check key
318 $timeyCache->setTime( $now );
319 $v = $timeyCache->getWithSetCallback(
321 $timeyCache::TTL_WEEK
,
323 [ 'graceTTL' => $timeyCache::TTL_DAY
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
325 $this->assertEquals( 'xxx1', $v, "Value returned" );
326 $this->assertEquals( 1, $wasSet, "Value computed" );
327 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
328 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
330 $timeyCache->setTime( $now +
300 ); // some time passes
331 $timeyCache->touchCheckKey( $checkKey ); // make key stale
332 $timeyCache->setTime( $now +
3600 ); // ~23 hours left of grace
333 $v = $timeyCache->getWithSetCallback(
335 $timeyCache::TTL_WEEK
,
337 [ 'graceTTL' => $timeyCache::TTL_DAY
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
339 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
340 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
342 $timeyCache->setTime( $now +
$timeyCache::TTL_DAY
);
343 $v = $timeyCache->getWithSetCallback(
345 $timeyCache::TTL_WEEK
,
347 [ 'graceTTL' => $timeyCache::TTL_DAY
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
349 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
350 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
351 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
352 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
355 public static function getWithSetCallback_provider() {
358 [ [ 'version' => 1 ], true ]
362 public function testPreemtiveRefresh() {
365 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value )
371 $cache = new NearExpiringWANObjectCache( [
372 'cache' => new HashBagOStuff(),
377 $key = wfRandomString();
378 $opts = [ 'lowTTL' => 30 ];
379 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
380 $this->assertEquals( $value, $v, "Value returned" );
381 $this->assertEquals( 1, $wasSet, "Value calculated" );
382 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
383 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
386 $key = wfRandomString();
387 $opts = [ 'lowTTL' => 1 ];
388 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
389 $this->assertEquals( $value, $v, "Value returned" );
390 $this->assertEquals( 1, $wasSet, "Value calculated" );
391 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
392 $this->assertEquals( 1, $wasSet, "Value cached" );
394 $cache = new PopularityRefreshingWANObjectCache( [
395 'cache' => new HashBagOStuff(),
399 $now = microtime( true ); // reference time
401 $key = wfRandomString();
402 $opts = [ 'hotTTR' => 900 ];
403 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
404 $this->assertEquals( $value, $v, "Value returned" );
405 $this->assertEquals( 1, $wasSet, "Value calculated" );
406 $cache->setTime( $now +
30 );
407 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
408 $this->assertEquals( 1, $wasSet, "Value cached" );
411 $key = wfRandomString();
412 $opts = [ 'hotTTR' => 10 ];
413 $cache->setTime( $now );
414 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
415 $this->assertEquals( $value, $v, "Value returned" );
416 $this->assertEquals( 1, $wasSet, "Value calculated" );
417 $cache->setTime( $now +
30 );
418 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
419 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
423 * @covers WANObjectCache::getWithSetCallback()
424 * @covers WANObjectCache::doGetWithSetCallback()
426 public function testGetWithSetCallback_invalidCallback() {
427 $this->setExpectedException( InvalidArgumentException
::class );
428 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
432 * @dataProvider getMultiWithSetCallback_provider
433 * @covers WANObjectCache::getMultiWithSetCallback
434 * @covers WANObjectCache::makeMultiKeys
435 * @covers WANObjectCache::getMulti
436 * @param array $extOpts
437 * @param bool $versioned
439 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
440 $cache = $this->cache
;
442 $keyA = wfRandomString();
443 $keyB = wfRandomString();
444 $keyC = wfRandomString();
445 $cKey1 = wfRandomString();
446 $cKey2 = wfRandomString();
451 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
452 &$wasSet, &$priorValue, &$priorAsOf
457 $ttl = 20; // override with another value
462 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
464 $v = $cache->getMultiWithSetCallback(
465 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
466 $this->assertEquals( $value, $v[$keyA], "Value returned" );
467 $this->assertEquals( 1, $wasSet, "Value regenerated" );
468 $this->assertFalse( $priorValue, "No prior value" );
469 $this->assertNull( $priorAsOf, "No prior value" );
472 $cache->get( $keyA, $curTTL );
473 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
474 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
478 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
479 $v = $cache->getMultiWithSetCallback(
480 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
481 $this->assertEquals( $value, $v[$keyB], "Value returned" );
482 $this->assertEquals( 1, $wasSet, "Value regenerated" );
483 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
484 $v = $cache->getMultiWithSetCallback(
485 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
486 $this->assertEquals( $value, $v[$keyB], "Value returned" );
487 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
488 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
490 $priorTime = microtime( true );
493 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
494 $v = $cache->getMultiWithSetCallback(
495 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
497 $this->assertEquals( $value, $v[$keyB], "Value returned" );
498 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
499 $this->assertEquals( $value, $priorValue, "Has prior value" );
500 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
501 $t1 = $cache->getCheckKeyTime( $cKey1 );
502 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
503 $t2 = $cache->getCheckKeyTime( $cKey2 );
504 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
506 $priorTime = microtime( true );
509 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
510 $v = $cache->getMultiWithSetCallback(
511 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
513 $this->assertEquals( $value, $v[$keyC], "Value returned" );
514 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
515 $t1 = $cache->getCheckKeyTime( $cKey1 );
516 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
517 $t2 = $cache->getCheckKeyTime( $cKey2 );
518 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
521 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
523 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
525 $this->assertEquals( $value, $v, "Value returned" );
527 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
530 $key = wfRandomString();
531 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
532 $v = $cache->getMultiWithSetCallback(
533 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
534 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
535 $cache->delete( $key );
536 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
537 $v = $cache->getMultiWithSetCallback(
538 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
539 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
540 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
543 $ids = [ 1, 2, 3, 4, 5, 6 ];
544 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
545 return $wanCache->makeKey( 'test', $id );
547 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
548 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
553 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
556 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
557 array_values( $values ),
558 "Correct values in correct order"
561 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
562 array_keys( $values ),
563 "Correct keys in correct order"
565 $this->assertEquals( count( $ids ), $calls );
567 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
568 $this->assertEquals( count( $ids ), $calls, "Values cached" );
570 // Mock the BagOStuff to assure only one getMulti() call given process caching
571 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
572 ->setMethods( [ 'getMulti' ] )->getMock();
573 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
574 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
575 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
577 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
579 // Warm the process cache
580 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
582 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
583 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
585 // Use the process cache
587 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
588 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
592 public static function getMultiWithSetCallback_provider() {
595 [ [ 'version' => 1 ], true ]
600 * @dataProvider getMultiWithUnionSetCallback_provider
601 * @covers WANObjectCache::getMultiWithUnionSetCallback()
602 * @covers WANObjectCache::makeMultiKeys()
603 * @param array $extOpts
604 * @param bool $versioned
606 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
607 $cache = $this->cache
;
609 $keyA = wfRandomString();
610 $keyB = wfRandomString();
611 $keyC = wfRandomString();
612 $cKey1 = wfRandomString();
613 $cKey2 = wfRandomString();
616 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
617 &$wasSet, &$priorValue, &$priorAsOf
620 foreach ( $ids as $id ) {
622 $newValues[$id] = "@$id$";
623 $ttls[$id] = 20; // override with another value
630 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
632 $v = $cache->getMultiWithUnionSetCallback(
633 $keyedIds, 30, $genFunc, $extOpts );
634 $this->assertEquals( $value, $v[$keyA], "Value returned" );
635 $this->assertEquals( 1, $wasSet, "Value regenerated" );
638 $cache->get( $keyA, $curTTL );
639 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
640 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
644 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
645 $v = $cache->getMultiWithUnionSetCallback(
646 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
647 $this->assertEquals( $value, $v[$keyB], "Value returned" );
648 $this->assertEquals( 1, $wasSet, "Value regenerated" );
649 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
650 $v = $cache->getMultiWithUnionSetCallback(
651 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
652 $this->assertEquals( $value, $v[$keyB], "Value returned" );
653 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
654 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
656 $priorTime = microtime( true );
659 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
660 $v = $cache->getMultiWithUnionSetCallback(
661 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
663 $this->assertEquals( $value, $v[$keyB], "Value returned" );
664 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
665 $t1 = $cache->getCheckKeyTime( $cKey1 );
666 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
667 $t2 = $cache->getCheckKeyTime( $cKey2 );
668 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
670 $priorTime = microtime( true );
673 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
674 $v = $cache->getMultiWithUnionSetCallback(
675 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
677 $this->assertEquals( $value, $v[$keyC], "Value returned" );
678 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
679 $t1 = $cache->getCheckKeyTime( $cKey1 );
680 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
681 $t2 = $cache->getCheckKeyTime( $cKey2 );
682 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
685 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
687 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
689 $this->assertEquals( $value, $v, "Value returned" );
691 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
694 $key = wfRandomString();
695 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
696 $v = $cache->getMultiWithUnionSetCallback(
697 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
698 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
699 $cache->delete( $key );
700 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
701 $v = $cache->getMultiWithUnionSetCallback(
702 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
703 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
704 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
707 $ids = [ 1, 2, 3, 4, 5, 6 ];
708 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
709 return $wanCache->makeKey( 'test', $id );
711 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
712 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
714 foreach ( $ids as $id ) {
716 $newValues[$id] = "val-{$id}";
721 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
724 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
725 array_values( $values ),
726 "Correct values in correct order"
729 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
730 array_keys( $values ),
731 "Correct keys in correct order"
733 $this->assertEquals( count( $ids ), $calls );
735 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
736 $this->assertEquals( count( $ids ), $calls, "Values cached" );
739 public static function getMultiWithUnionSetCallback_provider() {
742 [ [ 'version' => 1 ], true ]
747 * @covers WANObjectCache::getWithSetCallback()
748 * @covers WANObjectCache::doGetWithSetCallback()
750 public function testLockTSE() {
751 $cache = $this->cache
;
752 $key = wfRandomString();
753 $value = wfRandomString();
756 $func = function () use ( &$calls, $value, $cache, $key ) {
758 // Immediately kill any mutex rather than waiting a second
759 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
763 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
764 $this->assertEquals( $value, $ret );
765 $this->assertEquals( 1, $calls, 'Value was populated' );
767 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
768 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
770 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
771 $ret = $cache->getWithSetCallback( $key, 30, $func,
772 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
773 $this->assertEquals( $value, $ret, 'Old value used' );
774 $this->assertEquals( 1, $calls, 'Callback was not used' );
776 $cache->delete( $key );
777 $ret = $cache->getWithSetCallback( $key, 30, $func,
778 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
779 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
780 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
782 $ret = $cache->getWithSetCallback( $key, 30, $func,
783 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
784 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
785 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
789 * @covers WANObjectCache::getWithSetCallback()
790 * @covers WANObjectCache::doGetWithSetCallback()
791 * @covers WANObjectCache::set()
793 public function testLockTSESlow() {
794 $cache = $this->cache
;
795 $key = wfRandomString();
796 $value = wfRandomString();
799 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
801 $setOpts['since'] = microtime( true ) - 10;
802 // Immediately kill any mutex rather than waiting a second
803 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
807 // Value should be marked as stale due to snapshot lag
809 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
810 $this->assertEquals( $value, $ret );
811 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
812 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
813 $this->assertEquals( 1, $calls, 'Value was generated' );
815 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
816 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
817 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
818 $this->assertEquals( $value, $ret );
819 $this->assertEquals( 1, $calls, 'Callback was not used' );
823 * @covers WANObjectCache::getWithSetCallback()
824 * @covers WANObjectCache::doGetWithSetCallback()
826 public function testBusyValue() {
827 $cache = $this->cache
;
828 $key = wfRandomString();
829 $value = wfRandomString();
830 $busyValue = wfRandomString();
833 $func = function () use ( &$calls, $value, $cache, $key ) {
835 // Immediately kill any mutex rather than waiting a second
836 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
840 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
841 $this->assertEquals( $value, $ret );
842 $this->assertEquals( 1, $calls, 'Value was populated' );
844 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
845 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
847 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
848 $ret = $cache->getWithSetCallback( $key, 30, $func,
849 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
850 $this->assertEquals( $value, $ret, 'Callback used' );
851 $this->assertEquals( 2, $calls, 'Callback used' );
853 $ret = $cache->getWithSetCallback( $key, 30, $func,
854 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
855 $this->assertEquals( $value, $ret, 'Old value used' );
856 $this->assertEquals( 2, $calls, 'Callback was not used' );
858 $cache->delete( $key ); // no value at all anymore and still locked
859 $ret = $cache->getWithSetCallback( $key, 30, $func,
860 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
861 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
862 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
864 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
865 $ret = $cache->getWithSetCallback( $key, 30, $func,
866 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
867 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
868 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
870 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
871 $ret = $cache->getWithSetCallback( $key, 30, $func,
872 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
873 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
874 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
878 * @covers WANObjectCache::getMulti()
880 public function testGetMulti() {
881 $cache = $this->cache
;
883 $value1 = [ 'this' => 'is', 'a' => 'test' ];
884 $value2 = [ 'this' => 'is', 'another' => 'test' ];
886 $key1 = wfRandomString();
887 $key2 = wfRandomString();
888 $key3 = wfRandomString();
890 $cache->set( $key1, $value1, 5 );
891 $cache->set( $key2, $value2, 10 );
895 [ $key1 => $value1, $key2 => $value2 ],
896 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
897 'Result array populated'
900 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
901 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
902 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
904 $cKey1 = wfRandomString();
905 $cKey2 = wfRandomString();
907 $priorTime = microtime( true );
911 [ $key1 => $value1, $key2 => $value2 ],
912 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
913 "Result array populated even with new check keys"
915 $t1 = $cache->getCheckKeyTime( $cKey1 );
916 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
917 $t2 = $cache->getCheckKeyTime( $cKey2 );
918 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
919 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
920 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
921 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
926 [ $key1 => $value1, $key2 => $value2 ],
927 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
928 "Result array still populated even with new check keys"
930 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
931 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
932 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
936 * @covers WANObjectCache::getMulti()
937 * @covers WANObjectCache::processCheckKeys()
939 public function testGetMultiCheckKeys() {
940 $cache = $this->cache
;
942 $checkAll = wfRandomString();
943 $check1 = wfRandomString();
944 $check2 = wfRandomString();
945 $check3 = wfRandomString();
946 $value1 = wfRandomString();
947 $value2 = wfRandomString();
949 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
950 // several seconds during the test to assert the behaviour.
951 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
952 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
956 $cache->set( 'key1', $value1, 10 );
957 $cache->set( 'key2', $value2, 10 );
960 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
967 [ 'key1' => $value1, 'key2' => $value2 ],
971 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
972 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
973 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
974 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
976 $cache->touchCheckKey( $check1 );
979 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
986 [ 'key1' => $value1, 'key2' => $value2 ],
988 'key1 expired by check1, but value still provided'
990 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
991 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
993 $cache->touchCheckKey( $checkAll );
996 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1002 $this->assertEquals(
1003 [ 'key1' => $value1, 'key2' => $value2 ],
1005 'All keys expired by checkAll, but value still provided'
1007 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1008 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1012 * @covers WANObjectCache::get()
1013 * @covers WANObjectCache::processCheckKeys()
1015 public function testCheckKeyInitHoldoff() {
1016 $cache = $this->cache
;
1018 for ( $i = 0; $i < 500; ++
$i ) {
1019 $key = wfRandomString();
1020 $checkKey = wfRandomString();
1022 $cache->get( $key, $curTTL, [ $checkKey ] );
1023 $cache->set( $key, 'val', 10 );
1025 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1027 $this->assertEquals( 'val', $v );
1028 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1031 for ( $i = 0; $i < 500; ++
$i ) {
1032 $key = wfRandomString();
1033 $checkKey = wfRandomString();
1035 $cache->set( $key, 'val', 10 );
1037 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1039 $this->assertEquals( 'val', $v );
1040 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1045 * @covers WANObjectCache::delete
1046 * @covers WANObjectCache::relayDelete
1047 * @covers WANObjectCache::relayPurge
1049 public function testDelete() {
1050 $key = wfRandomString();
1051 $value = wfRandomString();
1052 $this->cache
->set( $key, $value );
1055 $v = $this->cache
->get( $key, $curTTL );
1056 $this->assertEquals( $value, $v, "Key was created with value" );
1057 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1059 $this->cache
->delete( $key );
1062 $v = $this->cache
->get( $key, $curTTL );
1063 $this->assertFalse( $v, "Deleted key has false value" );
1064 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1066 $this->cache
->set( $key, $value . 'more' );
1067 $v = $this->cache
->get( $key, $curTTL );
1068 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1069 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1071 $this->cache
->set( $key, $value );
1072 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1075 $v = $this->cache
->get( $key, $curTTL );
1076 $this->assertFalse( $v, "Deleted key has false value" );
1077 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1079 $this->cache
->set( $key, $value );
1080 $v = $this->cache
->get( $key, $curTTL );
1081 $this->assertEquals( $value, $v, "Key was created with value" );
1082 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1086 * @dataProvider getWithSetCallback_versions_provider
1087 * @covers WANObjectCache::getWithSetCallback()
1088 * @covers WANObjectCache::doGetWithSetCallback()
1089 * @param array $extOpts
1090 * @param bool $versioned
1092 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1093 $cache = $this->cache
;
1095 $key = wfRandomString();
1096 $value = wfRandomString();
1099 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
1104 // Set the main key (version N if versioned)
1106 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1107 $this->assertEquals( $value, $v, "Value returned" );
1108 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1109 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1110 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1111 // Set the key for version N+1 (if versioned)
1113 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1116 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1117 $this->assertEquals( $value, $v, "Value returned" );
1118 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1121 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1122 $this->assertEquals( $value, $v, "Value returned" );
1123 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1127 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1128 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1131 $cache->delete( $key );
1132 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1133 $this->assertEquals( $value, $v, "Value returned" );
1134 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1138 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1139 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1140 $this->assertEquals( $value, $v, "Value returned" );
1141 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1145 public static function getWithSetCallback_versions_provider() {
1148 [ [ 'version' => 1 ], true ]
1153 * @covers WANObjectCache::touchCheckKey
1154 * @covers WANObjectCache::resetCheckKey
1155 * @covers WANObjectCache::getCheckKeyTime
1156 * @covers WANObjectCache::makePurgeValue
1157 * @covers WANObjectCache::parsePurgeValue
1159 public function testTouchKeys() {
1160 $key = wfRandomString();
1162 $priorTime = microtime( true );
1164 $t0 = $this->cache
->getCheckKeyTime( $key );
1165 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1167 $priorTime = microtime( true );
1169 $this->cache
->touchCheckKey( $key );
1170 $t1 = $this->cache
->getCheckKeyTime( $key );
1171 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1173 $t2 = $this->cache
->getCheckKeyTime( $key );
1174 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1177 $this->cache
->touchCheckKey( $key );
1178 $t3 = $this->cache
->getCheckKeyTime( $key );
1179 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1181 $t4 = $this->cache
->getCheckKeyTime( $key );
1182 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1185 $this->cache
->resetCheckKey( $key );
1186 $t5 = $this->cache
->getCheckKeyTime( $key );
1187 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1189 $t6 = $this->cache
->getCheckKeyTime( $key );
1190 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1194 * @covers WANObjectCache::getMulti()
1196 public function testGetWithSeveralCheckKeys() {
1197 $key = wfRandomString();
1198 $tKey1 = wfRandomString();
1199 $tKey2 = wfRandomString();
1202 // Two check keys are newer (given hold-off) than $key, another is older
1203 $this->internalCache
->set(
1204 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1205 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1207 $this->internalCache
->set(
1208 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1209 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1211 $this->internalCache
->set(
1212 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1213 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1215 $this->cache
->set( $key, $value, 30 );
1218 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1219 $this->assertEquals( $value, $v, "Value matches" );
1220 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1221 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1225 * @covers WANObjectCache::reap()
1226 * @covers WANObjectCache::reapCheckKey()
1228 public function testReap() {
1229 $vKey1 = wfRandomString();
1230 $vKey2 = wfRandomString();
1231 $tKey1 = wfRandomString();
1232 $tKey2 = wfRandomString();
1235 $knownPurge = time() - 60;
1236 $goodTime = microtime( true ) - 5;
1237 $badTime = microtime( true ) - 300;
1239 $this->internalCache
->set(
1240 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1242 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1243 WANObjectCache
::FLD_VALUE
=> $value,
1244 WANObjectCache
::FLD_TTL
=> 3600,
1245 WANObjectCache
::FLD_TIME
=> $goodTime
1248 $this->internalCache
->set(
1249 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1251 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1252 WANObjectCache
::FLD_VALUE
=> $value,
1253 WANObjectCache
::FLD_TTL
=> 3600,
1254 WANObjectCache
::FLD_TIME
=> $badTime
1257 $this->internalCache
->set(
1258 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1259 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1261 $this->internalCache
->set(
1262 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1263 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1266 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1267 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1268 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1269 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1271 $this->assertFalse( $bad1 );
1272 $this->assertTrue( $bad2 );
1274 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1275 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1276 $this->assertFalse( $tBad1 );
1277 $this->assertTrue( $tBad2 );
1281 * @covers WANObjectCache::reap()
1283 public function testReap_fail() {
1284 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1285 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1286 $backend->expects( $this->once() )->method( 'get' )
1288 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1289 WANObjectCache
::FLD_VALUE
=> 'value',
1290 WANObjectCache
::FLD_TTL
=> 3600,
1291 WANObjectCache
::FLD_TIME
=> 300,
1293 $backend->expects( $this->once() )->method( 'changeTTL' )
1294 ->willReturn( false );
1296 $wanCache = new WANObjectCache( [
1297 'cache' => $backend,
1298 'pool' => 'testcache-hash',
1299 'relayer' => new EventRelayerNull( [] )
1303 $ret = $wanCache->reap( 'key', 360, $isStale );
1304 $this->assertTrue( $isStale, 'value was stale' );
1305 $this->assertFalse( $ret, 'changeTTL failed' );
1309 * @covers WANObjectCache::set()
1311 public function testSetWithLag() {
1314 $key = wfRandomString();
1315 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1316 $this->cache
->set( $key, $value, 30, $opts );
1317 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1319 $key = wfRandomString();
1320 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1321 $this->cache
->set( $key, $value, 30, $opts );
1322 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1324 $key = wfRandomString();
1325 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1326 $this->cache
->set( $key, $value, 30, $opts );
1327 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1331 * @covers WANObjectCache::set()
1333 public function testWritePending() {
1336 $key = wfRandomString();
1337 $opts = [ 'pending' => true ];
1338 $this->cache
->set( $key, $value, 30, $opts );
1339 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1342 public function testMcRouterSupport() {
1343 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1344 ->setMethods( [ 'set', 'delete' ] )->getMock();
1345 $localBag->expects( $this->never() )->method( 'set' );
1346 $localBag->expects( $this->never() )->method( 'delete' );
1347 $wanCache = new WANObjectCache( [
1348 'cache' => $localBag,
1349 'pool' => 'testcache-hash',
1350 'relayer' => new EventRelayerNull( [] )
1352 $valFunc = function () {
1356 // None of these should use broadcasting commands (e.g. SET, DELETE)
1357 $wanCache->get( 'x' );
1358 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1359 $wanCache->getMulti( [ 'x', 'y' ] );
1360 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1361 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1362 $wanCache->getCheckKeyTime( 'zzz' );
1363 $wanCache->reap( 'x', time() - 300 );
1364 $wanCache->reap( 'zzz', time() - 300 );
1368 * @dataProvider provideAdaptiveTTL
1369 * @covers WANObjectCache::adaptiveTTL()
1370 * @param float|int $ago
1371 * @param int $maxTTL
1372 * @param int $minTTL
1373 * @param float $factor
1374 * @param int $adaptiveTTL
1376 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1377 $mtime = $ago ?
time() - $ago : $ago;
1379 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1381 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1382 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1384 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1386 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1387 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1390 public static function provideAdaptiveTTL() {
1392 [ 3600, 900, 30, 0.2, 720 ],
1393 [ 3600, 500, 30, 0.2, 500 ],
1394 [ 3600, 86400, 800, 0.2, 800 ],
1395 [ false, 86400, 800, 0.2, 800 ],
1396 [ null, 86400, 800, 0.2, 800 ]
1401 * @covers WANObjectCache::__construct
1402 * @covers WANObjectCache::newEmpty
1404 public function testNewEmpty() {
1405 $this->assertInstanceOf(
1406 WANObjectCache
::class,
1407 WANObjectCache
::newEmpty()
1412 * @covers WANObjectCache::setLogger
1414 public function testSetLogger() {
1415 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1419 * @covers WANObjectCache::getQoS
1421 public function testGetQoS() {
1422 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1423 ->setMethods( [ 'getQoS' ] )->getMock();
1424 $backend->expects( $this->once() )->method( 'getQoS' )
1425 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1426 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1429 $wanCache::QOS_UNKNOWN
,
1430 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1435 * @covers WANObjectCache::makeKey
1437 public function testMakeKey() {
1438 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1439 ->setMethods( [ 'makeKey' ] )->getMock();
1440 $backend->expects( $this->once() )->method( 'makeKey' )
1441 ->willReturn( 'special' );
1443 $wanCache = new WANObjectCache( [
1444 'cache' => $backend,
1445 'pool' => 'testcache-hash',
1446 'relayer' => new EventRelayerNull( [] )
1449 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1453 * @covers WANObjectCache::makeGlobalKey
1455 public function testMakeGlobalKey() {
1456 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1457 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1458 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1459 ->willReturn( 'special' );
1461 $wanCache = new WANObjectCache( [
1462 'cache' => $backend,
1463 'pool' => 'testcache-hash',
1464 'relayer' => new EventRelayerNull( [] )
1467 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1470 public static function statsKeyProvider() {
1472 [ 'domain:page:5', 'page' ],
1473 [ 'domain:main-key', 'main-key' ],
1474 [ 'domain:page:history', 'page' ],
1475 [ 'missingdomainkey', 'missingdomainkey' ]
1480 * @dataProvider statsKeyProvider
1481 * @covers WANObjectCache::determineKeyClass
1483 public function testStatsKeyClass( $key, $class ) {
1484 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1485 'cache' => new HashBagOStuff
,
1486 'pool' => 'testcache-hash',
1487 'relayer' => new EventRelayerNull( [] )
1490 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1494 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1495 private $timeOverride = 0;
1497 public function setTime( $time ) {
1498 $this->timeOverride
= $time;
1501 protected function getCurrentTime() {
1502 return $this->timeOverride ?
: parent
::getCurrentTime();
1506 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1507 private $timeOverride = 0;
1509 public function setTime( $time ) {
1510 $this->timeOverride
= $time;
1511 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1512 $this->cache
->setTime( $time );
1516 protected function getCurrentTime() {
1517 return $this->timeOverride ?
: parent
::getCurrentTime();
1521 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1522 const CLOCK_SKEW
= 1;
1524 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1525 return ( ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1529 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1530 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1531 return ( ( $now - $asOf ) > $timeTillRefresh );