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