7053fc18babb458c4c1bc6ce0417b035dde10ecc
[lhc/web/wiklou.git] / tests / phpunit / includes / libs / objectcache / WANObjectCacheTest.php
1 <?php
2
3 use Wikimedia\TestingAccessWrapper;
4
5 /**
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
18 */
19 class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
20
21 use MediaWikiCoversValidator;
22
23 /** @var TimeAdjustableWANObjectCache */
24 private $cache;
25 /** @var BagOStuff */
26 private $internalCache;
27
28 protected function setUp() {
29 parent::setUp();
30
31 $this->cache = new TimeAdjustableWANObjectCache( [
32 'cache' => new TimeAdjustableHashBagOStuff(),
33 'pool' => 'testcache-hash',
34 'relayer' => new EventRelayerNull( [] )
35 ] );
36
37 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
38 /** @noinspection PhpUndefinedFieldInspection */
39 $this->internalCache = $wanCache->cache;
40 }
41
42 /**
43 * @dataProvider provideSetAndGet
44 * @covers WANObjectCache::set()
45 * @covers WANObjectCache::get()
46 * @covers WANObjectCache::makeKey()
47 * @param mixed $value
48 * @param int $ttl
49 */
50 public function testSetAndGet( $value, $ttl ) {
51 $curTTL = null;
52 $asOf = null;
53 $key = $this->cache->makeKey( 'x', wfRandomString() );
54
55 $this->cache->get( $key, $curTTL, [], $asOf );
56 $this->assertNull( $curTTL, "Current TTL is null" );
57 $this->assertNull( $asOf, "Current as-of-time is infinite" );
58
59 $t = microtime( true );
60 $this->cache->set( $key, $value, $ttl );
61
62 $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
63 if ( is_infinite( $ttl ) || $ttl == 0 ) {
64 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
65 } else {
66 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
67 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
68 }
69 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
70 $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
71 }
72
73 public static function provideSetAndGet() {
74 return [
75 [ 14141, 3 ],
76 [ 3535.666, 3 ],
77 [ [], 3 ],
78 [ null, 3 ],
79 [ '0', 3 ],
80 [ (object)[ 'meow' ], 3 ],
81 [ INF, 3 ],
82 [ '', 3 ],
83 [ 'pizzacat', INF ],
84 ];
85 }
86
87 /**
88 * @covers WANObjectCache::get()
89 * @covers WANObjectCache::makeGlobalKey()
90 */
91 public function testGetNotExists() {
92 $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
93 $curTTL = null;
94 $value = $this->cache->get( $key, $curTTL );
95
96 $this->assertFalse( $value, "Non-existing key has false value" );
97 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
98 }
99
100 /**
101 * @covers WANObjectCache::set()
102 */
103 public function testSetOver() {
104 $key = wfRandomString();
105 for ( $i = 0; $i < 3; ++$i ) {
106 $value = wfRandomString();
107 $this->cache->set( $key, $value, 3 );
108
109 $this->assertEquals( $this->cache->get( $key ), $value );
110 }
111 }
112
113 /**
114 * @covers WANObjectCache::set()
115 */
116 public function testStaleSet() {
117 $key = wfRandomString();
118 $value = wfRandomString();
119 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
120
121 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
122 }
123
124 public function testProcessCache() {
125 $hit = 0;
126 $callback = function () use ( &$hit ) {
127 ++$hit;
128 return 42;
129 };
130 $keys = [ wfRandomString(), wfRandomString(), wfRandomString() ];
131 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
132
133 foreach ( $keys as $i => $key ) {
134 $this->cache->getWithSetCallback(
135 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
136 }
137 $this->assertEquals( 3, $hit );
138
139 foreach ( $keys as $i => $key ) {
140 $this->cache->getWithSetCallback(
141 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
142 }
143 $this->assertEquals( 3, $hit, "Values cached" );
144
145 foreach ( $keys as $i => $key ) {
146 $this->cache->getWithSetCallback(
147 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
148 }
149 $this->assertEquals( 6, $hit );
150
151 foreach ( $keys as $i => $key ) {
152 $this->cache->getWithSetCallback(
153 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
154 }
155 $this->assertEquals( 6, $hit, "New values cached" );
156
157 foreach ( $keys as $i => $key ) {
158 $this->cache->delete( $key );
159 $this->cache->getWithSetCallback(
160 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
161 }
162 $this->assertEquals( 9, $hit, "Values evicted" );
163
164 $key = reset( $keys );
165 // Get into cache (default process cache group)
166 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
167 $this->assertEquals( 10, $hit, "Value calculated" );
168 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
169 $this->assertEquals( 10, $hit, "Value cached" );
170 $outerCallback = function () use ( &$callback, $key ) {
171 $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
172
173 return 43 + $v;
174 };
175 // Outer key misses and refuses inner key process cache value
176 $this->cache->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
177 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
178 }
179
180 /**
181 * @dataProvider getWithSetCallback_provider
182 * @covers WANObjectCache::getWithSetCallback()
183 * @covers WANObjectCache::doGetWithSetCallback()
184 * @param array $extOpts
185 * @param bool $versioned
186 */
187 public function testGetWithSetCallback( array $extOpts, $versioned ) {
188 $cache = $this->cache;
189
190 $key = wfRandomString();
191 $value = wfRandomString();
192 $cKey1 = wfRandomString();
193 $cKey2 = wfRandomString();
194
195 $priorValue = null;
196 $priorAsOf = null;
197 $wasSet = 0;
198 $func = function ( $old, &$ttl, &$opts, $asOf )
199 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
200 {
201 ++$wasSet;
202 $priorValue = $old;
203 $priorAsOf = $asOf;
204 $ttl = 20; // override with another value
205 return $value;
206 };
207
208 $wasSet = 0;
209 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
210 $this->assertEquals( $value, $v, "Value returned" );
211 $this->assertEquals( 1, $wasSet, "Value regenerated" );
212 $this->assertFalse( $priorValue, "No prior value" );
213 $this->assertNull( $priorAsOf, "No prior value" );
214
215 $curTTL = null;
216 $cache->get( $key, $curTTL );
217 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
218 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
219
220 $wasSet = 0;
221 $v = $cache->getWithSetCallback(
222 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
223 $this->assertEquals( $value, $v, "Value returned" );
224 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
225
226 $priorTime = microtime( true ); // reference time
227 $cache->setTime( $priorTime );
228
229 $cache->addTime( 1 );
230
231 $wasSet = 0;
232 $v = $cache->getWithSetCallback(
233 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
234 );
235 $this->assertEquals( $value, $v, "Value returned" );
236 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
237 $this->assertEquals( $value, $priorValue, "Has prior value" );
238 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
239 $t1 = $cache->getCheckKeyTime( $cKey1 );
240 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
241 $t2 = $cache->getCheckKeyTime( $cKey2 );
242 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
243
244 $priorTime = $cache->addTime( 0.01 );
245
246 $wasSet = 0;
247 $v = $cache->getWithSetCallback(
248 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
249 );
250 $this->assertEquals( $value, $v, "Value returned" );
251 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
252 $t1 = $cache->getCheckKeyTime( $cKey1 );
253 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
254 $t2 = $cache->getCheckKeyTime( $cKey2 );
255 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
256
257 $curTTL = null;
258 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
259 if ( $versioned ) {
260 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
261 } else {
262 $this->assertEquals( $value, $v, "Value returned" );
263 }
264 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
265
266 $wasSet = 0;
267 $key = wfRandomString();
268 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
269 $this->assertEquals( $value, $v, "Value returned" );
270 $cache->delete( $key );
271 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
272 $this->assertEquals( $value, $v, "Value still returned after deleted" );
273 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
274
275 $oldValReceived = -1;
276 $oldAsOfReceived = -1;
277 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
278 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
279 ++$wasSet;
280 $oldValReceived = $oldVal;
281 $oldAsOfReceived = $oldAsOf;
282
283 return 'xxx' . $wasSet;
284 };
285
286 $now = microtime( true ); // reference time
287
288 $wasSet = 0;
289 $key = wfRandomString();
290 $v = $cache->getWithSetCallback(
291 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
292 $this->assertEquals( 'xxx1', $v, "Value returned" );
293 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
294 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
295
296 $cache->addTime( 40 );
297 $v = $cache->getWithSetCallback(
298 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
299 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
300 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
301 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
302 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
303
304 $cache->addTime( 260 );
305 $v = $cache->getWithSetCallback(
306 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
307 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
308 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
309 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
310 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
311
312 $wasSet = 0;
313 $key = wfRandomString();
314 $checkKey = $cache->makeKey( 'template', 'X' );
315 $cache->setTime( $now - $cache::HOLDOFF_TTL - 1 );
316 $cache->touchCheckKey( $checkKey ); // init check key
317 $cache->setTime( $now );
318 $v = $cache->getWithSetCallback(
319 $key,
320 $cache::TTL_INDEFINITE,
321 $checkFunc,
322 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
323 );
324 $this->assertEquals( 'xxx1', $v, "Value returned" );
325 $this->assertEquals( 1, $wasSet, "Value computed" );
326 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
327 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
328
329 $cache->addTime( $cache::TTL_HOUR ); // some time passes
330 $v = $cache->getWithSetCallback(
331 $key,
332 $cache::TTL_INDEFINITE,
333 $checkFunc,
334 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
335 );
336 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
337 $this->assertEquals( 1, $wasSet, "Cached value returned" );
338
339 $cache->touchCheckKey( $checkKey ); // make key stale
340 $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
341
342 $v = $cache->getWithSetCallback(
343 $key,
344 $cache::TTL_INDEFINITE,
345 $checkFunc,
346 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
347 );
348 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
349 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
350
351 // Change of refresh increase to unity as staleness approaches graceTTL
352
353 $cache->addTime( $cache::TTL_WEEK ); // 8 days of being stale
354 $v = $cache->getWithSetCallback(
355 $key,
356 $cache::TTL_INDEFINITE,
357 $checkFunc,
358 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
359 );
360 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
361 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
362 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
363 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
364 }
365
366 public static function getWithSetCallback_provider() {
367 return [
368 [ [], false ],
369 [ [ 'version' => 1 ], true ]
370 ];
371 }
372
373 public function testPreemtiveRefresh() {
374 $value = 'KatCafe';
375 $wasSet = 0;
376 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
377 {
378 ++$wasSet;
379 return $value;
380 };
381
382 $cache = new NearExpiringWANObjectCache( [
383 'cache' => new HashBagOStuff(),
384 'pool' => 'empty',
385 ] );
386
387 $wasSet = 0;
388 $key = wfRandomString();
389 $opts = [ 'lowTTL' => 30 ];
390 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
391 $this->assertEquals( $value, $v, "Value returned" );
392 $this->assertEquals( 1, $wasSet, "Value calculated" );
393 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
394 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
395
396 $wasSet = 0;
397 $key = wfRandomString();
398 $opts = [ 'lowTTL' => 1 ];
399 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
400 $this->assertEquals( $value, $v, "Value returned" );
401 $this->assertEquals( 1, $wasSet, "Value calculated" );
402 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
403 $this->assertEquals( 1, $wasSet, "Value cached" );
404
405 $asycList = [];
406 $asyncHandler = function ( $callback ) use ( &$asycList ) {
407 $asycList[] = $callback;
408 };
409 $cache = new NearExpiringWANObjectCache( [
410 'cache' => new TimeAdjustableHashBagOStuff(),
411 'pool' => 'empty',
412 'asyncHandler' => $asyncHandler
413 ] );
414
415 $now = microtime( true ); // reference time
416 $cache->setTime( $now );
417
418 $wasSet = 0;
419 $key = wfRandomString();
420 $checkKey = wfRandomString();
421 $opts = [ 'lowTTL' => 100 ];
422 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
423 $this->assertEquals( $value, $v, "Value returned" );
424 $this->assertEquals( 1, $wasSet, "Value calculated" );
425 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
426 $this->assertEquals( 1, $wasSet, "Cached value used" );
427 $this->assertEquals( $v, $value, "Value cached" );
428
429 $cache->setTime( $now + 250 );
430 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
431 $this->assertEquals( $value, $v, "Value returned" );
432 $this->assertEquals( 1, $wasSet, "Stale value used" );
433 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
434 $value = 'NewCatsInTown'; // change callback return value
435 $asycList[0](); // run the refresh callback
436 $asycList = [];
437 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
438 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
439 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
440 $this->assertEquals( $value, $v, "New value stored" );
441
442 $cache = new PopularityRefreshingWANObjectCache( [
443 'cache' => new TimeAdjustableHashBagOStuff(),
444 'pool' => 'empty'
445 ] );
446
447 $cache->setTime( $now );
448
449 $wasSet = 0;
450 $key = wfRandomString();
451 $opts = [ 'hotTTR' => 900 ];
452 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
453 $this->assertEquals( $value, $v, "Value returned" );
454 $this->assertEquals( 1, $wasSet, "Value calculated" );
455 $cache->setTime( $now + 30 );
456 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
457 $this->assertEquals( 1, $wasSet, "Value cached" );
458
459 $wasSet = 0;
460 $key = wfRandomString();
461 $opts = [ 'hotTTR' => 10 ];
462 $cache->setTime( $now );
463 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
464 $this->assertEquals( $value, $v, "Value returned" );
465 $this->assertEquals( 1, $wasSet, "Value calculated" );
466 $cache->setTime( $now + 30 );
467 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
468 $this->assertEquals( $value, $v, "Value returned" );
469 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
470 }
471
472 /**
473 * @covers WANObjectCache::getWithSetCallback()
474 * @covers WANObjectCache::doGetWithSetCallback()
475 */
476 public function testGetWithSetCallback_invalidCallback() {
477 $this->setExpectedException( InvalidArgumentException::class );
478 $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
479 }
480
481 /**
482 * @dataProvider getMultiWithSetCallback_provider
483 * @covers WANObjectCache::getMultiWithSetCallback
484 * @covers WANObjectCache::makeMultiKeys
485 * @covers WANObjectCache::getMulti
486 * @param array $extOpts
487 * @param bool $versioned
488 */
489 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
490 $cache = $this->cache;
491
492 $keyA = wfRandomString();
493 $keyB = wfRandomString();
494 $keyC = wfRandomString();
495 $cKey1 = wfRandomString();
496 $cKey2 = wfRandomString();
497
498 $priorValue = null;
499 $priorAsOf = null;
500 $wasSet = 0;
501 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
502 &$wasSet, &$priorValue, &$priorAsOf
503 ) {
504 ++$wasSet;
505 $priorValue = $old;
506 $priorAsOf = $asOf;
507 $ttl = 20; // override with another value
508 return "@$id$";
509 };
510
511 $wasSet = 0;
512 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
513 $value = "@3353$";
514 $v = $cache->getMultiWithSetCallback(
515 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
516 $this->assertEquals( $value, $v[$keyA], "Value returned" );
517 $this->assertEquals( 1, $wasSet, "Value regenerated" );
518 $this->assertFalse( $priorValue, "No prior value" );
519 $this->assertNull( $priorAsOf, "No prior value" );
520
521 $curTTL = null;
522 $cache->get( $keyA, $curTTL );
523 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
524 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
525
526 $wasSet = 0;
527 $value = "@efef$";
528 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
529 $v = $cache->getMultiWithSetCallback(
530 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
531 $this->assertEquals( $value, $v[$keyB], "Value returned" );
532 $this->assertEquals( 1, $wasSet, "Value regenerated" );
533 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
534 $v = $cache->getMultiWithSetCallback(
535 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
536 $this->assertEquals( $value, $v[$keyB], "Value returned" );
537 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
538 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
539
540 $priorTime = microtime( true ); // reference time
541 $cache->setTime( $priorTime );
542
543 $cache->addTime( 1 );
544
545 $wasSet = 0;
546 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
547 $v = $cache->getMultiWithSetCallback(
548 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
549 );
550 $this->assertEquals( $value, $v[$keyB], "Value returned" );
551 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
552 $this->assertEquals( $value, $priorValue, "Has prior value" );
553 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
554 $t1 = $cache->getCheckKeyTime( $cKey1 );
555 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
556 $t2 = $cache->getCheckKeyTime( $cKey2 );
557 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
558
559 $priorTime = $cache->addTime( 0.01 );
560 $value = "@43636$";
561 $wasSet = 0;
562 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
563 $v = $cache->getMultiWithSetCallback(
564 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
565 );
566 $this->assertEquals( $value, $v[$keyC], "Value returned" );
567 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
568 $t1 = $cache->getCheckKeyTime( $cKey1 );
569 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
570 $t2 = $cache->getCheckKeyTime( $cKey2 );
571 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
572
573 $curTTL = null;
574 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
575 if ( $versioned ) {
576 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
577 } else {
578 $this->assertEquals( $value, $v, "Value returned" );
579 }
580 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
581
582 $wasSet = 0;
583 $key = wfRandomString();
584 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
585 $v = $cache->getMultiWithSetCallback(
586 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
587 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
588 $cache->delete( $key );
589 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
590 $v = $cache->getMultiWithSetCallback(
591 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
592 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
593 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
594
595 $calls = 0;
596 $ids = [ 1, 2, 3, 4, 5, 6 ];
597 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
598 return $wanCache->makeKey( 'test', $id );
599 };
600 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
601 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
602 ++$calls;
603
604 return "val-{$id}";
605 };
606 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
607
608 $this->assertEquals(
609 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
610 array_values( $values ),
611 "Correct values in correct order"
612 );
613 $this->assertEquals(
614 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
615 array_keys( $values ),
616 "Correct keys in correct order"
617 );
618 $this->assertEquals( count( $ids ), $calls );
619
620 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
621 $this->assertEquals( count( $ids ), $calls, "Values cached" );
622
623 // Mock the BagOStuff to assure only one getMulti() call given process caching
624 $localBag = $this->getMockBuilder( 'HashBagOStuff' )
625 ->setMethods( [ 'getMulti' ] )->getMock();
626 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
627 WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
628 WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
629 ] );
630 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
631
632 // Warm the process cache
633 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
634 $this->assertEquals(
635 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
636 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
637 );
638 // Use the process cache
639 $this->assertEquals(
640 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
641 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
642 );
643 }
644
645 public static function getMultiWithSetCallback_provider() {
646 return [
647 [ [], false ],
648 [ [ 'version' => 1 ], true ]
649 ];
650 }
651
652 /**
653 * @dataProvider getMultiWithUnionSetCallback_provider
654 * @covers WANObjectCache::getMultiWithUnionSetCallback()
655 * @covers WANObjectCache::makeMultiKeys()
656 * @param array $extOpts
657 * @param bool $versioned
658 */
659 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
660 $cache = $this->cache;
661
662 $keyA = wfRandomString();
663 $keyB = wfRandomString();
664 $keyC = wfRandomString();
665 $cKey1 = wfRandomString();
666 $cKey2 = wfRandomString();
667
668 $wasSet = 0;
669 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
670 &$wasSet, &$priorValue, &$priorAsOf
671 ) {
672 $newValues = [];
673 foreach ( $ids as $id ) {
674 ++$wasSet;
675 $newValues[$id] = "@$id$";
676 $ttls[$id] = 20; // override with another value
677 }
678
679 return $newValues;
680 };
681
682 $wasSet = 0;
683 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
684 $value = "@3353$";
685 $v = $cache->getMultiWithUnionSetCallback(
686 $keyedIds, 30, $genFunc, $extOpts );
687 $this->assertEquals( $value, $v[$keyA], "Value returned" );
688 $this->assertEquals( 1, $wasSet, "Value regenerated" );
689
690 $curTTL = null;
691 $cache->get( $keyA, $curTTL );
692 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
693 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
694
695 $wasSet = 0;
696 $value = "@efef$";
697 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
698 $v = $cache->getMultiWithUnionSetCallback(
699 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
700 $this->assertEquals( $value, $v[$keyB], "Value returned" );
701 $this->assertEquals( 1, $wasSet, "Value regenerated" );
702 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
703 $v = $cache->getMultiWithUnionSetCallback(
704 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
705 $this->assertEquals( $value, $v[$keyB], "Value returned" );
706 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
707 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
708
709 $priorTime = microtime( true ); // reference time
710 $cache->setTime( $priorTime );
711
712 $cache->addTime( 1 );
713
714 $wasSet = 0;
715 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
716 $v = $cache->getMultiWithUnionSetCallback(
717 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
718 );
719 $this->assertEquals( $value, $v[$keyB], "Value returned" );
720 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
721 $t1 = $cache->getCheckKeyTime( $cKey1 );
722 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
723 $t2 = $cache->getCheckKeyTime( $cKey2 );
724 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
725
726 $priorTime = $cache->addTime( 0.01 );
727 $value = "@43636$";
728 $wasSet = 0;
729 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
730 $v = $cache->getMultiWithUnionSetCallback(
731 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
732 );
733 $this->assertEquals( $value, $v[$keyC], "Value returned" );
734 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
735 $t1 = $cache->getCheckKeyTime( $cKey1 );
736 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
737 $t2 = $cache->getCheckKeyTime( $cKey2 );
738 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
739
740 $curTTL = null;
741 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
742 if ( $versioned ) {
743 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
744 } else {
745 $this->assertEquals( $value, $v, "Value returned" );
746 }
747 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
748
749 $wasSet = 0;
750 $key = wfRandomString();
751 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
752 $v = $cache->getMultiWithUnionSetCallback(
753 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
754 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
755 $cache->delete( $key );
756 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
757 $v = $cache->getMultiWithUnionSetCallback(
758 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
759 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
760 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
761
762 $calls = 0;
763 $ids = [ 1, 2, 3, 4, 5, 6 ];
764 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
765 return $wanCache->makeKey( 'test', $id );
766 };
767 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
768 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
769 $newValues = [];
770 foreach ( $ids as $id ) {
771 ++$calls;
772 $newValues[$id] = "val-{$id}";
773 }
774
775 return $newValues;
776 };
777 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
778
779 $this->assertEquals(
780 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
781 array_values( $values ),
782 "Correct values in correct order"
783 );
784 $this->assertEquals(
785 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
786 array_keys( $values ),
787 "Correct keys in correct order"
788 );
789 $this->assertEquals( count( $ids ), $calls );
790
791 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
792 $this->assertEquals( count( $ids ), $calls, "Values cached" );
793 }
794
795 public static function getMultiWithUnionSetCallback_provider() {
796 return [
797 [ [], false ],
798 [ [ 'version' => 1 ], true ]
799 ];
800 }
801
802 /**
803 * @covers WANObjectCache::getWithSetCallback()
804 * @covers WANObjectCache::doGetWithSetCallback()
805 */
806 public function testLockTSE() {
807 $cache = $this->cache;
808 $key = wfRandomString();
809 $value = wfRandomString();
810
811 $calls = 0;
812 $func = function () use ( &$calls, $value, $cache, $key ) {
813 ++$calls;
814 return $value;
815 };
816
817 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
818 $this->assertEquals( $value, $ret );
819 $this->assertEquals( 1, $calls, 'Value was populated' );
820
821 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
822 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
823
824 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
825 $ret = $cache->getWithSetCallback( $key, 30, $func,
826 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
827 $this->assertEquals( $value, $ret, 'Old value used' );
828 $this->assertEquals( 1, $calls, 'Callback was not used' );
829
830 $cache->delete( $key );
831 $ret = $cache->getWithSetCallback( $key, 30, $func,
832 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
833 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
834 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
835
836 $ret = $cache->getWithSetCallback( $key, 30, $func,
837 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
838 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
839 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
840 }
841
842 /**
843 * @covers WANObjectCache::getWithSetCallback()
844 * @covers WANObjectCache::doGetWithSetCallback()
845 * @covers WANObjectCache::set()
846 */
847 public function testLockTSESlow() {
848 $cache = $this->cache;
849 $key = wfRandomString();
850 $value = wfRandomString();
851
852 $calls = 0;
853 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
854 ++$calls;
855 $setOpts['since'] = microtime( true ) - 10;
856 // Immediately kill any mutex rather than waiting a second
857 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
858 return $value;
859 };
860
861 // Value should be marked as stale due to snapshot lag
862 $curTTL = null;
863 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
864 $this->assertEquals( $value, $ret );
865 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
866 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
867 $this->assertEquals( 1, $calls, 'Value was generated' );
868
869 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
870 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
871 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
872 $this->assertEquals( $value, $ret );
873 $this->assertEquals( 1, $calls, 'Callback was not used' );
874 }
875
876 /**
877 * @covers WANObjectCache::getWithSetCallback()
878 * @covers WANObjectCache::doGetWithSetCallback()
879 */
880 public function testBusyValue() {
881 $cache = $this->cache;
882 $key = wfRandomString();
883 $value = wfRandomString();
884 $busyValue = wfRandomString();
885
886 $calls = 0;
887 $func = function () use ( &$calls, $value, $cache, $key ) {
888 ++$calls;
889 // Immediately kill any mutex rather than waiting a second
890 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
891 return $value;
892 };
893
894 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
895 $this->assertEquals( $value, $ret );
896 $this->assertEquals( 1, $calls, 'Value was populated' );
897
898 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
899 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
900
901 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
902 $ret = $cache->getWithSetCallback( $key, 30, $func,
903 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
904 $this->assertEquals( $value, $ret, 'Callback used' );
905 $this->assertEquals( 2, $calls, 'Callback used' );
906
907 $ret = $cache->getWithSetCallback( $key, 30, $func,
908 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
909 $this->assertEquals( $value, $ret, 'Old value used' );
910 $this->assertEquals( 2, $calls, 'Callback was not used' );
911
912 $cache->delete( $key ); // no value at all anymore and still locked
913 $ret = $cache->getWithSetCallback( $key, 30, $func,
914 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
915 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
916 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
917
918 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
919 $ret = $cache->getWithSetCallback( $key, 30, $func,
920 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
921 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
922 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
923
924 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
925 $ret = $cache->getWithSetCallback( $key, 30, $func,
926 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
927 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
928 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
929 }
930
931 /**
932 * @covers WANObjectCache::getMulti()
933 */
934 public function testGetMulti() {
935 $cache = $this->cache;
936
937 $value1 = [ 'this' => 'is', 'a' => 'test' ];
938 $value2 = [ 'this' => 'is', 'another' => 'test' ];
939
940 $key1 = wfRandomString();
941 $key2 = wfRandomString();
942 $key3 = wfRandomString();
943
944 $cache->set( $key1, $value1, 5 );
945 $cache->set( $key2, $value2, 10 );
946
947 $curTTLs = [];
948 $this->assertEquals(
949 [ $key1 => $value1, $key2 => $value2 ],
950 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
951 'Result array populated'
952 );
953
954 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
955 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
956 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
957
958 $cKey1 = wfRandomString();
959 $cKey2 = wfRandomString();
960
961 $priorTime = microtime( true ); // reference time
962 $cache->setTime( $priorTime );
963
964 $cache->addTime( 1 );
965
966 $curTTLs = [];
967 $this->assertEquals(
968 [ $key1 => $value1, $key2 => $value2 ],
969 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
970 "Result array populated even with new check keys"
971 );
972 $t1 = $cache->getCheckKeyTime( $cKey1 );
973 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
974 $t2 = $cache->getCheckKeyTime( $cKey2 );
975 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
976 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
977 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
978 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
979
980 $cache->addTime( 1 );
981
982 $curTTLs = [];
983 $this->assertEquals(
984 [ $key1 => $value1, $key2 => $value2 ],
985 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
986 "Result array still populated even with new check keys"
987 );
988 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
989 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
990 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
991 }
992
993 /**
994 * @covers WANObjectCache::getMulti()
995 * @covers WANObjectCache::processCheckKeys()
996 */
997 public function testGetMultiCheckKeys() {
998 $cache = $this->cache;
999
1000 $checkAll = wfRandomString();
1001 $check1 = wfRandomString();
1002 $check2 = wfRandomString();
1003 $check3 = wfRandomString();
1004 $value1 = wfRandomString();
1005 $value2 = wfRandomString();
1006
1007 $cache->setTime( microtime( true ) );
1008
1009 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1010 // several seconds during the test to assert the behaviour.
1011 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1012 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
1013 }
1014
1015 $cache->addTime( 0.100 );
1016
1017 $cache->set( 'key1', $value1, 10 );
1018 $cache->set( 'key2', $value2, 10 );
1019
1020 $curTTLs = [];
1021 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1022 'key1' => $check1,
1023 $checkAll,
1024 'key2' => $check2,
1025 'key3' => $check3,
1026 ] );
1027 $this->assertEquals(
1028 [ 'key1' => $value1, 'key2' => $value2 ],
1029 $result,
1030 'Initial values'
1031 );
1032 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1033 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1034 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1035 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1036
1037 $cache->addTime( 0.100 );
1038 $cache->touchCheckKey( $check1 );
1039
1040 $curTTLs = [];
1041 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1042 'key1' => $check1,
1043 $checkAll,
1044 'key2' => $check2,
1045 'key3' => $check3,
1046 ] );
1047 $this->assertEquals(
1048 [ 'key1' => $value1, 'key2' => $value2 ],
1049 $result,
1050 'key1 expired by check1, but value still provided'
1051 );
1052 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1053 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1054
1055 $cache->touchCheckKey( $checkAll );
1056
1057 $curTTLs = [];
1058 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1059 'key1' => $check1,
1060 $checkAll,
1061 'key2' => $check2,
1062 'key3' => $check3,
1063 ] );
1064 $this->assertEquals(
1065 [ 'key1' => $value1, 'key2' => $value2 ],
1066 $result,
1067 'All keys expired by checkAll, but value still provided'
1068 );
1069 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1070 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1071 }
1072
1073 /**
1074 * @covers WANObjectCache::get()
1075 * @covers WANObjectCache::processCheckKeys()
1076 */
1077 public function testCheckKeyInitHoldoff() {
1078 $cache = $this->cache;
1079
1080 for ( $i = 0; $i < 500; ++$i ) {
1081 $key = wfRandomString();
1082 $checkKey = wfRandomString();
1083 // miss, set, hit
1084 $cache->get( $key, $curTTL, [ $checkKey ] );
1085 $cache->set( $key, 'val', 10 );
1086 $curTTL = null;
1087 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1088
1089 $this->assertEquals( 'val', $v );
1090 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1091 }
1092
1093 for ( $i = 0; $i < 500; ++$i ) {
1094 $key = wfRandomString();
1095 $checkKey = wfRandomString();
1096 // set, hit
1097 $cache->set( $key, 'val', 10 );
1098 $curTTL = null;
1099 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1100
1101 $this->assertEquals( 'val', $v );
1102 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1103 }
1104 }
1105
1106 /**
1107 * @covers WANObjectCache::delete
1108 * @covers WANObjectCache::relayDelete
1109 * @covers WANObjectCache::relayPurge
1110 */
1111 public function testDelete() {
1112 $key = wfRandomString();
1113 $value = wfRandomString();
1114 $this->cache->set( $key, $value );
1115
1116 $curTTL = null;
1117 $v = $this->cache->get( $key, $curTTL );
1118 $this->assertEquals( $value, $v, "Key was created with value" );
1119 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1120
1121 $this->cache->delete( $key );
1122
1123 $curTTL = null;
1124 $v = $this->cache->get( $key, $curTTL );
1125 $this->assertFalse( $v, "Deleted key has false value" );
1126 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1127
1128 $this->cache->set( $key, $value . 'more' );
1129 $v = $this->cache->get( $key, $curTTL );
1130 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1131 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1132
1133 $this->cache->set( $key, $value );
1134 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1135
1136 $curTTL = null;
1137 $v = $this->cache->get( $key, $curTTL );
1138 $this->assertFalse( $v, "Deleted key has false value" );
1139 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1140
1141 $this->cache->set( $key, $value );
1142 $v = $this->cache->get( $key, $curTTL );
1143 $this->assertEquals( $value, $v, "Key was created with value" );
1144 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1145 }
1146
1147 /**
1148 * @dataProvider getWithSetCallback_versions_provider
1149 * @covers WANObjectCache::getWithSetCallback()
1150 * @covers WANObjectCache::doGetWithSetCallback()
1151 * @param array $extOpts
1152 * @param bool $versioned
1153 */
1154 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1155 $cache = $this->cache;
1156
1157 $key = wfRandomString();
1158 $valueV1 = wfRandomString();
1159 $valueV2 = [ wfRandomString() ];
1160
1161 $wasSet = 0;
1162 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1163 ++$wasSet;
1164
1165 return $valueV1;
1166 };
1167
1168 $priorValue = false;
1169 $priorAsOf = null;
1170 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1171 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1172 $priorValue = $oldValue;
1173 $priorAsOf = $oldAsOf;
1174 ++$wasSet;
1175
1176 return $valueV2; // new array format
1177 };
1178
1179 // Set the main key (version N if versioned)
1180 $wasSet = 0;
1181 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1182 $this->assertEquals( $valueV1, $v, "Value returned" );
1183 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1184 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1185 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1186 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1187
1188 if ( $versioned ) {
1189 // Set the key for version N+1 format
1190 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1191 } else {
1192 // Start versioning now with the unversioned key still there
1193 $verOpts = [ 'version' => 1 ];
1194 }
1195
1196 // Value goes to secondary key since V1 already used $key
1197 $wasSet = 0;
1198 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1199 $this->assertEquals( $valueV2, $v, "Value returned" );
1200 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1201 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1202 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1203
1204 $wasSet = 0;
1205 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1206 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1207 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1208
1209 // Clear out the older or unversioned key
1210 $cache->delete( $key, 0 );
1211
1212 // Set the key for next/first versioned format
1213 $wasSet = 0;
1214 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1215 $this->assertEquals( $valueV2, $v, "Value returned" );
1216 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1217
1218 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1219 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1220 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1221 }
1222
1223 public static function getWithSetCallback_versions_provider() {
1224 return [
1225 [ [], false ],
1226 [ [ 'version' => 1 ], true ]
1227 ];
1228 }
1229
1230 /**
1231 * @covers WANObjectCache::useInterimHoldOffCaching
1232 * @covers WANObjectCache::getInterimValue
1233 */
1234 public function testInterimHoldOffCaching() {
1235 $cache = $this->cache;
1236
1237 $value = 'CRL-40-940';
1238 $wasCalled = 0;
1239 $func = function () use ( &$wasCalled, $value ) {
1240 $wasCalled++;
1241
1242 return $value;
1243 };
1244
1245 $cache->useInterimHoldOffCaching( true );
1246
1247 $key = wfRandomString( 32 );
1248 $v = $cache->getWithSetCallback( $key, 60, $func );
1249 $v = $cache->getWithSetCallback( $key, 60, $func );
1250 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1251 $cache->delete( $key );
1252 $v = $cache->getWithSetCallback( $key, 60, $func );
1253 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1254 $v = $cache->getWithSetCallback( $key, 60, $func );
1255 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1256 // Lock up the mutex so interim cache is used
1257 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1258 $v = $cache->getWithSetCallback( $key, 60, $func );
1259 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1260 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
1261
1262 $cache->useInterimHoldOffCaching( false );
1263
1264 $wasCalled = 0;
1265 $key = wfRandomString( 32 );
1266 $v = $cache->getWithSetCallback( $key, 60, $func );
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1269 $cache->delete( $key );
1270 $v = $cache->getWithSetCallback( $key, 60, $func );
1271 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1272 $v = $cache->getWithSetCallback( $key, 60, $func );
1273 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1274 $v = $cache->getWithSetCallback( $key, 60, $func );
1275 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1276 // Lock up the mutex so interim cache is used
1277 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1278 $v = $cache->getWithSetCallback( $key, 60, $func );
1279 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1280 }
1281
1282 /**
1283 * @covers WANObjectCache::touchCheckKey
1284 * @covers WANObjectCache::resetCheckKey
1285 * @covers WANObjectCache::getCheckKeyTime
1286 * @covers WANObjectCache::getMultiCheckKeyTime
1287 * @covers WANObjectCache::makePurgeValue
1288 * @covers WANObjectCache::parsePurgeValue
1289 */
1290 public function testTouchKeys() {
1291 $cache = $this->cache;
1292 $key = wfRandomString();
1293
1294 $priorTime = microtime( true ); // reference time
1295 $cache->setTime( $priorTime );
1296
1297 $newTime = $cache->addTime( 0.100 );
1298 $t0 = $cache->getCheckKeyTime( $key );
1299 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1300
1301 $priorTime = $newTime;
1302 $newTime = $cache->addTime( 0.100 );
1303 $cache->touchCheckKey( $key );
1304 $t1 = $cache->getCheckKeyTime( $key );
1305 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1306
1307 $t2 = $cache->getCheckKeyTime( $key );
1308 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1309
1310 $cache->addTime( 0.100 );
1311 $cache->touchCheckKey( $key );
1312 $t3 = $cache->getCheckKeyTime( $key );
1313 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1314
1315 $t4 = $cache->getCheckKeyTime( $key );
1316 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1317
1318 $cache->addTime( 0.100 );
1319 $cache->resetCheckKey( $key );
1320 $t5 = $cache->getCheckKeyTime( $key );
1321 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1322
1323 $t6 = $cache->getCheckKeyTime( $key );
1324 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1325 }
1326
1327 /**
1328 * @covers WANObjectCache::getMulti()
1329 */
1330 public function testGetWithSeveralCheckKeys() {
1331 $key = wfRandomString();
1332 $tKey1 = wfRandomString();
1333 $tKey2 = wfRandomString();
1334 $value = 'meow';
1335
1336 // Two check keys are newer (given hold-off) than $key, another is older
1337 $this->internalCache->set(
1338 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1339 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
1340 );
1341 $this->internalCache->set(
1342 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1343 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
1344 );
1345 $this->internalCache->set(
1346 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1347 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
1348 );
1349 $this->cache->set( $key, $value, 30 );
1350
1351 $curTTL = null;
1352 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1353 $this->assertEquals( $value, $v, "Value matches" );
1354 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1355 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1356 }
1357
1358 /**
1359 * @covers WANObjectCache::reap()
1360 * @covers WANObjectCache::reapCheckKey()
1361 */
1362 public function testReap() {
1363 $vKey1 = wfRandomString();
1364 $vKey2 = wfRandomString();
1365 $tKey1 = wfRandomString();
1366 $tKey2 = wfRandomString();
1367 $value = 'moo';
1368
1369 $knownPurge = time() - 60;
1370 $goodTime = microtime( true ) - 5;
1371 $badTime = microtime( true ) - 300;
1372
1373 $this->internalCache->set(
1374 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
1375 [
1376 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1377 WANObjectCache::FLD_VALUE => $value,
1378 WANObjectCache::FLD_TTL => 3600,
1379 WANObjectCache::FLD_TIME => $goodTime
1380 ]
1381 );
1382 $this->internalCache->set(
1383 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
1384 [
1385 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1386 WANObjectCache::FLD_VALUE => $value,
1387 WANObjectCache::FLD_TTL => 3600,
1388 WANObjectCache::FLD_TIME => $badTime
1389 ]
1390 );
1391 $this->internalCache->set(
1392 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1393 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
1394 );
1395 $this->internalCache->set(
1396 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1397 WANObjectCache::PURGE_VAL_PREFIX . $badTime
1398 );
1399
1400 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1401 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1402 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1403 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1404
1405 $this->assertFalse( $bad1 );
1406 $this->assertTrue( $bad2 );
1407
1408 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1409 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1410 $this->assertFalse( $tBad1 );
1411 $this->assertTrue( $tBad2 );
1412 }
1413
1414 /**
1415 * @covers WANObjectCache::reap()
1416 */
1417 public function testReap_fail() {
1418 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1419 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1420 $backend->expects( $this->once() )->method( 'get' )
1421 ->willReturn( [
1422 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1423 WANObjectCache::FLD_VALUE => 'value',
1424 WANObjectCache::FLD_TTL => 3600,
1425 WANObjectCache::FLD_TIME => 300,
1426 ] );
1427 $backend->expects( $this->once() )->method( 'changeTTL' )
1428 ->willReturn( false );
1429
1430 $wanCache = new WANObjectCache( [
1431 'cache' => $backend,
1432 'pool' => 'testcache-hash',
1433 'relayer' => new EventRelayerNull( [] )
1434 ] );
1435
1436 $isStale = null;
1437 $ret = $wanCache->reap( 'key', 360, $isStale );
1438 $this->assertTrue( $isStale, 'value was stale' );
1439 $this->assertFalse( $ret, 'changeTTL failed' );
1440 }
1441
1442 /**
1443 * @covers WANObjectCache::set()
1444 */
1445 public function testSetWithLag() {
1446 $value = 1;
1447
1448 $key = wfRandomString();
1449 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1450 $this->cache->set( $key, $value, 30, $opts );
1451 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1452
1453 $key = wfRandomString();
1454 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1455 $this->cache->set( $key, $value, 30, $opts );
1456 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1457
1458 $key = wfRandomString();
1459 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1460 $this->cache->set( $key, $value, 30, $opts );
1461 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1462 }
1463
1464 /**
1465 * @covers WANObjectCache::set()
1466 */
1467 public function testWritePending() {
1468 $value = 1;
1469
1470 $key = wfRandomString();
1471 $opts = [ 'pending' => true ];
1472 $this->cache->set( $key, $value, 30, $opts );
1473 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1474 }
1475
1476 public function testMcRouterSupport() {
1477 $localBag = $this->getMockBuilder( 'EmptyBagOStuff' )
1478 ->setMethods( [ 'set', 'delete' ] )->getMock();
1479 $localBag->expects( $this->never() )->method( 'set' );
1480 $localBag->expects( $this->never() )->method( 'delete' );
1481 $wanCache = new WANObjectCache( [
1482 'cache' => $localBag,
1483 'pool' => 'testcache-hash',
1484 'relayer' => new EventRelayerNull( [] )
1485 ] );
1486 $valFunc = function () {
1487 return 1;
1488 };
1489
1490 // None of these should use broadcasting commands (e.g. SET, DELETE)
1491 $wanCache->get( 'x' );
1492 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1493 $wanCache->getMulti( [ 'x', 'y' ] );
1494 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1495 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1496 $wanCache->getCheckKeyTime( 'zzz' );
1497 $wanCache->reap( 'x', time() - 300 );
1498 $wanCache->reap( 'zzz', time() - 300 );
1499 }
1500
1501 /**
1502 * @dataProvider provideAdaptiveTTL
1503 * @covers WANObjectCache::adaptiveTTL()
1504 * @param float|int $ago
1505 * @param int $maxTTL
1506 * @param int $minTTL
1507 * @param float $factor
1508 * @param int $adaptiveTTL
1509 */
1510 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1511 $mtime = $ago ? time() - $ago : $ago;
1512 $margin = 5;
1513 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1514
1515 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1516 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1517
1518 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1519
1520 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1521 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1522 }
1523
1524 public static function provideAdaptiveTTL() {
1525 return [
1526 [ 3600, 900, 30, 0.2, 720 ],
1527 [ 3600, 500, 30, 0.2, 500 ],
1528 [ 3600, 86400, 800, 0.2, 800 ],
1529 [ false, 86400, 800, 0.2, 800 ],
1530 [ null, 86400, 800, 0.2, 800 ]
1531 ];
1532 }
1533
1534 /**
1535 * @covers WANObjectCache::__construct
1536 * @covers WANObjectCache::newEmpty
1537 */
1538 public function testNewEmpty() {
1539 $this->assertInstanceOf(
1540 WANObjectCache::class,
1541 WANObjectCache::newEmpty()
1542 );
1543 }
1544
1545 /**
1546 * @covers WANObjectCache::setLogger
1547 */
1548 public function testSetLogger() {
1549 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1550 }
1551
1552 /**
1553 * @covers WANObjectCache::getQoS
1554 */
1555 public function testGetQoS() {
1556 $backend = $this->getMockBuilder( HashBagOStuff::class )
1557 ->setMethods( [ 'getQoS' ] )->getMock();
1558 $backend->expects( $this->once() )->method( 'getQoS' )
1559 ->willReturn( BagOStuff::QOS_UNKNOWN );
1560 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1561
1562 $this->assertSame(
1563 $wanCache::QOS_UNKNOWN,
1564 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1565 );
1566 }
1567
1568 /**
1569 * @covers WANObjectCache::makeKey
1570 */
1571 public function testMakeKey() {
1572 $backend = $this->getMockBuilder( HashBagOStuff::class )
1573 ->setMethods( [ 'makeKey' ] )->getMock();
1574 $backend->expects( $this->once() )->method( 'makeKey' )
1575 ->willReturn( 'special' );
1576
1577 $wanCache = new WANObjectCache( [
1578 'cache' => $backend,
1579 'pool' => 'testcache-hash',
1580 'relayer' => new EventRelayerNull( [] )
1581 ] );
1582
1583 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1584 }
1585
1586 /**
1587 * @covers WANObjectCache::makeGlobalKey
1588 */
1589 public function testMakeGlobalKey() {
1590 $backend = $this->getMockBuilder( HashBagOStuff::class )
1591 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1592 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1593 ->willReturn( 'special' );
1594
1595 $wanCache = new WANObjectCache( [
1596 'cache' => $backend,
1597 'pool' => 'testcache-hash',
1598 'relayer' => new EventRelayerNull( [] )
1599 ] );
1600
1601 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1602 }
1603
1604 public static function statsKeyProvider() {
1605 return [
1606 [ 'domain:page:5', 'page' ],
1607 [ 'domain:main-key', 'main-key' ],
1608 [ 'domain:page:history', 'page' ],
1609 [ 'missingdomainkey', 'missingdomainkey' ]
1610 ];
1611 }
1612
1613 /**
1614 * @dataProvider statsKeyProvider
1615 * @covers WANObjectCache::determineKeyClass
1616 */
1617 public function testStatsKeyClass( $key, $class ) {
1618 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1619 'cache' => new HashBagOStuff,
1620 'pool' => 'testcache-hash',
1621 'relayer' => new EventRelayerNull( [] )
1622 ] ) );
1623
1624 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1625 }
1626 }
1627
1628 class TimeAdjustableHashBagOStuff extends HashBagOStuff {
1629 private $timeOverride = 0;
1630
1631 public function setTime( $time ) {
1632 $this->timeOverride = $time;
1633 }
1634
1635 protected function getCurrentTime() {
1636 return $this->timeOverride ?: parent::getCurrentTime();
1637 }
1638 }
1639
1640 class TimeAdjustableWANObjectCache extends WANObjectCache {
1641 private $timeOverride = 0;
1642
1643 public function setTime( $time ) {
1644 $this->timeOverride = $time;
1645 if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1646 $this->cache->setTime( $time );
1647 }
1648 }
1649
1650 public function addTime( $time ) {
1651 $this->timeOverride += $time;
1652 if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1653 $this->cache->setTime( $this->timeOverride );
1654 }
1655
1656 return $this->timeOverride;
1657 }
1658
1659 protected function getCurrentTime() {
1660 return $this->timeOverride ?: parent::getCurrentTime();
1661 }
1662 }
1663
1664 class NearExpiringWANObjectCache extends TimeAdjustableWANObjectCache {
1665 const CLOCK_SKEW = 1;
1666
1667 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1668 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1669 }
1670 }
1671
1672 class PopularityRefreshingWANObjectCache extends TimeAdjustableWANObjectCache {
1673 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1674 return ( ( $now - $asOf ) > $timeTillRefresh );
1675 }
1676 }