MediaWiki REL1_32
WANObjectCacheTest.php
Go to the documentation of this file.
1<?php
2
3use Wikimedia\TestingAccessWrapper;
4
19class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
20
21 use MediaWikiCoversValidator;
22 use PHPUnit4And6Compat;
23
25 private $cache;
28
29 protected function setUp() {
30 parent::setUp();
31
32 $this->cache = new WANObjectCache( [
33 'cache' => new HashBagOStuff(),
34 'pool' => 'testcache-hash',
35 'relayer' => new EventRelayerNull( [] )
36 ] );
37
38 $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
40 $this->internalCache = $wanCache->cache;
41 }
42
51 public function testSetAndGet( $value, $ttl ) {
52 $curTTL = null;
53 $asOf = null;
54 $key = $this->cache->makeKey( 'x', wfRandomString() );
55
56 $this->cache->get( $key, $curTTL, [], $asOf );
57 $this->assertNull( $curTTL, "Current TTL is null" );
58 $this->assertNull( $asOf, "Current as-of-time is infinite" );
59
60 $t = microtime( true );
61 $this->cache->set( $key, $value, $ttl );
62
63 $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
64 if ( is_infinite( $ttl ) || $ttl == 0 ) {
65 $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
66 } else {
67 $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
68 $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
69 }
70 $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
71 $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
72 }
73
74 public static function provideSetAndGet() {
75 return [
76 [ 14141, 3 ],
77 [ 3535.666, 3 ],
78 [ [], 3 ],
79 [ null, 3 ],
80 [ '0', 3 ],
81 [ (object)[ 'meow' ], 3 ],
82 [ INF, 3 ],
83 [ '', 3 ],
84 [ 'pizzacat', INF ],
85 ];
86 }
87
92 public function testGetNotExists() {
93 $key = $this->cache->makeGlobalKey( 'y', wfRandomString(), 'p' );
94 $curTTL = null;
95 $value = $this->cache->get( $key, $curTTL );
96
97 $this->assertFalse( $value, "Non-existing key has false value" );
98 $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
99 }
100
104 public function testSetOver() {
105 $key = wfRandomString();
106 for ( $i = 0; $i < 3; ++$i ) {
108 $this->cache->set( $key, $value, 3 );
109
110 $this->assertEquals( $this->cache->get( $key ), $value );
111 }
112 }
113
117 public function testStaleSet() {
118 $key = wfRandomString();
120 $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
121
122 $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
123 }
124
125 public function testProcessCache() {
126 $hit = 0;
127 $callback = function () use ( &$hit ) {
128 ++$hit;
129 return 42;
130 };
132 $groups = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ];
133
134 foreach ( $keys as $i => $key ) {
135 $this->cache->getWithSetCallback(
136 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
137 }
138 $this->assertEquals( 3, $hit );
139
140 foreach ( $keys as $i => $key ) {
141 $this->cache->getWithSetCallback(
142 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
143 }
144 $this->assertEquals( 3, $hit, "Values cached" );
145
146 foreach ( $keys as $i => $key ) {
147 $this->cache->getWithSetCallback(
148 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
149 }
150 $this->assertEquals( 6, $hit );
151
152 foreach ( $keys as $i => $key ) {
153 $this->cache->getWithSetCallback(
154 "$key-2", 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
155 }
156 $this->assertEquals( 6, $hit, "New values cached" );
157
158 foreach ( $keys as $i => $key ) {
159 $this->cache->delete( $key );
160 $this->cache->getWithSetCallback(
161 $key, 100, $callback, [ 'pcTTL' => 5, 'pcGroup' => $groups[$i] ] );
162 }
163 $this->assertEquals( 9, $hit, "Values evicted" );
164
165 $key = reset( $keys );
166 // Get into cache (default process cache group)
167 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
168 $this->assertEquals( 10, $hit, "Value calculated" );
169 $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
170 $this->assertEquals( 10, $hit, "Value cached" );
171 $outerCallback = function () use ( &$callback, $key ) {
172 $v = $this->cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] );
173
174 return 43 + $v;
175 };
176 // Outer key misses and refuses inner key process cache value
177 $this->cache->getWithSetCallback( "$key-miss-outer", 100, $outerCallback );
178 $this->assertEquals( 11, $hit, "Nested callback value process cache skipped" );
179 }
180
188 public function testGetWithSetCallback( array $extOpts, $versioned ) {
190
191 $key = wfRandomString();
193 $cKey1 = wfRandomString();
194 $cKey2 = wfRandomString();
195
196 $priorValue = null;
197 $priorAsOf = null;
198 $wasSet = 0;
199 $func = function ( $old, &$ttl, &$opts, $asOf )
200 use ( &$wasSet, &$priorValue, &$priorAsOf, $value )
201 {
202 ++$wasSet;
203 $priorValue = $old;
204 $priorAsOf = $asOf;
205 $ttl = 20; // override with another value
206 return $value;
207 };
208
209 $wasSet = 0;
210 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
211 $this->assertEquals( $value, $v, "Value returned" );
212 $this->assertEquals( 1, $wasSet, "Value regenerated" );
213 $this->assertFalse( $priorValue, "No prior value" );
214 $this->assertNull( $priorAsOf, "No prior value" );
215
216 $curTTL = null;
217 $cache->get( $key, $curTTL );
218 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
219 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
220
221 $wasSet = 0;
223 $key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts );
224 $this->assertEquals( $value, $v, "Value returned" );
225 $this->assertEquals( 0, $wasSet, "Value not regenerated" );
226
227 $mockWallClock = microtime( true );
228 $priorTime = $mockWallClock; // reference time
229 $cache->setMockTime( $mockWallClock );
230
231 $mockWallClock += 1;
232
233 $wasSet = 0;
235 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
236 );
237 $this->assertEquals( $value, $v, "Value returned" );
238 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
239 $this->assertEquals( $value, $priorValue, "Has prior value" );
240 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
241 $t1 = $cache->getCheckKeyTime( $cKey1 );
242 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
243 $t2 = $cache->getCheckKeyTime( $cKey2 );
244 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
245
246 $mockWallClock += 0.01;
247 $priorTime = $mockWallClock; // reference time
248 $wasSet = 0;
250 $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
251 );
252 $this->assertEquals( $value, $v, "Value returned" );
253 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
254 $t1 = $cache->getCheckKeyTime( $cKey1 );
255 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
256 $t2 = $cache->getCheckKeyTime( $cKey2 );
257 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
258
259 $curTTL = null;
260 $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
261 if ( $versioned ) {
262 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
263 } else {
264 $this->assertEquals( $value, $v, "Value returned" );
265 }
266 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
267
268 $wasSet = 0;
269 $key = wfRandomString();
270 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
271 $this->assertEquals( $value, $v, "Value returned" );
272 $cache->delete( $key );
273 $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
274 $this->assertEquals( $value, $v, "Value still returned after deleted" );
275 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
276
277 $oldValReceived = -1;
278 $oldAsOfReceived = -1;
279 $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
280 use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
281 ++$wasSet;
282 $oldValReceived = $oldVal;
283 $oldAsOfReceived = $oldAsOf;
284
285 return 'xxx' . $wasSet;
286 };
287
288 $mockWallClock = microtime( true );
289 $priorTime = $mockWallClock; // reference time
290
291 $wasSet = 0;
292 $key = wfRandomString();
294 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
295 $this->assertEquals( 'xxx1', $v, "Value returned" );
296 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
297 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
298
299 $mockWallClock += 40;
301 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
302 $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
303 $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
304 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
305 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
306
307 $mockWallClock += 260;
309 $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
310 $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
311 $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
312 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
313 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
314
315 $mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL - 1 );
316 $wasSet = 0;
317 $key = wfRandomString();
318 $checkKey = $cache->makeKey( 'template', 'X' );
319 $cache->touchCheckKey( $checkKey ); // init check key
320 $mockWallClock = $priorTime;
322 $key,
323 $cache::TTL_INDEFINITE,
324 $checkFunc,
325 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
326 );
327 $this->assertEquals( 'xxx1', $v, "Value returned" );
328 $this->assertEquals( 1, $wasSet, "Value computed" );
329 $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
330 $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
331
332 $mockWallClock += $cache::TTL_HOUR; // some time passes
334 $key,
335 $cache::TTL_INDEFINITE,
336 $checkFunc,
337 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
338 );
339 $this->assertEquals( 'xxx1', $v, "Cached value returned" );
340 $this->assertEquals( 1, $wasSet, "Cached value returned" );
341
342 $cache->touchCheckKey( $checkKey ); // make key stale
343 $mockWallClock += 0.01; // ~1 week left of grace (barely stale to avoid refreshes)
344
346 $key,
347 $cache::TTL_INDEFINITE,
348 $checkFunc,
349 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
350 );
351 $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
352 $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
353
354 // Change of refresh increase to unity as staleness approaches graceTTL
355 $mockWallClock += $cache::TTL_WEEK; // 8 days of being stale
357 $key,
358 $cache::TTL_INDEFINITE,
359 $checkFunc,
360 [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
361 );
362 $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
363 $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
364 $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
365 $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
366 }
367
368 public static function getWithSetCallback_provider() {
369 return [
370 [ [], false ],
371 [ [ 'version' => 1 ], true ]
372 ];
373 }
374
375 public function testPreemtiveRefresh() {
376 $value = 'KatCafe';
377 $wasSet = 0;
378 $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
379 {
380 ++$wasSet;
381 return $value;
382 };
383
385 'cache' => new HashBagOStuff(),
386 'pool' => 'empty',
387 ] );
388
389 $wasSet = 0;
390 $key = wfRandomString();
391 $opts = [ 'lowTTL' => 30 ];
392 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
393 $this->assertEquals( $value, $v, "Value returned" );
394 $this->assertEquals( 1, $wasSet, "Value calculated" );
395 $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
396 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
397
398 $wasSet = 0;
399 $key = wfRandomString();
400 $opts = [ 'lowTTL' => 1 ];
401 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
402 $this->assertEquals( $value, $v, "Value returned" );
403 $this->assertEquals( 1, $wasSet, "Value calculated" );
404 $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
405 $this->assertEquals( 1, $wasSet, "Value cached" );
406
407 $asycList = [];
408 $asyncHandler = function ( $callback ) use ( &$asycList ) {
409 $asycList[] = $callback;
410 };
412 'cache' => new HashBagOStuff(),
413 'pool' => 'empty',
414 'asyncHandler' => $asyncHandler
415 ] );
416
417 $mockWallClock = microtime( true );
418 $priorTime = $mockWallClock; // reference time
419 $cache->setMockTime( $mockWallClock );
420
421 $wasSet = 0;
422 $key = wfRandomString();
423 $opts = [ 'lowTTL' => 100 ];
424 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
425 $this->assertEquals( $value, $v, "Value returned" );
426 $this->assertEquals( 1, $wasSet, "Value calculated" );
427 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
428 $this->assertEquals( 1, $wasSet, "Cached value used" );
429 $this->assertEquals( $v, $value, "Value cached" );
430
431 $mockWallClock += 250;
432 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
433 $this->assertEquals( $value, $v, "Value returned" );
434 $this->assertEquals( 1, $wasSet, "Stale value used" );
435 $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
436 $value = 'NewCatsInTown'; // change callback return value
437 $asycList[0](); // run the refresh callback
438 $asycList = [];
439 $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
440 $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
441 $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
442 $this->assertEquals( $value, $v, "New value stored" );
443
445 'cache' => new HashBagOStuff(),
446 'pool' => 'empty'
447 ] );
448
449 $mockWallClock = $priorTime;
450 $cache->setMockTime( $mockWallClock );
451
452 $wasSet = 0;
453 $key = wfRandomString();
454 $opts = [ 'hotTTR' => 900 ];
455 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
456 $this->assertEquals( $value, $v, "Value returned" );
457 $this->assertEquals( 1, $wasSet, "Value calculated" );
458
459 $mockWallClock += 30;
460
461 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
462 $this->assertEquals( 1, $wasSet, "Value cached" );
463
464 $mockWallClock = $priorTime;
465 $wasSet = 0;
466 $key = wfRandomString();
467 $opts = [ 'hotTTR' => 10 ];
468 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
469 $this->assertEquals( $value, $v, "Value returned" );
470 $this->assertEquals( 1, $wasSet, "Value calculated" );
471
472 $mockWallClock += 30;
473
474 $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
475 $this->assertEquals( $value, $v, "Value returned" );
476 $this->assertEquals( 2, $wasSet, "Value re-calculated" );
477 }
478
484 $this->setExpectedException( InvalidArgumentException::class );
485 $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
486 }
487
496 public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
498
499 $keyA = wfRandomString();
500 $keyB = wfRandomString();
501 $keyC = wfRandomString();
502 $cKey1 = wfRandomString();
503 $cKey2 = wfRandomString();
504
505 $priorValue = null;
506 $priorAsOf = null;
507 $wasSet = 0;
508 $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
509 &$wasSet, &$priorValue, &$priorAsOf
510 ) {
511 ++$wasSet;
512 $priorValue = $old;
513 $priorAsOf = $asOf;
514 $ttl = 20; // override with another value
515 return "@$id$";
516 };
517
518 $wasSet = 0;
519 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
520 $value = "@3353$";
522 $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
523 $this->assertEquals( $value, $v[$keyA], "Value returned" );
524 $this->assertEquals( 1, $wasSet, "Value regenerated" );
525 $this->assertFalse( $priorValue, "No prior value" );
526 $this->assertNull( $priorAsOf, "No prior value" );
527
528 $curTTL = null;
529 $cache->get( $keyA, $curTTL );
530 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
531 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
532
533 $wasSet = 0;
534 $value = "@efef$";
535 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
537 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
538 $this->assertEquals( $value, $v[$keyB], "Value returned" );
539 $this->assertEquals( 1, $wasSet, "Value regenerated" );
540 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
542 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
543 $this->assertEquals( $value, $v[$keyB], "Value returned" );
544 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
545 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
546
547 $mockWallClock = microtime( true );
548 $priorTime = $mockWallClock; // reference time
549 $cache->setMockTime( $mockWallClock );
550
551 $mockWallClock += 1;
552
553 $wasSet = 0;
554 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
556 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
557 );
558 $this->assertEquals( $value, $v[$keyB], "Value returned" );
559 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
560 $this->assertEquals( $value, $priorValue, "Has prior value" );
561 $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
562 $t1 = $cache->getCheckKeyTime( $cKey1 );
563 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
564 $t2 = $cache->getCheckKeyTime( $cKey2 );
565 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
566
567 $mockWallClock += 0.01;
568 $priorTime = $mockWallClock;
569 $value = "@43636$";
570 $wasSet = 0;
571 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
573 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
574 );
575 $this->assertEquals( $value, $v[$keyC], "Value returned" );
576 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
577 $t1 = $cache->getCheckKeyTime( $cKey1 );
578 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
579 $t2 = $cache->getCheckKeyTime( $cKey2 );
580 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
581
582 $curTTL = null;
583 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
584 if ( $versioned ) {
585 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
586 } else {
587 $this->assertEquals( $value, $v, "Value returned" );
588 }
589 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
590
591 $wasSet = 0;
592 $key = wfRandomString();
593 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
595 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
596 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
597 $cache->delete( $key );
598 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
600 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
601 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
602 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
603
604 $calls = 0;
605 $ids = [ 1, 2, 3, 4, 5, 6 ];
606 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
607 return $wanCache->makeKey( 'test', $id );
608 };
609 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
610 $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
611 ++$calls;
612
613 return "val-{$id}";
614 };
615 $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
616
617 $this->assertEquals(
618 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
619 array_values( $values ),
620 "Correct values in correct order"
621 );
622 $this->assertEquals(
623 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
624 array_keys( $values ),
625 "Correct keys in correct order"
626 );
627 $this->assertEquals( count( $ids ), $calls );
628
629 $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
630 $this->assertEquals( count( $ids ), $calls, "Values cached" );
631
632 // Mock the BagOStuff to assure only one getMulti() call given process caching
633 $localBag = $this->getMockBuilder( HashBagOStuff::class )
634 ->setMethods( [ 'getMulti' ] )->getMock();
635 $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
636 WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
637 WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
638 ] );
639 $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
640
641 // Warm the process cache
642 $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
643 $this->assertEquals(
644 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
645 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
646 );
647 // Use the process cache
648 $this->assertEquals(
649 [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
650 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
651 );
652 }
653
654 public static function getMultiWithSetCallback_provider() {
655 return [
656 [ [], false ],
657 [ [ 'version' => 1 ], true ]
658 ];
659 }
660
668 public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
670
671 $keyA = wfRandomString();
672 $keyB = wfRandomString();
673 $keyC = wfRandomString();
674 $cKey1 = wfRandomString();
675 $cKey2 = wfRandomString();
676
677 $wasSet = 0;
678 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
679 &$wasSet, &$priorValue, &$priorAsOf
680 ) {
681 $newValues = [];
682 foreach ( $ids as $id ) {
683 ++$wasSet;
684 $newValues[$id] = "@$id$";
685 $ttls[$id] = 20; // override with another value
686 }
687
688 return $newValues;
689 };
690
691 $wasSet = 0;
692 $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
693 $value = "@3353$";
695 $keyedIds, 30, $genFunc, $extOpts );
696 $this->assertEquals( $value, $v[$keyA], "Value returned" );
697 $this->assertEquals( 1, $wasSet, "Value regenerated" );
698
699 $curTTL = null;
700 $cache->get( $keyA, $curTTL );
701 $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
702 $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
703
704 $wasSet = 0;
705 $value = "@efef$";
706 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
708 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
709 $this->assertEquals( $value, $v[$keyB], "Value returned" );
710 $this->assertEquals( 1, $wasSet, "Value regenerated" );
711 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
713 $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
714 $this->assertEquals( $value, $v[$keyB], "Value returned" );
715 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
716 $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
717
718 $mockWallClock = microtime( true );
719 $priorTime = $mockWallClock; // reference time
720 $cache->setMockTime( $mockWallClock );
721
722 $mockWallClock += 1;
723
724 $wasSet = 0;
725 $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
727 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
728 );
729 $this->assertEquals( $value, $v[$keyB], "Value returned" );
730 $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
731 $t1 = $cache->getCheckKeyTime( $cKey1 );
732 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
733 $t2 = $cache->getCheckKeyTime( $cKey2 );
734 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
735
736 $mockWallClock += 0.01;
737 $priorTime = $mockWallClock;
738 $value = "@43636$";
739 $wasSet = 0;
740 $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
742 $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
743 );
744 $this->assertEquals( $value, $v[$keyC], "Value returned" );
745 $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
746 $t1 = $cache->getCheckKeyTime( $cKey1 );
747 $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
748 $t2 = $cache->getCheckKeyTime( $cKey2 );
749 $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
750
751 $curTTL = null;
752 $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
753 if ( $versioned ) {
754 $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
755 } else {
756 $this->assertEquals( $value, $v, "Value returned" );
757 }
758 $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
759
760 $wasSet = 0;
761 $key = wfRandomString();
762 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
764 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
765 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
766 $cache->delete( $key );
767 $keyedIds = new ArrayIterator( [ $key => 242424 ] );
769 $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
770 $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
771 $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
772
773 $calls = 0;
774 $ids = [ 1, 2, 3, 4, 5, 6 ];
775 $keyFunc = function ( $id, WANObjectCache $wanCache ) {
776 return $wanCache->makeKey( 'test', $id );
777 };
778 $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
779 $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
780 $newValues = [];
781 foreach ( $ids as $id ) {
782 ++$calls;
783 $newValues[$id] = "val-{$id}";
784 }
785
786 return $newValues;
787 };
788 $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
789
790 $this->assertEquals(
791 [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
792 array_values( $values ),
793 "Correct values in correct order"
794 );
795 $this->assertEquals(
796 array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
797 array_keys( $values ),
798 "Correct keys in correct order"
799 );
800 $this->assertEquals( count( $ids ), $calls );
801
802 $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
803 $this->assertEquals( count( $ids ), $calls, "Values cached" );
804 }
805
806 public static function getMultiWithUnionSetCallback_provider() {
807 return [
808 [ [], false ],
809 [ [ 'version' => 1 ], true ]
810 ];
811 }
812
817 public function testLockTSE() {
819 $key = wfRandomString();
821
822 $calls = 0;
823 $func = function () use ( &$calls, $value, $cache, $key ) {
824 ++$calls;
825 return $value;
826 };
827
828 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
829 $this->assertEquals( $value, $ret );
830 $this->assertEquals( 1, $calls, 'Value was populated' );
831
832 // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
833 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
834
835 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
836 $ret = $cache->getWithSetCallback( $key, 30, $func,
837 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
838 $this->assertEquals( $value, $ret, 'Old value used' );
839 $this->assertEquals( 1, $calls, 'Callback was not used' );
840
841 $cache->delete( $key );
842 $ret = $cache->getWithSetCallback( $key, 30, $func,
843 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
844 $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
845 $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
846
847 $ret = $cache->getWithSetCallback( $key, 30, $func,
848 [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
849 $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
850 $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
851 }
852
858 public function testLockTSESlow() {
860 $key = wfRandomString();
862
863 $calls = 0;
864 $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
865 ++$calls;
866 $setOpts['since'] = microtime( true ) - 10;
867 // Immediately kill any mutex rather than waiting a second
868 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
869 return $value;
870 };
871
872 // Value should be marked as stale due to snapshot lag
873 $curTTL = null;
874 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
875 $this->assertEquals( $value, $ret );
876 $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
877 $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
878 $this->assertEquals( 1, $calls, 'Value was generated' );
879
880 // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
881 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
882 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
883 $this->assertEquals( $value, $ret );
884 $this->assertEquals( 1, $calls, 'Callback was not used' );
885 }
886
891 public function testBusyValue() {
893 $key = wfRandomString();
895 $busyValue = wfRandomString();
896
897 $calls = 0;
898 $func = function () use ( &$calls, $value, $cache, $key ) {
899 ++$calls;
900 // Immediately kill any mutex rather than waiting a second
901 $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
902 return $value;
903 };
904
905 $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
906 $this->assertEquals( $value, $ret );
907 $this->assertEquals( 1, $calls, 'Value was populated' );
908
909 // Acquire a lock to verify that getWithSetCallback uses busyValue properly
910 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
911
912 $checkKeys = [ wfRandomString() ]; // new check keys => force misses
913 $ret = $cache->getWithSetCallback( $key, 30, $func,
914 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
915 $this->assertEquals( $value, $ret, 'Callback used' );
916 $this->assertEquals( 2, $calls, 'Callback used' );
917
918 $ret = $cache->getWithSetCallback( $key, 30, $func,
919 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
920 $this->assertEquals( $value, $ret, 'Old value used' );
921 $this->assertEquals( 2, $calls, 'Callback was not used' );
922
923 $cache->delete( $key ); // no value at all anymore and still locked
924 $ret = $cache->getWithSetCallback( $key, 30, $func,
925 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
926 $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
927 $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
928
929 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
930 $ret = $cache->getWithSetCallback( $key, 30, $func,
931 [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
932 $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
933 $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
934
935 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
936 $ret = $cache->getWithSetCallback( $key, 30, $func,
937 [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
938 $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
939 $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
940 }
941
945 public function testGetMulti() {
947
948 $value1 = [ 'this' => 'is', 'a' => 'test' ];
949 $value2 = [ 'this' => 'is', 'another' => 'test' ];
950
951 $key1 = wfRandomString();
952 $key2 = wfRandomString();
953 $key3 = wfRandomString();
954
955 $cache->set( $key1, $value1, 5 );
956 $cache->set( $key2, $value2, 10 );
957
958 $curTTLs = [];
959 $this->assertEquals(
960 [ $key1 => $value1, $key2 => $value2 ],
961 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
962 'Result array populated'
963 );
964
965 $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
966 $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
967 $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
968
969 $cKey1 = wfRandomString();
970 $cKey2 = wfRandomString();
971
972 $mockWallClock = microtime( true );
973 $priorTime = $mockWallClock; // reference time
974 $cache->setMockTime( $mockWallClock );
975
976 $mockWallClock += 1;
977
978 $curTTLs = [];
979 $this->assertEquals(
980 [ $key1 => $value1, $key2 => $value2 ],
981 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
982 "Result array populated even with new check keys"
983 );
984 $t1 = $cache->getCheckKeyTime( $cKey1 );
985 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
986 $t2 = $cache->getCheckKeyTime( $cKey2 );
987 $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
988 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
989 $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
990 $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
991
992 $mockWallClock += 1;
993
994 $curTTLs = [];
995 $this->assertEquals(
996 [ $key1 => $value1, $key2 => $value2 ],
997 $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
998 "Result array still populated even with new check keys"
999 );
1000 $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
1001 $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
1002 $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
1003 }
1004
1009 public function testGetMultiCheckKeys() {
1011
1012 $checkAll = wfRandomString();
1013 $check1 = wfRandomString();
1014 $check2 = wfRandomString();
1015 $check3 = wfRandomString();
1016 $value1 = wfRandomString();
1017 $value2 = wfRandomString();
1018
1019 $mockWallClock = microtime( true );
1020 $cache->setMockTime( $mockWallClock );
1021
1022 // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1023 // several seconds during the test to assert the behaviour.
1024 foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1025 $cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_NONE );
1026 }
1027
1028 $mockWallClock += 0.100;
1029
1030 $cache->set( 'key1', $value1, 10 );
1031 $cache->set( 'key2', $value2, 10 );
1032
1033 $curTTLs = [];
1034 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1035 'key1' => $check1,
1036 $checkAll,
1037 'key2' => $check2,
1038 'key3' => $check3,
1039 ] );
1040 $this->assertEquals(
1041 [ 'key1' => $value1, 'key2' => $value2 ],
1042 $result,
1043 'Initial values'
1044 );
1045 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1046 $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1047 $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1048 $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1049
1050 $mockWallClock += 0.100;
1051 $cache->touchCheckKey( $check1 );
1052
1053 $curTTLs = [];
1054 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1055 'key1' => $check1,
1056 $checkAll,
1057 'key2' => $check2,
1058 'key3' => $check3,
1059 ] );
1060 $this->assertEquals(
1061 [ 'key1' => $value1, 'key2' => $value2 ],
1062 $result,
1063 'key1 expired by check1, but value still provided'
1064 );
1065 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1066 $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1067
1068 $cache->touchCheckKey( $checkAll );
1069
1070 $curTTLs = [];
1071 $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1072 'key1' => $check1,
1073 $checkAll,
1074 'key2' => $check2,
1075 'key3' => $check3,
1076 ] );
1077 $this->assertEquals(
1078 [ 'key1' => $value1, 'key2' => $value2 ],
1079 $result,
1080 'All keys expired by checkAll, but value still provided'
1081 );
1082 $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1083 $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1084 }
1085
1090 public function testCheckKeyInitHoldoff() {
1092
1093 for ( $i = 0; $i < 500; ++$i ) {
1094 $key = wfRandomString();
1095 $checkKey = wfRandomString();
1096 // miss, set, hit
1097 $cache->get( $key, $curTTL, [ $checkKey ] );
1098 $cache->set( $key, 'val', 10 );
1099 $curTTL = null;
1100 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1101
1102 $this->assertEquals( 'val', $v );
1103 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1104 }
1105
1106 for ( $i = 0; $i < 500; ++$i ) {
1107 $key = wfRandomString();
1108 $checkKey = wfRandomString();
1109 // set, hit
1110 $cache->set( $key, 'val', 10 );
1111 $curTTL = null;
1112 $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1113
1114 $this->assertEquals( 'val', $v );
1115 $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
1116 }
1117 }
1118
1124 public function testDelete() {
1125 $key = wfRandomString();
1127 $this->cache->set( $key, $value );
1128
1129 $curTTL = null;
1130 $v = $this->cache->get( $key, $curTTL );
1131 $this->assertEquals( $value, $v, "Key was created with value" );
1132 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1133
1134 $this->cache->delete( $key );
1135
1136 $curTTL = null;
1137 $v = $this->cache->get( $key, $curTTL );
1138 $this->assertFalse( $v, "Deleted key has false value" );
1139 $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1140
1141 $this->cache->set( $key, $value . 'more' );
1142 $v = $this->cache->get( $key, $curTTL );
1143 $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1144 $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1145
1146 $this->cache->set( $key, $value );
1147 $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1148
1149 $curTTL = null;
1150 $v = $this->cache->get( $key, $curTTL );
1151 $this->assertFalse( $v, "Deleted key has false value" );
1152 $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1153
1154 $this->cache->set( $key, $value );
1155 $v = $this->cache->get( $key, $curTTL );
1156 $this->assertEquals( $value, $v, "Key was created with value" );
1157 $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1158 }
1159
1167 public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1169
1170 $key = wfRandomString();
1171 $valueV1 = wfRandomString();
1172 $valueV2 = [ wfRandomString() ];
1173
1174 $wasSet = 0;
1175 $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1176 ++$wasSet;
1177
1178 return $valueV1;
1179 };
1180
1181 $priorValue = false;
1182 $priorAsOf = null;
1183 $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1184 use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1185 $priorValue = $oldValue;
1186 $priorAsOf = $oldAsOf;
1187 ++$wasSet;
1188
1189 return $valueV2; // new array format
1190 };
1191
1192 // Set the main key (version N if versioned)
1193 $wasSet = 0;
1194 $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1195 $this->assertEquals( $valueV1, $v, "Value returned" );
1196 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1197 $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1198 $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1199 $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1200
1201 if ( $versioned ) {
1202 // Set the key for version N+1 format
1203 $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1204 } else {
1205 // Start versioning now with the unversioned key still there
1206 $verOpts = [ 'version' => 1 ];
1207 }
1208
1209 // Value goes to secondary key since V1 already used $key
1210 $wasSet = 0;
1211 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1212 $this->assertEquals( $valueV2, $v, "Value returned" );
1213 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1214 $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1215 $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1216
1217 $wasSet = 0;
1218 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1219 $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1220 $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1221
1222 // Clear out the older or unversioned key
1223 $cache->delete( $key, 0 );
1224
1225 // Set the key for next/first versioned format
1226 $wasSet = 0;
1227 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1228 $this->assertEquals( $valueV2, $v, "Value returned" );
1229 $this->assertEquals( 1, $wasSet, "Value regenerated" );
1230
1231 $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1232 $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1233 $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1234 }
1235
1236 public static function getWithSetCallback_versions_provider() {
1237 return [
1238 [ [], false ],
1239 [ [ 'version' => 1 ], true ]
1240 ];
1241 }
1242
1247 public function testInterimHoldOffCaching() {
1249
1250 $value = 'CRL-40-940';
1251 $wasCalled = 0;
1252 $func = function () use ( &$wasCalled, $value ) {
1253 $wasCalled++;
1254
1255 return $value;
1256 };
1257
1259
1260 $key = wfRandomString( 32 );
1261 $v = $cache->getWithSetCallback( $key, 60, $func );
1262 $v = $cache->getWithSetCallback( $key, 60, $func );
1263 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1264 $cache->delete( $key );
1265 $v = $cache->getWithSetCallback( $key, 60, $func );
1266 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1267 $v = $cache->getWithSetCallback( $key, 60, $func );
1268 $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1269 // Lock up the mutex so interim cache is used
1270 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1271 $v = $cache->getWithSetCallback( $key, 60, $func );
1272 $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1273 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
1274
1276
1277 $wasCalled = 0;
1278 $key = wfRandomString( 32 );
1279 $v = $cache->getWithSetCallback( $key, 60, $func );
1280 $v = $cache->getWithSetCallback( $key, 60, $func );
1281 $this->assertEquals( 1, $wasCalled, 'Value cached' );
1282 $cache->delete( $key );
1283 $v = $cache->getWithSetCallback( $key, 60, $func );
1284 $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1285 $v = $cache->getWithSetCallback( $key, 60, $func );
1286 $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1287 $v = $cache->getWithSetCallback( $key, 60, $func );
1288 $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1289 // Lock up the mutex so interim cache is used
1290 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1291 $v = $cache->getWithSetCallback( $key, 60, $func );
1292 $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1293 }
1294
1303 public function testTouchKeys() {
1305 $key = wfRandomString();
1306
1307 $mockWallClock = microtime( true );
1308 $priorTime = $mockWallClock; // reference time
1309 $cache->setMockTime( $mockWallClock );
1310
1311 $mockWallClock += 0.100;
1312 $t0 = $cache->getCheckKeyTime( $key );
1313 $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1314
1315 $priorTime = $mockWallClock;
1316 $mockWallClock += 0.100;
1317 $cache->touchCheckKey( $key );
1318 $t1 = $cache->getCheckKeyTime( $key );
1319 $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1320
1321 $t2 = $cache->getCheckKeyTime( $key );
1322 $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1323
1324 $mockWallClock += 0.100;
1325 $cache->touchCheckKey( $key );
1326 $t3 = $cache->getCheckKeyTime( $key );
1327 $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1328
1329 $t4 = $cache->getCheckKeyTime( $key );
1330 $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1331
1332 $mockWallClock += 0.100;
1333 $cache->resetCheckKey( $key );
1334 $t5 = $cache->getCheckKeyTime( $key );
1335 $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1336
1337 $t6 = $cache->getCheckKeyTime( $key );
1338 $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1339 }
1340
1345 $key = wfRandomString();
1346 $tKey1 = wfRandomString();
1347 $tKey2 = wfRandomString();
1348 $value = 'meow';
1349
1350 // Two check keys are newer (given hold-off) than $key, another is older
1351 $this->internalCache->set(
1352 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1353 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
1354 );
1355 $this->internalCache->set(
1356 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1357 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
1358 );
1359 $this->internalCache->set(
1360 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1361 WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
1362 );
1363 $this->cache->set( $key, $value, 30 );
1364
1365 $curTTL = null;
1366 $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1367 $this->assertEquals( $value, $v, "Value matches" );
1368 $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1369 $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1370 }
1371
1376 public function testReap() {
1377 $vKey1 = wfRandomString();
1378 $vKey2 = wfRandomString();
1379 $tKey1 = wfRandomString();
1380 $tKey2 = wfRandomString();
1381 $value = 'moo';
1382
1383 $knownPurge = time() - 60;
1384 $goodTime = microtime( true ) - 5;
1385 $badTime = microtime( true ) - 300;
1386
1387 $this->internalCache->set(
1388 WANObjectCache::VALUE_KEY_PREFIX . $vKey1,
1389 [
1390 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1391 WANObjectCache::FLD_VALUE => $value,
1392 WANObjectCache::FLD_TTL => 3600,
1393 WANObjectCache::FLD_TIME => $goodTime
1394 ]
1395 );
1396 $this->internalCache->set(
1397 WANObjectCache::VALUE_KEY_PREFIX . $vKey2,
1398 [
1399 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1400 WANObjectCache::FLD_VALUE => $value,
1401 WANObjectCache::FLD_TTL => 3600,
1402 WANObjectCache::FLD_TIME => $badTime
1403 ]
1404 );
1405 $this->internalCache->set(
1406 WANObjectCache::TIME_KEY_PREFIX . $tKey1,
1407 WANObjectCache::PURGE_VAL_PREFIX . $goodTime
1408 );
1409 $this->internalCache->set(
1410 WANObjectCache::TIME_KEY_PREFIX . $tKey2,
1411 WANObjectCache::PURGE_VAL_PREFIX . $badTime
1412 );
1413
1414 $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1415 $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1416 $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1417 $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1418
1419 $this->assertFalse( $bad1 );
1420 $this->assertTrue( $bad2 );
1421
1422 $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1423 $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1424 $this->assertFalse( $tBad1 );
1425 $this->assertTrue( $tBad2 );
1426 }
1427
1431 public function testReap_fail() {
1432 $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1433 ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1434 $backend->expects( $this->once() )->method( 'get' )
1435 ->willReturn( [
1436 WANObjectCache::FLD_VERSION => WANObjectCache::VERSION,
1437 WANObjectCache::FLD_VALUE => 'value',
1438 WANObjectCache::FLD_TTL => 3600,
1439 WANObjectCache::FLD_TIME => 300,
1440 ] );
1441 $backend->expects( $this->once() )->method( 'changeTTL' )
1442 ->willReturn( false );
1443
1444 $wanCache = new WANObjectCache( [
1445 'cache' => $backend,
1446 'pool' => 'testcache-hash',
1447 'relayer' => new EventRelayerNull( [] )
1448 ] );
1449
1450 $isStale = null;
1451 $ret = $wanCache->reap( 'key', 360, $isStale );
1452 $this->assertTrue( $isStale, 'value was stale' );
1453 $this->assertFalse( $ret, 'changeTTL failed' );
1454 }
1455
1459 public function testSetWithLag() {
1460 $value = 1;
1461
1462 $key = wfRandomString();
1463 $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1464 $this->cache->set( $key, $value, 30, $opts );
1465 $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1466
1467 $key = wfRandomString();
1468 $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1469 $this->cache->set( $key, $value, 30, $opts );
1470 $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1471
1472 $key = wfRandomString();
1473 $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1474 $this->cache->set( $key, $value, 30, $opts );
1475 $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1476 }
1477
1481 public function testWritePending() {
1482 $value = 1;
1483
1484 $key = wfRandomString();
1485 $opts = [ 'pending' => true ];
1486 $this->cache->set( $key, $value, 30, $opts );
1487 $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1488 }
1489
1490 public function testMcRouterSupport() {
1491 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1492 ->setMethods( [ 'set', 'delete' ] )->getMock();
1493 $localBag->expects( $this->never() )->method( 'set' );
1494 $localBag->expects( $this->never() )->method( 'delete' );
1495 $wanCache = new WANObjectCache( [
1496 'cache' => $localBag,
1497 'pool' => 'testcache-hash',
1498 'relayer' => new EventRelayerNull( [] ),
1499 'mcrouterAware' => true,
1500 'region' => 'pmtpa',
1501 'cluster' => 'mw-wan'
1502 ] );
1503 $valFunc = function () {
1504 return 1;
1505 };
1506
1507 // None of these should use broadcasting commands (e.g. SET, DELETE)
1508 $wanCache->get( 'x' );
1509 $wanCache->get( 'x', $ctl, [ 'check1' ] );
1510 $wanCache->getMulti( [ 'x', 'y' ] );
1511 $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1512 $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1513 $wanCache->getCheckKeyTime( 'zzz' );
1514 $wanCache->reap( 'x', time() - 300 );
1515 $wanCache->reap( 'zzz', time() - 300 );
1516 }
1517
1519 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1520 ->setMethods( [ 'set' ] )->getMock();
1521 $wanCache = new WANObjectCache( [
1522 'cache' => $localBag,
1523 'pool' => 'testcache-hash',
1524 'relayer' => new EventRelayerNull( [] ),
1525 'mcrouterAware' => true,
1526 'region' => 'pmtpa',
1527 'cluster' => 'mw-wan'
1528 ] );
1529
1530 $localBag->expects( $this->once() )->method( 'set' )
1531 ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX . "test" );
1532
1533 $wanCache->delete( 'test' );
1534 }
1535
1537 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1538 ->setMethods( [ 'set' ] )->getMock();
1539 $wanCache = new WANObjectCache( [
1540 'cache' => $localBag,
1541 'pool' => 'testcache-hash',
1542 'relayer' => new EventRelayerNull( [] ),
1543 'mcrouterAware' => true,
1544 'region' => 'pmtpa',
1545 'cluster' => 'mw-wan'
1546 ] );
1547
1548 $localBag->expects( $this->once() )->method( 'set' )
1549 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1550
1551 $wanCache->touchCheckKey( 'test' );
1552 }
1553
1555 $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1556 ->setMethods( [ 'delete' ] )->getMock();
1557 $wanCache = new WANObjectCache( [
1558 'cache' => $localBag,
1559 'pool' => 'testcache-hash',
1560 'relayer' => new EventRelayerNull( [] ),
1561 'mcrouterAware' => true,
1562 'region' => 'pmtpa',
1563 'cluster' => 'mw-wan'
1564 ] );
1565
1566 $localBag->expects( $this->once() )->method( 'delete' )
1567 ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1568
1569 $wanCache->resetCheckKey( 'test' );
1570 }
1571
1572 public function testEpoch() {
1573 $bag = new HashBagOStuff();
1574 $cache = new WANObjectCache( [ 'cache' => $bag, 'pool' => 'testcache-hash' ] );
1575 $key = $cache->makeGlobalKey( 'The whole of the Law' );
1576
1577 $now = microtime( true );
1578 $cache->setMockTime( $now );
1579
1580 $cache->set( $key, 'Do what thou Wilt' );
1581 $cache->touchCheckKey( $key );
1582
1583 $then = $now;
1584 $now += 30;
1585 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1586 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key init', 0.01 );
1587
1588 $cache = new WANObjectCache( [
1589 'cache' => $bag,
1590 'pool' => 'testcache-hash',
1591 'epoch' => $now - 3600
1592 ] );
1593 $cache->setMockTime( $now );
1594
1595 $this->assertEquals( 'Do what thou Wilt', $cache->get( $key ) );
1596 $this->assertEquals( $then, $cache->getCheckKeyTime( $key ), 'Check key kept', 0.01 );
1597
1598 $now += 30;
1599 $cache = new WANObjectCache( [
1600 'cache' => $bag,
1601 'pool' => 'testcache-hash',
1602 'epoch' => $now + 3600
1603 ] );
1604 $cache->setMockTime( $now );
1605
1606 $this->assertFalse( $cache->get( $key ), 'Key rejected due to epoch' );
1607 $this->assertEquals( $now, $cache->getCheckKeyTime( $key ), 'Check key reset', 0.01 );
1608 }
1609
1619 public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1620 $mtime = $ago ? time() - $ago : $ago;
1621 $margin = 5;
1622 $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1623
1624 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1625 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1626
1627 $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1628
1629 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1630 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1631 }
1632
1633 public static function provideAdaptiveTTL() {
1634 return [
1635 [ 3600, 900, 30, 0.2, 720 ],
1636 [ 3600, 500, 30, 0.2, 500 ],
1637 [ 3600, 86400, 800, 0.2, 800 ],
1638 [ false, 86400, 800, 0.2, 800 ],
1639 [ null, 86400, 800, 0.2, 800 ]
1640 ];
1641 }
1642
1647 public function testNewEmpty() {
1648 $this->assertInstanceOf(
1649 WANObjectCache::class,
1650 WANObjectCache::newEmpty()
1651 );
1652 }
1653
1657 public function testSetLogger() {
1658 $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1659 }
1660
1664 public function testGetQoS() {
1665 $backend = $this->getMockBuilder( HashBagOStuff::class )
1666 ->setMethods( [ 'getQoS' ] )->getMock();
1667 $backend->expects( $this->once() )->method( 'getQoS' )
1668 ->willReturn( BagOStuff::QOS_UNKNOWN );
1669 $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1670
1671 $this->assertSame(
1672 $wanCache::QOS_UNKNOWN,
1673 $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1674 );
1675 }
1676
1680 public function testMakeKey() {
1681 $backend = $this->getMockBuilder( HashBagOStuff::class )
1682 ->setMethods( [ 'makeKey' ] )->getMock();
1683 $backend->expects( $this->once() )->method( 'makeKey' )
1684 ->willReturn( 'special' );
1685
1686 $wanCache = new WANObjectCache( [
1687 'cache' => $backend,
1688 'pool' => 'testcache-hash',
1689 'relayer' => new EventRelayerNull( [] )
1690 ] );
1691
1692 $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1693 }
1694
1698 public function testMakeGlobalKey() {
1699 $backend = $this->getMockBuilder( HashBagOStuff::class )
1700 ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1701 $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1702 ->willReturn( 'special' );
1703
1704 $wanCache = new WANObjectCache( [
1705 'cache' => $backend,
1706 'pool' => 'testcache-hash',
1707 'relayer' => new EventRelayerNull( [] )
1708 ] );
1709
1710 $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1711 }
1712
1713 public static function statsKeyProvider() {
1714 return [
1715 [ 'domain:page:5', 'page' ],
1716 [ 'domain:main-key', 'main-key' ],
1717 [ 'domain:page:history', 'page' ],
1718 [ 'missingdomainkey', 'missingdomainkey' ]
1719 ];
1720 }
1721
1726 public function testStatsKeyClass( $key, $class ) {
1727 $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1728 'cache' => new HashBagOStuff,
1729 'pool' => 'testcache-hash',
1730 'relayer' => new EventRelayerNull( [] )
1731 ] ) );
1732
1733 $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1734 }
1735}
1736
1738 const CLOCK_SKEW = 1;
1739
1740 protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1741 return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1742 }
1743}
1744
1746 protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1747 return ( ( $now - $asOf ) > $timeTillRefresh );
1748 }
1749}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Class representing a cache/ephemeral data store.
Definition BagOStuff.php:58
No-op class for publishing messages into a PubSub system.
Simple store for keeping values in an associative array for the current process.
worthRefreshExpiring( $curTTL, $lowTTL)
Check if a key is nearing expiration and thus due for randomized regeneration.
worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now)
Check if a key is due for randomized regeneration due to its popularity.
WANObjectCache::wrap WANObjectCache::unwrap WANObjectCache::worthRefreshExpiring WANObjectCache::wort...
testGetWithSetCallback_versions(array $extOpts, $versioned)
getWithSetCallback_versions_provider WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSe...
testNewEmpty()
WANObjectCache::__construct WANObjectCache::newEmpty.
testBusyValue()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
testGetWithSetCallback(array $extOpts, $versioned)
getWithSetCallback_provider WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback...
testDelete()
WANObjectCache::delete WANObjectCache::relayDelete WANObjectCache::relayPurge.
testSetAndGet( $value, $ttl)
provideSetAndGet WANObjectCache::set() WANObjectCache::get() WANObjectCache::makeKey()
testGetQoS()
WANObjectCache::getQoS.
testReap_fail()
WANObjectCache::reap()
testLockTSESlow()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback() WANObjectCache::set()
testMakeKey()
WANObjectCache::makeKey.
testGetWithSeveralCheckKeys()
WANObjectCache::getMulti()
testSetOver()
WANObjectCache::set()
testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL)
provideAdaptiveTTL WANObjectCache::adaptiveTTL()
testGetMulti()
WANObjectCache::getMulti()
testGetNotExists()
WANObjectCache::get() WANObjectCache::makeGlobalKey()
testReap()
WANObjectCache::reap() WANObjectCache::reapCheckKey()
testGetMultiCheckKeys()
WANObjectCache::getMulti() WANObjectCache::processCheckKeys()
testTouchKeys()
WANObjectCache::touchCheckKey WANObjectCache::resetCheckKey WANObjectCache::getCheckKeyTime WANObject...
testMakeGlobalKey()
WANObjectCache::makeGlobalKey.
static getMultiWithUnionSetCallback_provider()
testSetWithLag()
WANObjectCache::set()
static getMultiWithSetCallback_provider()
testSetLogger()
WANObjectCache::setLogger.
testCheckKeyInitHoldoff()
WANObjectCache::get() WANObjectCache::processCheckKeys()
static getWithSetCallback_versions_provider()
testWritePending()
WANObjectCache::set()
testGetMultiWithUnionSetCallback(array $extOpts, $versioned)
getMultiWithUnionSetCallback_provider WANObjectCache::getMultiWithUnionSetCallback() WANObjectCache::...
testStaleSet()
WANObjectCache::set()
testGetMultiWithSetCallback(array $extOpts, $versioned)
getMultiWithSetCallback_provider WANObjectCache::getMultiWithSetCallback WANObjectCache::makeMultiKey...
testStatsKeyClass( $key, $class)
statsKeyProvider WANObjectCache::determineKeyClass
testInterimHoldOffCaching()
WANObjectCache::useInterimHoldOffCaching WANObjectCache::getInterimValue.
testGetWithSetCallback_invalidCallback()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
testLockTSE()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
Multi-datacenter aware caching interface.
touchCheckKey( $key, $holdoff=self::HOLDOFF_TTL)
Purge a "check" key from all datacenters, invalidating keys that use it.
delete( $key, $ttl=self::HOLDOFF_TTL)
Purge a key from all datacenters.
getMulti(array $keys, &$curTTLs=[], array $checkKeys=[], array &$asOfs=[])
Fetch the value of several keys from cache.
getCheckKeyTime( $key)
Fetch the value of a timestamp "check" key.
getMultiWithUnionSetCallback(ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[])
Method to fetch/regenerate multiple cache keys at once.
getMultiWithSetCallback(ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[])
Method to fetch multiple cache keys at once with regeneration.
getWithSetCallback( $key, $ttl, $callback, array $opts=[])
Method to fetch/regenerate cache keys.
makeMultiKeys(array $entities, callable $keyFunc)
useInterimHoldOffCaching( $enabled)
Enable or disable the use of brief caching for tombstoned keys.
get( $key, &$curTTL=null, array $checkKeys=[], &$asOf=null)
Fetch the value of a key from cache.
makeKey( $class, $component=null)
resetCheckKey( $key)
Delete a "check" key from all datacenters, invalidating keys that use it.
makeGlobalKey( $class, $component=null)
set( $key, $value, $ttl=0, array $opts=[])
Set the value of a key in cache.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
globals will be eliminated from MediaWiki replaced by an application object which would be passed to constructors Whether that would be an convenient solution remains to be but certainly PHP makes such object oriented programming models easier than they were in previous versions For the time being MediaWiki programmers will have to work in an environment with some global context At the time of globals were initialised on startup by MediaWiki of these were configuration which are documented in DefaultSettings php There is no comprehensive documentation for the remaining however some of the most important ones are listed below They are typically initialised either in index php or in Setup php $wgTitle Title object created from the request URL $wgOut OutputPage object for HTTP response $wgUser User object for the user associated with the current request $wgLang Language object selected by user preferences $wgContLang Language object associated with the wiki being viewed $wgParser Parser object Parser extensions register their hooks here $wgRequest WebRequest object
Definition globals.txt:62
The index of the header message $result[1]=The index of the body text message $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters. 'ImgAuthModifyHeaders':Executed just before a file is streamed to a user via img_auth.php, allowing headers to be modified beforehand. $title:LinkTarget object & $headers:HTTP headers(name=> value, names are case insensitive). Two headers get special handling:If-Modified-Since(value must be a valid HTTP date) and Range(must be of the form "bytes=(\d*-\d*)") will be honored when streaming the file. 'ImportHandleLogItemXMLTag':When parsing a XML tag in a log item. Return false to stop further processing of the tag $reader:XMLReader object $logInfo:Array of information 'ImportHandlePageXMLTag':When parsing a XML tag in a page. Return false to stop further processing of the tag $reader:XMLReader object & $pageInfo:Array of information 'ImportHandleRevisionXMLTag':When parsing a XML tag in a page revision. Return false to stop further processing of the tag $reader:XMLReader object $pageInfo:Array of page information $revisionInfo:Array of revision information 'ImportHandleToplevelXMLTag':When parsing a top level XML tag. Return false to stop further processing of the tag $reader:XMLReader object 'ImportHandleUnknownUser':When a user doesn 't exist locally, this hook is called to give extensions an opportunity to auto-create it. If the auto-creation is successful, return false. $name:User name 'ImportHandleUploadXMLTag':When parsing a XML tag in a file upload. Return false to stop further processing of the tag $reader:XMLReader object $revisionInfo:Array of information 'ImportLogInterwikiLink':Hook to change the interwiki link used in log entries and edit summaries for transwiki imports. & $fullInterwikiPrefix:Interwiki prefix, may contain colons. & $pageTitle:String that contains page title. 'ImportSources':Called when reading from the $wgImportSources configuration variable. Can be used to lazy-load the import sources list. & $importSources:The value of $wgImportSources. Modify as necessary. See the comment in DefaultSettings.php for the detail of how to structure this array. 'InfoAction':When building information to display on the action=info page. $context:IContextSource object & $pageInfo:Array of information 'InitializeArticleMaybeRedirect':MediaWiki check to see if title is a redirect. & $title:Title object for the current page & $request:WebRequest & $ignoreRedirect:boolean to skip redirect check & $target:Title/string of redirect target & $article:Article object 'InternalParseBeforeLinks':during Parser 's internalParse method before links but after nowiki/noinclude/includeonly/onlyinclude and other processings. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InternalParseBeforeSanitize':during Parser 's internalParse method just before the parser removes unwanted/dangerous HTML tags and after nowiki/noinclude/includeonly/onlyinclude and other processings. Ideal for syntax-extensions after template/parser function execution which respect nowiki and HTML-comments. & $parser:Parser object & $text:string containing partially parsed text & $stripState:Parser 's internal StripState object 'InterwikiLoadPrefix':When resolving if a given prefix is an interwiki or not. Return true without providing an interwiki to continue interwiki search. $prefix:interwiki prefix we are looking for. & $iwData:output array describing the interwiki with keys iw_url, iw_local, iw_trans and optionally iw_api and iw_wikiid. 'InvalidateEmailComplete':Called after a user 's email has been invalidated successfully. $user:user(object) whose email is being invalidated 'IRCLineURL':When constructing the URL to use in an IRC notification. Callee may modify $url and $query, URL will be constructed as $url . $query & $url:URL to index.php & $query:Query string $rc:RecentChange object that triggered url generation 'IsFileCacheable':Override the result of Article::isFileCacheable()(if true) & $article:article(object) being checked 'IsTrustedProxy':Override the result of IP::isTrustedProxy() & $ip:IP being check & $result:Change this value to override the result of IP::isTrustedProxy() 'IsUploadAllowedFromUrl':Override the result of UploadFromUrl::isAllowedUrl() $url:URL used to upload from & $allowed:Boolean indicating if uploading is allowed for given URL 'isValidEmailAddr':Override the result of Sanitizer::validateEmail(), for instance to return false if the domain name doesn 't match your organization. $addr:The e-mail address entered by the user & $result:Set this and return false to override the internal checks 'isValidPassword':Override the result of User::isValidPassword() $password:The password entered by the user & $result:Set this and return false to override the internal checks $user:User the password is being validated for 'Language::getMessagesFileName':$code:The language code or the language we 're looking for a messages file for & $file:The messages file path, you can override this to change the location. 'LanguageGetMagic':DEPRECATED since 1.16! Use $magicWords in a file listed in $wgExtensionMessagesFiles instead. Use this to define synonyms of magic words depending of the language & $magicExtensions:associative array of magic words synonyms $lang:language code(string) 'LanguageGetNamespaces':Provide custom ordering for namespaces or remove namespaces. Do not use this hook to add namespaces. Use CanonicalNamespaces for that. & $namespaces:Array of namespaces indexed by their numbers 'LanguageGetSpecialPageAliases':DEPRECATED! Use $specialPageAliases in a file listed in $wgExtensionMessagesFiles instead. Use to define aliases of special pages names depending of the language & $specialPageAliases:associative array of magic words synonyms $lang:language code(string) 'LanguageGetTranslatedLanguageNames':Provide translated language names. & $names:array of language code=> language name $code:language of the preferred translations 'LanguageLinks':Manipulate a page 's language links. This is called in various places to allow extensions to define the effective language links for a page. $title:The page 's Title. & $links:Array with elements of the form "language:title" in the order that they will be output. & $linkFlags:Associative array mapping prefixed links to arrays of flags. Currently unused, but planned to provide support for marking individual language links in the UI, e.g. for featured articles. 'LanguageSelector':Hook to change the language selector available on a page. $out:The output page. $cssClassName:CSS class name of the language selector. 'LinkBegin':DEPRECATED since 1.28! Use HtmlPageLinkRendererBegin instead. Used when generating internal and interwiki links in Linker::link(), before processing starts. Return false to skip default processing and return $ret. See documentation for Linker::link() for details on the expected meanings of parameters. $skin:the Skin object $target:the Title that the link is pointing to & $html:the contents that the< a > tag should have(raw HTML) $result
Definition hooks.txt:2042
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2055
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition hooks.txt:2054
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
you have access to all of the normal MediaWiki so you can get a DB use the cache
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))