3 use Wikimedia\TestingAccessWrapper
;
6 * @covers WANObjectCache::wrap
7 * @covers WANObjectCache::unwrap
8 * @covers WANObjectCache::worthRefreshExpiring
9 * @covers WANObjectCache::worthRefreshPopular
10 * @covers WANObjectCache::isValid
11 * @covers WANObjectCache::getWarmupKeyMisses
12 * @covers WANObjectCache::prefixCacheKeys
13 * @covers WANObjectCache::getProcessCache
14 * @covers WANObjectCache::getNonProcessCachedKeys
15 * @covers WANObjectCache::getRawKeysForWarmup
16 * @covers WANObjectCache::getInterimValue
17 * @covers WANObjectCache::setInterimValue
19 class WANObjectCacheTest
extends PHPUnit_Framework_TestCase
{
20 /** @var TimeAdjustableWANObjectCache */
23 private $internalCache;
25 protected function setUp() {
28 $this->cache
= new TimeAdjustableWANObjectCache( [
29 'cache' => new TimeAdjustableHashBagOStuff(),
30 'pool' => 'testcache-hash',
31 'relayer' => new EventRelayerNull( [] )
34 $wanCache = TestingAccessWrapper
::newFromObject( $this->cache
);
35 /** @noinspection PhpUndefinedFieldInspection */
36 $this->internalCache
= $wanCache->cache
;
40 * @dataProvider provideSetAndGet
41 * @covers WANObjectCache::set()
42 * @covers WANObjectCache::get()
43 * @covers WANObjectCache::makeKey()
47 public function testSetAndGet( $value, $ttl ) {
50 $key = $this->cache
->makeKey( 'x', wfRandomString() );
52 $this->cache
->get( $key, $curTTL, [], $asOf );
53 $this->assertNull( $curTTL, "Current TTL is null" );
54 $this->assertNull( $asOf, "Current as-of-time is infinite" );
56 $t = microtime( true );
57 $this->cache
->set( $key, $value, $ttl );
59 $this->assertEquals( $value, $this->cache
->get( $key, $curTTL, [], $asOf ) );
60 if ( is_infinite( $ttl ) ||
$ttl == 0 ) {
61 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
63 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
64 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
66 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
67 $this->assertLessThanOrEqual( $t +
1, $asOf, "As-of-time in range of set() time" );
70 public static function provideSetAndGet() {
77 [ (object)[ 'meow' ], 3 ],
85 * @covers WANObjectCache::get()
86 * @covers WANObjectCache::makeGlobalKey()
88 public function testGetNotExists() {
89 $key = $this->cache
->makeGlobalKey( 'y', wfRandomString(), 'p' );
91 $value = $this->cache
->get( $key, $curTTL );
93 $this->assertFalse( $value, "Non-existing key has false value" );
94 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
98 * @covers WANObjectCache::set()
100 public function testSetOver() {
101 $key = wfRandomString();
102 for ( $i = 0; $i < 3; ++
$i ) {
103 $value = wfRandomString();
104 $this->cache
->set( $key, $value, 3 );
106 $this->assertEquals( $this->cache
->get( $key ), $value );
111 * @covers WANObjectCache::set()
113 public function testStaleSet() {
114 $key = wfRandomString();
115 $value = wfRandomString();
116 $this->cache
->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
118 $this->assertFalse( $this->cache
->get( $key ), "Stale set() value ignored" );
121 public function testProcessCache() {
123 $callback = function () use ( &$hit ) {
127 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
128 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
130 foreach ( $keys as $i => $key ) {
131 $this->cache
->getWithSetCallback(
132 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
134 $this->assertEquals( 3, $hit );
136 foreach ( $keys as $i => $key ) {
137 $this->cache
->getWithSetCallback(
138 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
140 $this->assertEquals( 3, $hit, "Values cached" );
142 foreach ( $keys as $i => $key ) {
143 $this->cache
->getWithSetCallback(
144 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
146 $this->assertEquals( 6, $hit );
148 foreach ( $keys as $i => $key ) {
149 $this->cache
->getWithSetCallback(
150 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
152 $this->assertEquals( 6, $hit, "New values cached" );
154 foreach ( $keys as $i => $key ) {
155 $this->cache
->delete( $key );
156 $this->cache
->getWithSetCallback(
157 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
159 $this->assertEquals( 9, $hit, "Values evicted" );
161 $key = reset( $keys );
162 // Get into cache (default process cache group)
163 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
164 $this->assertEquals( 10, $hit, "Value calculated" );
165 $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
166 $this->assertEquals( 10, $hit, "Value cached" );
167 $outerCallback = function () use ( &$callback, $key ) {
168 $v = $this->cache
->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
172 // Outer key misses and refuses inner key process cache value
173 $this->cache
->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
174 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
178 * @dataProvider getWithSetCallback_provider
179 * @covers WANObjectCache::getWithSetCallback()
180 * @covers WANObjectCache::doGetWithSetCallback()
181 * @param array $extOpts
182 * @param bool $versioned
184 public function testGetWithSetCallback( array $extOpts, $versioned ) {
185 $cache = $this->cache
;
187 $key = wfRandomString();
188 $value = wfRandomString();
189 $cKey1 = wfRandomString();
190 $cKey2 = wfRandomString();
195 $func = function ( $old, &$ttl, &$opts, $asOf )
196 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
201 $ttl = 20; // override with another value
206 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] +
$extOpts );
207 $this->assertEquals( $value, $v, "Value returned" );
208 $this->assertEquals( 1, $wasSet, "Value regenerated" );
209 $this->assertFalse( $priorValue, "No prior value" );
210 $this->assertNull( $priorAsOf, "No prior value" );
213 $cache->get( $key, $curTTL );
214 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
215 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
218 $v = $cache->getWithSetCallback(
219 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] +
$extOpts );
220 $this->assertEquals( $value, $v, "Value returned" );
221 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
223 $priorTime = microtime( true ); // reference time
224 $cache->setTime( $priorTime );
226 $cache->addTime( 1 );
229 $v = $cache->getWithSetCallback(
230 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
232 $this->assertEquals( $value, $v, "Value returned" );
233 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
234 $this->assertEquals( $value, $priorValue, "Has prior value" );
235 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
236 $t1 = $cache->getCheckKeyTime( $cKey1 );
237 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
238 $t2 = $cache->getCheckKeyTime( $cKey2 );
239 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
241 $priorTime = $cache->addTime( 0.01 );
244 $v = $cache->getWithSetCallback(
245 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
247 $this->assertEquals( $value, $v, "Value returned" );
248 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
249 $t1 = $cache->getCheckKeyTime( $cKey1 );
250 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
251 $t2 = $cache->getCheckKeyTime( $cKey2 );
252 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
255 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
257 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
259 $this->assertEquals( $value, $v, "Value returned" );
261 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
264 $key = wfRandomString();
265 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
266 $this->assertEquals( $value, $v, "Value returned" );
267 $cache->delete( $key );
268 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] +
$extOpts );
269 $this->assertEquals( $value, $v, "Value still returned after deleted" );
270 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
272 $oldValReceived = -1;
273 $oldAsOfReceived = -1;
274 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
275 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
277 $oldValReceived = $oldVal;
278 $oldAsOfReceived = $oldAsOf;
280 return 'xxx' . $wasSet;
283 $now = microtime( true ); // reference time
286 $key = wfRandomString();
287 $v = $cache->getWithSetCallback(
288 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
289 $this->assertEquals( 'xxx1', $v, "Value returned" );
290 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
291 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
293 $cache->addTime( 40 );
294 $v = $cache->getWithSetCallback(
295 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
296 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
297 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
298 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
299 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
301 $cache->addTime( 260 );
302 $v = $cache->getWithSetCallback(
303 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] +
$extOpts );
304 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
305 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
306 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
307 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
310 $key = wfRandomString();
311 $checkKey = $cache->makeKey( 'template', 'X' );
312 $cache->setTime( $now - $cache::HOLDOFF_TTL
- 1 );
313 $cache->touchCheckKey( $checkKey ); // init check key
314 $cache->setTime( $now );
315 $v = $cache->getWithSetCallback(
317 $cache::TTL_INDEFINITE
,
319 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
321 $this->assertEquals( 'xxx1', $v, "Value returned" );
322 $this->assertEquals( 1, $wasSet, "Value computed" );
323 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
324 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
326 $cache->addTime( $cache::TTL_HOUR
); // some time passes
327 $v = $cache->getWithSetCallback(
329 $cache::TTL_INDEFINITE
,
331 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
333 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
334 $this->assertEquals( 1, $wasSet, "Cached value returned" );
336 $cache->touchCheckKey( $checkKey ); // make key stale
337 $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
339 $v = $cache->getWithSetCallback(
341 $cache::TTL_INDEFINITE
,
343 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
345 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
346 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
348 // Change of refresh increase to unity as staleness approaches graceTTL
350 $cache->addTime( $cache::TTL_WEEK
); // 8 days of being stale
351 $v = $cache->getWithSetCallback(
353 $cache::TTL_INDEFINITE
,
355 [ 'graceTTL' => $cache::TTL_WEEK
, 'checkKeys' => [ $checkKey ] ] +
$extOpts
357 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
358 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
359 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
360 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
363 public static function getWithSetCallback_provider() {
366 [ [ 'version' => 1 ], true ]
370 public function testPreemtiveRefresh() {
373 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, $value )
379 $cache = new NearExpiringWANObjectCache( [
380 'cache' => new HashBagOStuff(),
385 $key = wfRandomString();
386 $opts = [ 'lowTTL' => 30 ];
387 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
388 $this->assertEquals( $value, $v, "Value returned" );
389 $this->assertEquals( 1, $wasSet, "Value calculated" );
390 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
391 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
394 $key = wfRandomString();
395 $opts = [ 'lowTTL' => 1 ];
396 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
397 $this->assertEquals( $value, $v, "Value returned" );
398 $this->assertEquals( 1, $wasSet, "Value calculated" );
399 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
400 $this->assertEquals( 1, $wasSet, "Value cached" );
402 $cache = new PopularityRefreshingWANObjectCache( [
403 'cache' => new HashBagOStuff(),
407 $now = microtime( true ); // reference time
409 $key = wfRandomString();
410 $opts = [ 'hotTTR' => 900 ];
411 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
412 $this->assertEquals( $value, $v, "Value returned" );
413 $this->assertEquals( 1, $wasSet, "Value calculated" );
414 $cache->setTime( $now +
30 );
415 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
416 $this->assertEquals( 1, $wasSet, "Value cached" );
419 $key = wfRandomString();
420 $opts = [ 'hotTTR' => 10 ];
421 $cache->setTime( $now );
422 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
423 $this->assertEquals( $value, $v, "Value returned" );
424 $this->assertEquals( 1, $wasSet, "Value calculated" );
425 $cache->setTime( $now +
30 );
426 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
427 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
431 * @covers WANObjectCache::getWithSetCallback()
432 * @covers WANObjectCache::doGetWithSetCallback()
434 public function testGetWithSetCallback_invalidCallback() {
435 $this->setExpectedException( InvalidArgumentException
::class );
436 $this->cache
->getWithSetCallback( 'key', 30, 'invalid callback' );
440 * @dataProvider getMultiWithSetCallback_provider
441 * @covers WANObjectCache::getMultiWithSetCallback
442 * @covers WANObjectCache::makeMultiKeys
443 * @covers WANObjectCache::getMulti
444 * @param array $extOpts
445 * @param bool $versioned
447 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
448 $cache = $this->cache
;
450 $keyA = wfRandomString();
451 $keyB = wfRandomString();
452 $keyC = wfRandomString();
453 $cKey1 = wfRandomString();
454 $cKey2 = wfRandomString();
459 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
460 &$wasSet, &$priorValue, &$priorAsOf
465 $ttl = 20; // override with another value
470 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
472 $v = $cache->getMultiWithSetCallback(
473 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] +
$extOpts );
474 $this->assertEquals( $value, $v[$keyA], "Value returned" );
475 $this->assertEquals( 1, $wasSet, "Value regenerated" );
476 $this->assertFalse( $priorValue, "No prior value" );
477 $this->assertNull( $priorAsOf, "No prior value" );
480 $cache->get( $keyA, $curTTL );
481 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
482 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
486 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
487 $v = $cache->getMultiWithSetCallback(
488 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
489 $this->assertEquals( $value, $v[$keyB], "Value returned" );
490 $this->assertEquals( 1, $wasSet, "Value regenerated" );
491 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
492 $v = $cache->getMultiWithSetCallback(
493 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] +
$extOpts );
494 $this->assertEquals( $value, $v[$keyB], "Value returned" );
495 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
496 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
498 $priorTime = microtime( true ); // reference time
499 $cache->setTime( $priorTime );
501 $cache->addTime( 1 );
504 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
505 $v = $cache->getMultiWithSetCallback(
506 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
508 $this->assertEquals( $value, $v[$keyB], "Value returned" );
509 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
510 $this->assertEquals( $value, $priorValue, "Has prior value" );
511 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
512 $t1 = $cache->getCheckKeyTime( $cKey1 );
513 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
514 $t2 = $cache->getCheckKeyTime( $cKey2 );
515 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
517 $priorTime = $cache->addTime( 0.01 );
520 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
521 $v = $cache->getMultiWithSetCallback(
522 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
524 $this->assertEquals( $value, $v[$keyC], "Value returned" );
525 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
526 $t1 = $cache->getCheckKeyTime( $cKey1 );
527 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
528 $t2 = $cache->getCheckKeyTime( $cKey2 );
529 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
532 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
534 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
536 $this->assertEquals( $value, $v, "Value returned" );
538 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
541 $key = wfRandomString();
542 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
543 $v = $cache->getMultiWithSetCallback(
544 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
545 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
546 $cache->delete( $key );
547 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
548 $v = $cache->getMultiWithSetCallback(
549 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
550 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
551 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
554 $ids = [ 1, 2, 3, 4, 5, 6 ];
555 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
556 return $wanCache->makeKey( 'test', $id );
558 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
559 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
564 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
567 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
568 array_values( $values ),
569 "Correct values in correct order"
572 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
573 array_keys( $values ),
574 "Correct keys in correct order"
576 $this->assertEquals( count( $ids ), $calls );
578 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
579 $this->assertEquals( count( $ids ), $calls, "Values cached" );
581 // Mock the BagOStuff to assure only one getMulti() call given process caching
582 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
583 ->setMethods( [ 'getMulti' ] )->getMock();
584 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
585 WANObjectCache
::VALUE_KEY_PREFIX
. 'k1' => 'val-id1',
586 WANObjectCache
::VALUE_KEY_PREFIX
. 'k2' => 'val-id2'
588 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
590 // Warm the process cache
591 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
593 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
594 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
596 // Use the process cache
598 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
599 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
603 public static function getMultiWithSetCallback_provider() {
606 [ [ 'version' => 1 ], true ]
611 * @dataProvider getMultiWithUnionSetCallback_provider
612 * @covers WANObjectCache::getMultiWithUnionSetCallback()
613 * @covers WANObjectCache::makeMultiKeys()
614 * @param array $extOpts
615 * @param bool $versioned
617 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
618 $cache = $this->cache
;
620 $keyA = wfRandomString();
621 $keyB = wfRandomString();
622 $keyC = wfRandomString();
623 $cKey1 = wfRandomString();
624 $cKey2 = wfRandomString();
627 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
628 &$wasSet, &$priorValue, &$priorAsOf
631 foreach ( $ids as $id ) {
633 $newValues[$id] = "@$id$";
634 $ttls[$id] = 20; // override with another value
641 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
643 $v = $cache->getMultiWithUnionSetCallback(
644 $keyedIds, 30, $genFunc, $extOpts );
645 $this->assertEquals( $value, $v[$keyA], "Value returned" );
646 $this->assertEquals( 1, $wasSet, "Value regenerated" );
649 $cache->get( $keyA, $curTTL );
650 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
651 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
655 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
656 $v = $cache->getMultiWithUnionSetCallback(
657 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
658 $this->assertEquals( $value, $v[$keyB], "Value returned" );
659 $this->assertEquals( 1, $wasSet, "Value regenerated" );
660 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
661 $v = $cache->getMultiWithUnionSetCallback(
662 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] +
$extOpts );
663 $this->assertEquals( $value, $v[$keyB], "Value returned" );
664 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
665 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
667 $priorTime = microtime( true ); // reference time
668 $cache->setTime( $priorTime );
670 $cache->addTime( 1 );
673 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
674 $v = $cache->getMultiWithUnionSetCallback(
675 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
677 $this->assertEquals( $value, $v[$keyB], "Value returned" );
678 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
679 $t1 = $cache->getCheckKeyTime( $cKey1 );
680 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
681 $t2 = $cache->getCheckKeyTime( $cKey2 );
682 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
684 $priorTime = $cache->addTime( 0.01 );
687 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
688 $v = $cache->getMultiWithUnionSetCallback(
689 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] +
$extOpts
691 $this->assertEquals( $value, $v[$keyC], "Value returned" );
692 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
693 $t1 = $cache->getCheckKeyTime( $cKey1 );
694 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
695 $t2 = $cache->getCheckKeyTime( $cKey2 );
696 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
699 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
701 $this->assertEquals( $value, $v[$cache::VFLD_DATA
], "Value returned" );
703 $this->assertEquals( $value, $v, "Value returned" );
705 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
708 $key = wfRandomString();
709 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
710 $v = $cache->getMultiWithUnionSetCallback(
711 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
712 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
713 $cache->delete( $key );
714 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
715 $v = $cache->getMultiWithUnionSetCallback(
716 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] +
$extOpts );
717 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
718 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
721 $ids = [ 1, 2, 3, 4, 5, 6 ];
722 $keyFunc = function ( $id, WANObjectCache
$wanCache ) {
723 return $wanCache->makeKey( 'test', $id );
725 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
726 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
728 foreach ( $ids as $id ) {
730 $newValues[$id] = "val-{$id}";
735 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
738 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
739 array_values( $values ),
740 "Correct values in correct order"
743 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache
) ),
744 array_keys( $values ),
745 "Correct keys in correct order"
747 $this->assertEquals( count( $ids ), $calls );
749 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
750 $this->assertEquals( count( $ids ), $calls, "Values cached" );
753 public static function getMultiWithUnionSetCallback_provider() {
756 [ [ 'version' => 1 ], true ]
761 * @covers WANObjectCache::getWithSetCallback()
762 * @covers WANObjectCache::doGetWithSetCallback()
764 public function testLockTSE() {
765 $cache = $this->cache
;
766 $key = wfRandomString();
767 $value = wfRandomString();
770 $func = function () use ( &$calls, $value, $cache, $key ) {
772 // Immediately kill any mutex rather than waiting a second
773 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
777 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
778 $this->assertEquals( $value, $ret );
779 $this->assertEquals( 1, $calls, 'Value was populated' );
781 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
782 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
784 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
785 $ret = $cache->getWithSetCallback( $key, 30, $func,
786 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
787 $this->assertEquals( $value, $ret, 'Old value used' );
788 $this->assertEquals( 1, $calls, 'Callback was not used' );
790 $cache->delete( $key );
791 $ret = $cache->getWithSetCallback( $key, 30, $func,
792 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
793 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
794 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
796 $ret = $cache->getWithSetCallback( $key, 30, $func,
797 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
798 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
799 $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
803 * @covers WANObjectCache::getWithSetCallback()
804 * @covers WANObjectCache::doGetWithSetCallback()
805 * @covers WANObjectCache::set()
807 public function testLockTSESlow() {
808 $cache = $this->cache
;
809 $key = wfRandomString();
810 $value = wfRandomString();
813 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
815 $setOpts['since'] = microtime( true ) - 10;
816 // Immediately kill any mutex rather than waiting a second
817 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
821 // Value should be marked as stale due to snapshot lag
823 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
824 $this->assertEquals( $value, $ret );
825 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
826 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
827 $this->assertEquals( 1, $calls, 'Value was generated' );
829 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
830 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
831 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
832 $this->assertEquals( $value, $ret );
833 $this->assertEquals( 1, $calls, 'Callback was not used' );
837 * @covers WANObjectCache::getWithSetCallback()
838 * @covers WANObjectCache::doGetWithSetCallback()
840 public function testBusyValue() {
841 $cache = $this->cache
;
842 $key = wfRandomString();
843 $value = wfRandomString();
844 $busyValue = wfRandomString();
847 $func = function () use ( &$calls, $value, $cache, $key ) {
849 // Immediately kill any mutex rather than waiting a second
850 $cache->delete( $cache::MUTEX_KEY_PREFIX
. $key );
854 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
855 $this->assertEquals( $value, $ret );
856 $this->assertEquals( 1, $calls, 'Value was populated' );
858 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
859 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
861 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
862 $ret = $cache->getWithSetCallback( $key, 30, $func,
863 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
864 $this->assertEquals( $value, $ret, 'Callback used' );
865 $this->assertEquals( 2, $calls, 'Callback used' );
867 $ret = $cache->getWithSetCallback( $key, 30, $func,
868 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
869 $this->assertEquals( $value, $ret, 'Old value used' );
870 $this->assertEquals( 2, $calls, 'Callback was not used' );
872 $cache->delete( $key ); // no value at all anymore and still locked
873 $ret = $cache->getWithSetCallback( $key, 30, $func,
874 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
875 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
876 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
878 $this->internalCache
->delete( $cache::MUTEX_KEY_PREFIX
. $key );
879 $ret = $cache->getWithSetCallback( $key, 30, $func,
880 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
881 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
882 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
884 $this->internalCache
->add( $cache::MUTEX_KEY_PREFIX
. $key, 1, 0 );
885 $ret = $cache->getWithSetCallback( $key, 30, $func,
886 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
887 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
888 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
892 * @covers WANObjectCache::getMulti()
894 public function testGetMulti() {
895 $cache = $this->cache
;
897 $value1 = [ 'this' => 'is', 'a' => 'test' ];
898 $value2 = [ 'this' => 'is', 'another' => 'test' ];
900 $key1 = wfRandomString();
901 $key2 = wfRandomString();
902 $key3 = wfRandomString();
904 $cache->set( $key1, $value1, 5 );
905 $cache->set( $key2, $value2, 10 );
909 [ $key1 => $value1, $key2 => $value2 ],
910 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
911 'Result array populated'
914 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
915 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
916 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
918 $cKey1 = wfRandomString();
919 $cKey2 = wfRandomString();
921 $priorTime = microtime( true ); // reference time
922 $cache->setTime( $priorTime );
924 $cache->addTime( 1 );
928 [ $key1 => $value1, $key2 => $value2 ],
929 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
930 "Result array populated even with new check keys"
932 $t1 = $cache->getCheckKeyTime( $cKey1 );
933 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
934 $t2 = $cache->getCheckKeyTime( $cKey2 );
935 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
936 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
937 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
938 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
940 $cache->addTime( 1 );
944 [ $key1 => $value1, $key2 => $value2 ],
945 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
946 "Result array still populated even with new check keys"
948 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
949 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
950 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
954 * @covers WANObjectCache::getMulti()
955 * @covers WANObjectCache::processCheckKeys()
957 public function testGetMultiCheckKeys() {
958 $cache = $this->cache
;
960 $checkAll = wfRandomString();
961 $check1 = wfRandomString();
962 $check2 = wfRandomString();
963 $check3 = wfRandomString();
964 $value1 = wfRandomString();
965 $value2 = wfRandomString();
967 $cache->setTime( microtime( true ) );
969 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
970 // several seconds during the test to assert the behaviour.
971 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
972 $cache->touchCheckKey( $checkKey, WANObjectCache
::HOLDOFF_NONE
);
975 $cache->addTime( 0.100 );
977 $cache->set( 'key1', $value1, 10 );
978 $cache->set( 'key2', $value2, 10 );
981 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
988 [ 'key1' => $value1, 'key2' => $value2 ],
992 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
993 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
994 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
995 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
997 $cache->addTime( 0.100 );
998 $cache->touchCheckKey( $check1 );
1001 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1007 $this->assertEquals(
1008 [ 'key1' => $value1, 'key2' => $value2 ],
1010 'key1 expired by check1, but value still provided'
1012 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1013 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1015 $cache->touchCheckKey( $checkAll );
1018 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1024 $this->assertEquals(
1025 [ 'key1' => $value1, 'key2' => $value2 ],
1027 'All keys expired by checkAll, but value still provided'
1029 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1030 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1034 * @covers WANObjectCache::get()
1035 * @covers WANObjectCache::processCheckKeys()
1037 public function testCheckKeyInitHoldoff() {
1038 $cache = $this->cache
;
1040 for ( $i = 0; $i < 500; ++
$i ) {
1041 $key = wfRandomString();
1042 $checkKey = wfRandomString();
1044 $cache->get( $key, $curTTL, [ $checkKey ] );
1045 $cache->set( $key, 'val', 10 );
1047 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1049 $this->assertEquals( 'val', $v );
1050 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1053 for ( $i = 0; $i < 500; ++
$i ) {
1054 $key = wfRandomString();
1055 $checkKey = wfRandomString();
1057 $cache->set( $key, 'val', 10 );
1059 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1061 $this->assertEquals( 'val', $v );
1062 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1067 * @covers WANObjectCache::delete
1068 * @covers WANObjectCache::relayDelete
1069 * @covers WANObjectCache::relayPurge
1071 public function testDelete() {
1072 $key = wfRandomString();
1073 $value = wfRandomString();
1074 $this->cache
->set( $key, $value );
1077 $v = $this->cache
->get( $key, $curTTL );
1078 $this->assertEquals( $value, $v, "Key was created with value" );
1079 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1081 $this->cache
->delete( $key );
1084 $v = $this->cache
->get( $key, $curTTL );
1085 $this->assertFalse( $v, "Deleted key has false value" );
1086 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1088 $this->cache
->set( $key, $value . 'more' );
1089 $v = $this->cache
->get( $key, $curTTL );
1090 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1091 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1093 $this->cache
->set( $key, $value );
1094 $this->cache
->delete( $key, WANObjectCache
::HOLDOFF_NONE
);
1097 $v = $this->cache
->get( $key, $curTTL );
1098 $this->assertFalse( $v, "Deleted key has false value" );
1099 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1101 $this->cache
->set( $key, $value );
1102 $v = $this->cache
->get( $key, $curTTL );
1103 $this->assertEquals( $value, $v, "Key was created with value" );
1104 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1108 * @dataProvider getWithSetCallback_versions_provider
1109 * @covers WANObjectCache::getWithSetCallback()
1110 * @covers WANObjectCache::doGetWithSetCallback()
1111 * @param array $extOpts
1112 * @param bool $versioned
1114 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1115 $cache = $this->cache
;
1117 $key = wfRandomString();
1118 $valueV1 = wfRandomString();
1119 $valueV2 = [ wfRandomString() ];
1122 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1128 $priorValue = false;
1130 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1131 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1132 $priorValue = $oldValue;
1133 $priorAsOf = $oldAsOf;
1136 return $valueV2; // new array format
1139 // Set the main key (version N if versioned)
1141 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1142 $this->assertEquals( $valueV1, $v, "Value returned" );
1143 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1144 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1145 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1146 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1149 // Set the key for version N+1 format
1150 $verOpts = [ 'version' => $extOpts['version'] +
1 ];
1152 // Start versioning now with the unversioned key still there
1153 $verOpts = [ 'version' => 1 ];
1156 // Value goes to secondary key since V1 already used $key
1158 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1159 $this->assertEquals( $valueV2, $v, "Value returned" );
1160 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1161 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1162 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1165 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1166 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1167 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1169 // Clear out the older or unversioned key
1170 $cache->delete( $key, 0 );
1172 // Set the key for next/first versioned format
1174 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1175 $this->assertEquals( $valueV2, $v, "Value returned" );
1176 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1178 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts +
$extOpts );
1179 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1180 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1183 public static function getWithSetCallback_versions_provider() {
1186 [ [ 'version' => 1 ], true ]
1191 * @covers WANObjectCache::touchCheckKey
1192 * @covers WANObjectCache::resetCheckKey
1193 * @covers WANObjectCache::getCheckKeyTime
1194 * @covers WANObjectCache::makePurgeValue
1195 * @covers WANObjectCache::parsePurgeValue
1197 public function testTouchKeys() {
1198 $cache = $this->cache
;
1199 $key = wfRandomString();
1201 $priorTime = microtime( true ); // reference time
1202 $cache->setTime( $priorTime );
1204 $newTime = $cache->addTime( 0.100 );
1205 $t0 = $cache->getCheckKeyTime( $key );
1206 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1208 $priorTime = $newTime;
1209 $newTime = $cache->addTime( 0.100 );
1210 $cache->touchCheckKey( $key );
1211 $t1 = $cache->getCheckKeyTime( $key );
1212 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1214 $t2 = $cache->getCheckKeyTime( $key );
1215 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1217 $cache->addTime( 0.100 );
1218 $cache->touchCheckKey( $key );
1219 $t3 = $cache->getCheckKeyTime( $key );
1220 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1222 $t4 = $cache->getCheckKeyTime( $key );
1223 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1225 $cache->addTime( 0.100 );
1226 $cache->resetCheckKey( $key );
1227 $t5 = $cache->getCheckKeyTime( $key );
1228 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1230 $t6 = $cache->getCheckKeyTime( $key );
1231 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1235 * @covers WANObjectCache::getMulti()
1237 public function testGetWithSeveralCheckKeys() {
1238 $key = wfRandomString();
1239 $tKey1 = wfRandomString();
1240 $tKey2 = wfRandomString();
1243 // Two check keys are newer (given hold-off) than $key, another is older
1244 $this->internalCache
->set(
1245 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1246 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 3 )
1248 $this->internalCache
->set(
1249 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1250 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 5 )
1252 $this->internalCache
->set(
1253 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1254 WANObjectCache
::PURGE_VAL_PREFIX
. ( microtime( true ) - 30 )
1256 $this->cache
->set( $key, $value, 30 );
1259 $v = $this->cache
->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1260 $this->assertEquals( $value, $v, "Value matches" );
1261 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1262 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1266 * @covers WANObjectCache::reap()
1267 * @covers WANObjectCache::reapCheckKey()
1269 public function testReap() {
1270 $vKey1 = wfRandomString();
1271 $vKey2 = wfRandomString();
1272 $tKey1 = wfRandomString();
1273 $tKey2 = wfRandomString();
1276 $knownPurge = time() - 60;
1277 $goodTime = microtime( true ) - 5;
1278 $badTime = microtime( true ) - 300;
1280 $this->internalCache
->set(
1281 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey1,
1283 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1284 WANObjectCache
::FLD_VALUE
=> $value,
1285 WANObjectCache
::FLD_TTL
=> 3600,
1286 WANObjectCache
::FLD_TIME
=> $goodTime
1289 $this->internalCache
->set(
1290 WANObjectCache
::VALUE_KEY_PREFIX
. $vKey2,
1292 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1293 WANObjectCache
::FLD_VALUE
=> $value,
1294 WANObjectCache
::FLD_TTL
=> 3600,
1295 WANObjectCache
::FLD_TIME
=> $badTime
1298 $this->internalCache
->set(
1299 WANObjectCache
::TIME_KEY_PREFIX
. $tKey1,
1300 WANObjectCache
::PURGE_VAL_PREFIX
. $goodTime
1302 $this->internalCache
->set(
1303 WANObjectCache
::TIME_KEY_PREFIX
. $tKey2,
1304 WANObjectCache
::PURGE_VAL_PREFIX
. $badTime
1307 $this->assertEquals( $value, $this->cache
->get( $vKey1 ) );
1308 $this->assertEquals( $value, $this->cache
->get( $vKey2 ) );
1309 $this->cache
->reap( $vKey1, $knownPurge, $bad1 );
1310 $this->cache
->reap( $vKey2, $knownPurge, $bad2 );
1312 $this->assertFalse( $bad1 );
1313 $this->assertTrue( $bad2 );
1315 $this->cache
->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1316 $this->cache
->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1317 $this->assertFalse( $tBad1 );
1318 $this->assertTrue( $tBad2 );
1322 * @covers WANObjectCache::reap()
1324 public function testReap_fail() {
1325 $backend = $this->getMockBuilder( EmptyBagOStuff
::class )
1326 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1327 $backend->expects( $this->once() )->method( 'get' )
1329 WANObjectCache
::FLD_VERSION
=> WANObjectCache
::VERSION
,
1330 WANObjectCache
::FLD_VALUE
=> 'value',
1331 WANObjectCache
::FLD_TTL
=> 3600,
1332 WANObjectCache
::FLD_TIME
=> 300,
1334 $backend->expects( $this->once() )->method( 'changeTTL' )
1335 ->willReturn( false );
1337 $wanCache = new WANObjectCache( [
1338 'cache' => $backend,
1339 'pool' => 'testcache-hash',
1340 'relayer' => new EventRelayerNull( [] )
1344 $ret = $wanCache->reap( 'key', 360, $isStale );
1345 $this->assertTrue( $isStale, 'value was stale' );
1346 $this->assertFalse( $ret, 'changeTTL failed' );
1350 * @covers WANObjectCache::set()
1352 public function testSetWithLag() {
1355 $key = wfRandomString();
1356 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1357 $this->cache
->set( $key, $value, 30, $opts );
1358 $this->assertEquals( $value, $this->cache
->get( $key ), "Rep-lagged value written." );
1360 $key = wfRandomString();
1361 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1362 $this->cache
->set( $key, $value, 30, $opts );
1363 $this->assertEquals( false, $this->cache
->get( $key ), "Trx-lagged value not written." );
1365 $key = wfRandomString();
1366 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1367 $this->cache
->set( $key, $value, 30, $opts );
1368 $this->assertEquals( false, $this->cache
->get( $key ), "Lagged value not written." );
1372 * @covers WANObjectCache::set()
1374 public function testWritePending() {
1377 $key = wfRandomString();
1378 $opts = [ 'pending' => true ];
1379 $this->cache
->set( $key, $value, 30, $opts );
1380 $this->assertEquals( false, $this->cache
->get( $key ), "Pending value not written." );
1383 public function testMcRouterSupport() {
1384 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1385 ->setMethods( [ 'set', 'delete' ] )->getMock();
1386 $localBag->expects( $this->never() )->method( 'set' );
1387 $localBag->expects( $this->never() )->method( 'delete' );
1388 $wanCache = new WANObjectCache( [
1389 'cache' => $localBag,
1390 'pool' => 'testcache-hash',
1391 'relayer' => new EventRelayerNull( [] )
1393 $valFunc = function () {
1397 // None of these should use broadcasting commands (e.g. SET, DELETE)
1398 $wanCache->get( 'x' );
1399 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1400 $wanCache->getMulti( [ 'x', 'y' ] );
1401 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1402 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1403 $wanCache->getCheckKeyTime( 'zzz' );
1404 $wanCache->reap( 'x', time() - 300 );
1405 $wanCache->reap( 'zzz', time() - 300 );
1409 * @dataProvider provideAdaptiveTTL
1410 * @covers WANObjectCache::adaptiveTTL()
1411 * @param float|int $ago
1412 * @param int $maxTTL
1413 * @param int $minTTL
1414 * @param float $factor
1415 * @param int $adaptiveTTL
1417 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1418 $mtime = $ago ?
time() - $ago : $ago;
1420 $ttl = $this->cache
->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1422 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1423 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1425 $ttl = $this->cache
->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1427 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1428 $this->assertLessThanOrEqual( $adaptiveTTL +
$margin, $ttl );
1431 public static function provideAdaptiveTTL() {
1433 [ 3600, 900, 30, 0.2, 720 ],
1434 [ 3600, 500, 30, 0.2, 500 ],
1435 [ 3600, 86400, 800, 0.2, 800 ],
1436 [ false, 86400, 800, 0.2, 800 ],
1437 [ null, 86400, 800, 0.2, 800 ]
1442 * @covers WANObjectCache::__construct
1443 * @covers WANObjectCache::newEmpty
1445 public function testNewEmpty() {
1446 $this->assertInstanceOf(
1447 WANObjectCache
::class,
1448 WANObjectCache
::newEmpty()
1453 * @covers WANObjectCache::setLogger
1455 public function testSetLogger() {
1456 $this->assertSame( null, $this->cache
->setLogger( new Psr\Log\NullLogger
) );
1460 * @covers WANObjectCache::getQoS
1462 public function testGetQoS() {
1463 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1464 ->setMethods( [ 'getQoS' ] )->getMock();
1465 $backend->expects( $this->once() )->method( 'getQoS' )
1466 ->willReturn( BagOStuff
::QOS_UNKNOWN
);
1467 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1470 $wanCache::QOS_UNKNOWN
,
1471 $wanCache->getQoS( $wanCache::ATTR_EMULATION
)
1476 * @covers WANObjectCache::makeKey
1478 public function testMakeKey() {
1479 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1480 ->setMethods( [ 'makeKey' ] )->getMock();
1481 $backend->expects( $this->once() )->method( 'makeKey' )
1482 ->willReturn( 'special' );
1484 $wanCache = new WANObjectCache( [
1485 'cache' => $backend,
1486 'pool' => 'testcache-hash',
1487 'relayer' => new EventRelayerNull( [] )
1490 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1494 * @covers WANObjectCache::makeGlobalKey
1496 public function testMakeGlobalKey() {
1497 $backend = $this->getMockBuilder( HashBagOStuff
::class )
1498 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1499 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1500 ->willReturn( 'special' );
1502 $wanCache = new WANObjectCache( [
1503 'cache' => $backend,
1504 'pool' => 'testcache-hash',
1505 'relayer' => new EventRelayerNull( [] )
1508 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1511 public static function statsKeyProvider() {
1513 [ 'domain:page:5', 'page' ],
1514 [ 'domain:main-key', 'main-key' ],
1515 [ 'domain:page:history', 'page' ],
1516 [ 'missingdomainkey', 'missingdomainkey' ]
1521 * @dataProvider statsKeyProvider
1522 * @covers WANObjectCache::determineKeyClass
1524 public function testStatsKeyClass( $key, $class ) {
1525 $wanCache = TestingAccessWrapper
::newFromObject( new WANObjectCache( [
1526 'cache' => new HashBagOStuff
,
1527 'pool' => 'testcache-hash',
1528 'relayer' => new EventRelayerNull( [] )
1531 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1535 class TimeAdjustableHashBagOStuff
extends HashBagOStuff
{
1536 private $timeOverride = 0;
1538 public function setTime( $time ) {
1539 $this->timeOverride
= $time;
1542 protected function getCurrentTime() {
1543 return $this->timeOverride ?
: parent
::getCurrentTime();
1547 class TimeAdjustableWANObjectCache
extends WANObjectCache
{
1548 private $timeOverride = 0;
1550 public function setTime( $time ) {
1551 $this->timeOverride
= $time;
1552 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1553 $this->cache
->setTime( $time );
1557 public function addTime( $time ) {
1558 $this->timeOverride +
= $time;
1559 if ( $this->cache
instanceof TimeAdjustableHashBagOStuff
) {
1560 $this->cache
->setTime( $this->timeOverride
);
1563 return $this->timeOverride
;
1566 protected function getCurrentTime() {
1567 return $this->timeOverride ?
: parent
::getCurrentTime();
1571 class NearExpiringWANObjectCache
extends TimeAdjustableWANObjectCache
{
1572 const CLOCK_SKEW
= 1;
1574 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1575 return ( $curTTL > 0 && ( $curTTL + self
::CLOCK_SKEW
) < $lowTTL );
1579 class PopularityRefreshingWANObjectCache
extends TimeAdjustableWANObjectCache
{
1580 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1581 return ( ( $now - $asOf ) > $timeTillRefresh );