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 $backToTheFutureCache = 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;
287 $key = wfRandomString();
288 $v = $backToTheFutureCache->getWithSetCallback(
289 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
290 $this->assertEquals( 'xxx1', $v, "Value returned" );
291 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
292 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
294 $backToTheFutureCache->setTime( microtime( true ) +
40 );
295 $v = $backToTheFutureCache->getWithSetCallback(
296 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
297 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
298 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
299 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
300 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
302 $backToTheFutureCache->setTime( microtime( true ) +
300 );
303 $v = $backToTheFutureCache->getWithSetCallback(
304 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
305 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
306 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
307 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
308 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
311 public static function getWithSetCallback_provider() {
314 [ [ 'version' => 1 ], true ]
318 public function testPreemtiveRefresh() {
321 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value )
327 $cache = new NearExpiringWANObjectCache( [
328 'cache' => new HashBagOStuff(),
333 $key = wfRandomString();
334 $opts = [ 'lowTTL' => 30 ];
335 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
336 $this->assertEquals( $value, $v, "Value returned" );
337 $this->assertEquals( 1, $wasSet, "Value calculated" );
338 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
339 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
342 $key = wfRandomString();
343 $opts = [ 'lowTTL' => 1 ];
344 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
345 $this->assertEquals( $value, $v, "Value returned" );
346 $this->assertEquals( 1, $wasSet, "Value calculated" );
347 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
348 $this->assertEquals( 1, $wasSet, "Value cached" );
350 $cache = new PopularityRefreshingWANObjectCache( [
351 'cache' => new HashBagOStuff(),
355 $now = microtime( true ); // reference time
357 $key = wfRandomString();
358 $opts = [ 'hotTTR' => 900 ];
359 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
360 $this->assertEquals( $value, $v, "Value returned" );
361 $this->assertEquals( 1, $wasSet, "Value calculated" );
362 $cache->setTime( $now +
30 );
363 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
364 $this->assertEquals( 1, $wasSet, "Value cached" );
367 $key = wfRandomString();
368 $opts = [ 'hotTTR' => 10 ];
369 $cache->setTime( $now );
370 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
371 $this->assertEquals( $value, $v, "Value returned" );
372 $this->assertEquals( 1, $wasSet, "Value calculated" );
373 $cache->setTime( $now +
30 );
374 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
375 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
379 * @covers WANObjectCache::getWithSetCallback()
380 * @covers WANObjectCache::doGetWithSetCallback()
382 public function testGetWithSetCallback_invalidCallback() {
383 $this->setExpectedException( InvalidArgumentException
::class );
384 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
388 * @dataProvider getMultiWithSetCallback_provider
389 * @covers WANObjectCache::getMultiWithSetCallback
390 * @covers WANObjectCache::makeMultiKeys
391 * @covers WANObjectCache::getMulti
392 * @param array $extOpts
393 * @param bool $versioned
395 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
396 $cache = $this->cache
;
398 $keyA = wfRandomString();
399 $keyB = wfRandomString();
400 $keyC = wfRandomString();
401 $cKey1 = wfRandomString();
402 $cKey2 = wfRandomString();
407 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
408 &$wasSet, &$priorValue, &$priorAsOf
413 $ttl = 20; // override with another value
418 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
420 $v = $cache->getMultiWithSetCallback(
421 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
422 $this->assertEquals( $value, $v[$keyA], "Value returned" );
423 $this->assertEquals( 1, $wasSet, "Value regenerated" );
424 $this->assertFalse( $priorValue, "No prior value" );
425 $this->assertNull( $priorAsOf, "No prior value" );
428 $cache->get( $keyA, $curTTL );
429 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
430 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
434 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
435 $v = $cache->getMultiWithSetCallback(
436 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
437 $this->assertEquals( $value, $v[$keyB], "Value returned" );
438 $this->assertEquals( 1, $wasSet, "Value regenerated" );
439 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
440 $v = $cache->getMultiWithSetCallback(
441 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
442 $this->assertEquals( $value, $v[$keyB], "Value returned" );
443 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
444 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
446 $priorTime = microtime( true );
449 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
450 $v = $cache->getMultiWithSetCallback(
451 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
453 $this->assertEquals( $value, $v[$keyB], "Value returned" );
454 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
455 $this->assertEquals( $value, $priorValue, "Has prior value" );
456 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
457 $t1 = $cache->getCheckKeyTime( $cKey1 );
458 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
459 $t2 = $cache->getCheckKeyTime( $cKey2 );
460 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
462 $priorTime = microtime( true );
465 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
466 $v = $cache->getMultiWithSetCallback(
467 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
469 $this->assertEquals( $value, $v[$keyC], "Value returned" );
470 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
471 $t1 = $cache->getCheckKeyTime( $cKey1 );
472 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
473 $t2 = $cache->getCheckKeyTime( $cKey2 );
474 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
477 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
479 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
481 $this->assertEquals( $value, $v, "Value returned" );
483 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
486 $key = wfRandomString();
487 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
488 $v = $cache->getMultiWithSetCallback(
489 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
490 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
491 $cache->delete( $key );
492 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
493 $v = $cache->getMultiWithSetCallback(
494 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
495 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
496 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
499 $ids = [ 1, 2, 3, 4, 5, 6 ];
500 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
501 return $wanCache->makeKey( 'test', $id );
503 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
504 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
509 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
512 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
513 array_values( $values ),
514 "Correct values in correct order"
517 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
518 array_keys( $values ),
519 "Correct keys in correct order"
521 $this->assertEquals( count( $ids ), $calls );
523 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
524 $this->assertEquals( count( $ids ), $calls, "Values cached" );
526 // Mock the BagOStuff to assure only one getMulti() call given process caching
527 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
528 ->setMethods( [ 'getMulti' ] )->getMock();
529 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
530 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
531 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
533 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
535 // Warm the process cache
536 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
538 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
539 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
541 // Use the process cache
543 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
544 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
548 public static function getMultiWithSetCallback_provider() {
551 [ [ 'version' => 1 ], true ]
556 * @dataProvider getMultiWithUnionSetCallback_provider
557 * @covers WANObjectCache::getMultiWithUnionSetCallback()
558 * @covers WANObjectCache::makeMultiKeys()
559 * @param array $extOpts
560 * @param bool $versioned
562 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
563 $cache = $this->cache
;
565 $keyA = wfRandomString();
566 $keyB = wfRandomString();
567 $keyC = wfRandomString();
568 $cKey1 = wfRandomString();
569 $cKey2 = wfRandomString();
572 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
573 &$wasSet, &$priorValue, &$priorAsOf
576 foreach ( $ids as $id ) {
578 $newValues[$id] = "@$id$";
579 $ttls[$id] = 20; // override with another value
586 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
588 $v = $cache->getMultiWithUnionSetCallback(
589 $keyedIds, 30, $genFunc, $extOpts );
590 $this->assertEquals( $value, $v[$keyA], "Value returned" );
591 $this->assertEquals( 1, $wasSet, "Value regenerated" );
594 $cache->get( $keyA, $curTTL );
595 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
596 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
600 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
601 $v = $cache->getMultiWithUnionSetCallback(
602 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
603 $this->assertEquals( $value, $v[$keyB], "Value returned" );
604 $this->assertEquals( 1, $wasSet, "Value regenerated" );
605 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
606 $v = $cache->getMultiWithUnionSetCallback(
607 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
608 $this->assertEquals( $value, $v[$keyB], "Value returned" );
609 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
610 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
612 $priorTime = microtime( true );
615 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
616 $v = $cache->getMultiWithUnionSetCallback(
617 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
619 $this->assertEquals( $value, $v[$keyB], "Value returned" );
620 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
621 $t1 = $cache->getCheckKeyTime( $cKey1 );
622 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
623 $t2 = $cache->getCheckKeyTime( $cKey2 );
624 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
626 $priorTime = microtime( true );
629 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
630 $v = $cache->getMultiWithUnionSetCallback(
631 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
633 $this->assertEquals( $value, $v[$keyC], "Value returned" );
634 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
635 $t1 = $cache->getCheckKeyTime( $cKey1 );
636 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
637 $t2 = $cache->getCheckKeyTime( $cKey2 );
638 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
641 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
643 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
645 $this->assertEquals( $value, $v, "Value returned" );
647 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
650 $key = wfRandomString();
651 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
652 $v = $cache->getMultiWithUnionSetCallback(
653 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
654 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
655 $cache->delete( $key );
656 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
657 $v = $cache->getMultiWithUnionSetCallback(
658 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
659 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
660 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
663 $ids = [ 1, 2, 3, 4, 5, 6 ];
664 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
665 return $wanCache->makeKey( 'test', $id );
667 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
668 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
670 foreach ( $ids as $id ) {
672 $newValues[$id] = "val-{$id}";
677 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
680 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
681 array_values( $values ),
682 "Correct values in correct order"
685 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
686 array_keys( $values ),
687 "Correct keys in correct order"
689 $this->assertEquals( count( $ids ), $calls );
691 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
692 $this->assertEquals( count( $ids ), $calls, "Values cached" );
695 public static function getMultiWithUnionSetCallback_provider() {
698 [ [ 'version' => 1 ], true ]
703 * @covers WANObjectCache::getWithSetCallback()
704 * @covers WANObjectCache::doGetWithSetCallback()
706 public function testLockTSE() {
707 $cache = $this->cache
;
708 $key = wfRandomString();
709 $value = wfRandomString();
712 $func = function () use ( &$calls, $value, $cache, $key ) {
714 // Immediately kill any mutex rather than waiting a second
715 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
719 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
720 $this->assertEquals( $value, $ret );
721 $this->assertEquals( 1, $calls, 'Value was populated' );
723 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
724 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
726 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
727 $ret = $cache->getWithSetCallback( $key, 30, $func,
728 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
729 $this->assertEquals( $value, $ret, 'Old value used' );
730 $this->assertEquals( 1, $calls, 'Callback was not used' );
732 $cache->delete( $key );
733 $ret = $cache->getWithSetCallback( $key, 30, $func,
734 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
735 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
736 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
738 $ret = $cache->getWithSetCallback( $key, 30, $func,
739 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
740 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
741 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
745 * @covers WANObjectCache::getWithSetCallback()
746 * @covers WANObjectCache::doGetWithSetCallback()
747 * @covers WANObjectCache::set()
749 public function testLockTSESlow() {
750 $cache = $this->cache
;
751 $key = wfRandomString();
752 $value = wfRandomString();
755 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
757 $setOpts['since'] = microtime( true ) - 10;
758 // Immediately kill any mutex rather than waiting a second
759 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
763 // Value should be marked as stale due to snapshot lag
765 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
766 $this->assertEquals( $value, $ret );
767 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
768 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
769 $this->assertEquals( 1, $calls, 'Value was generated' );
771 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
772 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
773 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
774 $this->assertEquals( $value, $ret );
775 $this->assertEquals( 1, $calls, 'Callback was not used' );
779 * @covers WANObjectCache::getWithSetCallback()
780 * @covers WANObjectCache::doGetWithSetCallback()
782 public function testBusyValue() {
783 $cache = $this->cache
;
784 $key = wfRandomString();
785 $value = wfRandomString();
786 $busyValue = wfRandomString();
789 $func = function () use ( &$calls, $value, $cache, $key ) {
791 // Immediately kill any mutex rather than waiting a second
792 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
796 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
797 $this->assertEquals( $value, $ret );
798 $this->assertEquals( 1, $calls, 'Value was populated' );
800 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
801 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
803 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
804 $ret = $cache->getWithSetCallback( $key, 30, $func,
805 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
806 $this->assertEquals( $value, $ret, 'Callback used' );
807 $this->assertEquals( 2, $calls, 'Callback used' );
809 $ret = $cache->getWithSetCallback( $key, 30, $func,
810 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
811 $this->assertEquals( $value, $ret, 'Old value used' );
812 $this->assertEquals( 2, $calls, 'Callback was not used' );
814 $cache->delete( $key ); // no value at all anymore and still locked
815 $ret = $cache->getWithSetCallback( $key, 30, $func,
816 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
817 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
818 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
820 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
821 $ret = $cache->getWithSetCallback( $key, 30, $func,
822 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
823 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
824 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
826 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
827 $ret = $cache->getWithSetCallback( $key, 30, $func,
828 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
829 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
830 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
834 * @covers WANObjectCache::getMulti()
836 public function testGetMulti() {
837 $cache = $this->cache
;
839 $value1 = [ 'this' => 'is', 'a' => 'test' ];
840 $value2 = [ 'this' => 'is', 'another' => 'test' ];
842 $key1 = wfRandomString();
843 $key2 = wfRandomString();
844 $key3 = wfRandomString();
846 $cache->set( $key1, $value1, 5 );
847 $cache->set( $key2, $value2, 10 );
851 [ $key1 => $value1, $key2 => $value2 ],
852 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
853 'Result array populated'
856 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
857 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
858 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
860 $cKey1 = wfRandomString();
861 $cKey2 = wfRandomString();
863 $priorTime = microtime( true );
867 [ $key1 => $value1, $key2 => $value2 ],
868 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
869 "Result array populated even with new check keys"
871 $t1 = $cache->getCheckKeyTime( $cKey1 );
872 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
873 $t2 = $cache->getCheckKeyTime( $cKey2 );
874 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
875 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
876 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
877 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
882 [ $key1 => $value1, $key2 => $value2 ],
883 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
884 "Result array still populated even with new check keys"
886 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
887 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
888 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
892 * @covers WANObjectCache::getMulti()
893 * @covers WANObjectCache::processCheckKeys()
895 public function testGetMultiCheckKeys() {
896 $cache = $this->cache
;
898 $checkAll = wfRandomString();
899 $check1 = wfRandomString();
900 $check2 = wfRandomString();
901 $check3 = wfRandomString();
902 $value1 = wfRandomString();
903 $value2 = wfRandomString();
905 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
906 // several seconds during the test to assert the behaviour.
907 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
908 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
912 $cache->set( 'key1', $value1, 10 );
913 $cache->set( 'key2', $value2, 10 );
916 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
923 [ 'key1' => $value1, 'key2' => $value2 ],
927 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
928 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
929 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
930 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
932 $cache->touchCheckKey( $check1 );
935 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
942 [ 'key1' => $value1, 'key2' => $value2 ],
944 'key1 expired by check1, but value still provided'
946 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
947 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
949 $cache->touchCheckKey( $checkAll );
952 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
959 [ 'key1' => $value1, 'key2' => $value2 ],
961 'All keys expired by checkAll, but value still provided'
963 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
964 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
968 * @covers WANObjectCache::get()
969 * @covers WANObjectCache::processCheckKeys()
971 public function testCheckKeyInitHoldoff() {
972 $cache = $this->cache
;
974 for ( $i = 0; $i < 500; ++
$i ) {
975 $key = wfRandomString();
976 $checkKey = wfRandomString();
978 $cache->get( $key, $curTTL, [ $checkKey ] );
979 $cache->set( $key, 'val', 10 );
981 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
983 $this->assertEquals( 'val', $v );
984 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
987 for ( $i = 0; $i < 500; ++
$i ) {
988 $key = wfRandomString();
989 $checkKey = wfRandomString();
991 $cache->set( $key, 'val', 10 );
993 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
995 $this->assertEquals( 'val', $v );
996 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1001 * @covers WANObjectCache::delete
1002 * @covers WANObjectCache::relayDelete
1003 * @covers WANObjectCache::relayPurge
1005 public function testDelete() {
1006 $key = wfRandomString();
1007 $value = wfRandomString();
1008 $this->cache
->set( $key, $value );
1011 $v = $this->cache
->get( $key, $curTTL );
1012 $this->assertEquals( $value, $v, "Key was created with value" );
1013 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1015 $this->cache
->delete( $key );
1018 $v = $this->cache
->get( $key, $curTTL );
1019 $this->assertFalse( $v, "Deleted key has false value" );
1020 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1022 $this->cache
->set( $key, $value . 'more' );
1023 $v = $this->cache
->get( $key, $curTTL );
1024 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1025 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1027 $this->cache
->set( $key, $value );
1028 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1031 $v = $this->cache
->get( $key, $curTTL );
1032 $this->assertFalse( $v, "Deleted key has false value" );
1033 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1035 $this->cache
->set( $key, $value );
1036 $v = $this->cache
->get( $key, $curTTL );
1037 $this->assertEquals( $value, $v, "Key was created with value" );
1038 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1042 * @dataProvider getWithSetCallback_versions_provider
1043 * @covers WANObjectCache::getWithSetCallback()
1044 * @covers WANObjectCache::doGetWithSetCallback()
1045 * @param array $extOpts
1046 * @param bool $versioned
1048 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1049 $cache = $this->cache
;
1051 $key = wfRandomString();
1052 $value = wfRandomString();
1055 $func = function ( $old, &$ttl ) use ( &$wasSet, $value ) {
1060 // Set the main key (version N if versioned)
1062 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1063 $this->assertEquals( $value, $v, "Value returned" );
1064 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1065 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1066 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1067 // Set the key for version N+1 (if versioned)
1069 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1072 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1073 $this->assertEquals( $value, $v, "Value returned" );
1074 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1077 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1078 $this->assertEquals( $value, $v, "Value returned" );
1079 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1083 $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1084 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
1087 $cache->delete( $key );
1088 $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
1089 $this->assertEquals( $value, $v, "Value returned" );
1090 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1094 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1095 $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts +
$extOpts );
1096 $this->assertEquals( $value, $v, "Value returned" );
1097 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1101 public static function getWithSetCallback_versions_provider() {
1104 [ [ 'version' => 1 ], true ]
1109 * @covers WANObjectCache::touchCheckKey
1110 * @covers WANObjectCache::resetCheckKey
1111 * @covers WANObjectCache::getCheckKeyTime
1112 * @covers WANObjectCache::makePurgeValue
1113 * @covers WANObjectCache::parsePurgeValue
1115 public function testTouchKeys() {
1116 $key = wfRandomString();
1118 $priorTime = microtime( true );
1120 $t0 = $this->cache
->getCheckKeyTime( $key );
1121 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1123 $priorTime = microtime( true );
1125 $this->cache
->touchCheckKey( $key );
1126 $t1 = $this->cache
->getCheckKeyTime( $key );
1127 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1129 $t2 = $this->cache
->getCheckKeyTime( $key );
1130 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1133 $this->cache
->touchCheckKey( $key );
1134 $t3 = $this->cache
->getCheckKeyTime( $key );
1135 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1137 $t4 = $this->cache
->getCheckKeyTime( $key );
1138 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1141 $this->cache
->resetCheckKey( $key );
1142 $t5 = $this->cache
->getCheckKeyTime( $key );
1143 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1145 $t6 = $this->cache
->getCheckKeyTime( $key );
1146 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1150 * @covers WANObjectCache::getMulti()
1152 public function testGetWithSeveralCheckKeys() {
1153 $key = wfRandomString();
1154 $tKey1 = wfRandomString();
1155 $tKey2 = wfRandomString();
1158 // Two check keys are newer (given hold-off) than $key, another is older
1159 $this->internalCache
->set(
1160 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1161 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1163 $this->internalCache
->set(
1164 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1165 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1167 $this->internalCache
->set(
1168 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1169 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1171 $this->cache
->set( $key, $value, 30 );
1174 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1175 $this->assertEquals( $value, $v, "Value matches" );
1176 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1177 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1181 * @covers WANObjectCache::reap()
1182 * @covers WANObjectCache::reapCheckKey()
1184 public function testReap() {
1185 $vKey1 = wfRandomString();
1186 $vKey2 = wfRandomString();
1187 $tKey1 = wfRandomString();
1188 $tKey2 = wfRandomString();
1191 $knownPurge = time() - 60;
1192 $goodTime = microtime( true ) - 5;
1193 $badTime = microtime( true ) - 300;
1195 $this->internalCache
->set(
1196 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1198 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1199 WANObjectCache
::FLD_VALUE
=> $value,
1200 WANObjectCache
::FLD_TTL
=> 3600,
1201 WANObjectCache
::FLD_TIME
=> $goodTime
1204 $this->internalCache
->set(
1205 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1207 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1208 WANObjectCache
::FLD_VALUE
=> $value,
1209 WANObjectCache
::FLD_TTL
=> 3600,
1210 WANObjectCache
::FLD_TIME
=> $badTime
1213 $this->internalCache
->set(
1214 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1215 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1217 $this->internalCache
->set(
1218 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1219 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1222 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1223 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1224 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1225 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1227 $this->assertFalse( $bad1 );
1228 $this->assertTrue( $bad2 );
1230 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1231 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1232 $this->assertFalse( $tBad1 );
1233 $this->assertTrue( $tBad2 );
1237 * @covers WANObjectCache::reap()
1239 public function testReap_fail() {
1240 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1241 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1242 $backend->expects( $this->once() )->method( 'get' )
1244 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1245 WANObjectCache
::FLD_VALUE
=> 'value',
1246 WANObjectCache
::FLD_TTL
=> 3600,
1247 WANObjectCache
::FLD_TIME
=> 300,
1249 $backend->expects( $this->once() )->method( 'changeTTL' )
1250 ->willReturn( false );
1252 $wanCache = new WANObjectCache( [
1253 'cache' => $backend,
1254 'pool' => 'testcache-hash',
1255 'relayer' => new EventRelayerNull( [] )
1259 $ret = $wanCache->reap( 'key', 360, $isStale );
1260 $this->assertTrue( $isStale, 'value was stale' );
1261 $this->assertFalse( $ret, 'changeTTL failed' );
1265 * @covers WANObjectCache::set()
1267 public function testSetWithLag() {
1270 $key = wfRandomString();
1271 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1272 $this->cache
->set( $key, $value, 30, $opts );
1273 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1275 $key = wfRandomString();
1276 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1277 $this->cache
->set( $key, $value, 30, $opts );
1278 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1280 $key = wfRandomString();
1281 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1282 $this->cache
->set( $key, $value, 30, $opts );
1283 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1287 * @covers WANObjectCache::set()
1289 public function testWritePending() {
1292 $key = wfRandomString();
1293 $opts = [ 'pending' => true ];
1294 $this->cache
->set( $key, $value, 30, $opts );
1295 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1298 public function testMcRouterSupport() {
1299 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1300 ->setMethods( [ 'set', 'delete' ] )->getMock();
1301 $localBag->expects( $this->never() )->method( 'set' );
1302 $localBag->expects( $this->never() )->method( 'delete' );
1303 $wanCache = new WANObjectCache( [
1304 'cache' => $localBag,
1305 'pool' => 'testcache-hash',
1306 'relayer' => new EventRelayerNull( [] )
1308 $valFunc = function () {
1312 // None of these should use broadcasting commands (e.g. SET, DELETE)
1313 $wanCache->get( 'x' );
1314 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1315 $wanCache->getMulti( [ 'x', 'y' ] );
1316 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1317 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1318 $wanCache->getCheckKeyTime( 'zzz' );
1319 $wanCache->reap( 'x', time() - 300 );
1320 $wanCache->reap( 'zzz', time() - 300 );
1324 * @dataProvider provideAdaptiveTTL
1325 * @covers WANObjectCache::adaptiveTTL()
1326 * @param float|int $ago
1327 * @param int $maxTTL
1328 * @param int $minTTL
1329 * @param float $factor
1330 * @param int $adaptiveTTL
1332 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1333 $mtime = $ago ?
time() - $ago : $ago;
1335 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1337 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1338 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1340 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1342 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1343 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1346 public static function provideAdaptiveTTL() {
1348 [ 3600, 900, 30, 0.2, 720 ],
1349 [ 3600, 500, 30, 0.2, 500 ],
1350 [ 3600, 86400, 800, 0.2, 800 ],
1351 [ false, 86400, 800, 0.2, 800 ],
1352 [ null, 86400, 800, 0.2, 800 ]
1357 * @covers WANObjectCache::__construct
1358 * @covers WANObjectCache::newEmpty
1360 public function testNewEmpty() {
1361 $this->assertInstanceOf(
1362 WANObjectCache
::class,
1363 WANObjectCache
::newEmpty()
1368 * @covers WANObjectCache::setLogger
1370 public function testSetLogger() {
1371 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1375 * @covers WANObjectCache::getQoS
1377 public function testGetQoS() {
1378 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1379 ->setMethods( [ 'getQoS' ] )->getMock();
1380 $backend->expects( $this->once() )->method( 'getQoS' )
1381 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1382 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1385 $wanCache::QOS_UNKNOWN
,
1386 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1391 * @covers WANObjectCache::makeKey
1393 public function testMakeKey() {
1394 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1395 ->setMethods( [ 'makeKey' ] )->getMock();
1396 $backend->expects( $this->once() )->method( 'makeKey' )
1397 ->willReturn( 'special' );
1399 $wanCache = new WANObjectCache( [
1400 'cache' => $backend,
1401 'pool' => 'testcache-hash',
1402 'relayer' => new EventRelayerNull( [] )
1405 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1409 * @covers WANObjectCache::makeGlobalKey
1411 public function testMakeGlobalKey() {
1412 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1413 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1414 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1415 ->willReturn( 'special' );
1417 $wanCache = new WANObjectCache( [
1418 'cache' => $backend,
1419 'pool' => 'testcache-hash',
1420 'relayer' => new EventRelayerNull( [] )
1423 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1426 public static function statsKeyProvider() {
1428 [ 'domain:page:5', 'page' ],
1429 [ 'domain:main-key', 'main-key' ],
1430 [ 'domain:page:history', 'page' ],
1431 [ 'missingdomainkey', 'missingdomainkey' ]
1436 * @dataProvider statsKeyProvider
1437 * @covers WANObjectCache::determineKeyClass
1439 public function testStatsKeyClass( $key, $class ) {
1440 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1441 'cache' => new HashBagOStuff
,
1442 'pool' => 'testcache-hash',
1443 'relayer' => new EventRelayerNull( [] )
1446 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1450 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1451 private $timeOverride = 0;
1453 public function setTime( $time ) {
1454 $this->timeOverride
= $time;
1457 protected function getCurrentTime() {
1458 return $this->timeOverride ?
: parent
::getCurrentTime();
1462 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1463 private $timeOverride = 0;
1465 public function setTime( $time ) {
1466 $this->timeOverride
= $time;
1467 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1468 $this->cache
->setTime( $time );
1472 protected function getCurrentTime() {
1473 return $this->timeOverride ?
: parent
::getCurrentTime();
1477 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1478 const CLOCK_SKEW
= 1;
1480 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1481 return ( ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1485 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1486 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1487 return ( ( $now - $asOf ) > $timeTillRefresh );