MediaWiki  1.31.0
WANObjectCacheTest.php
Go to the documentation of this file.
1 <?php
2 
3 use Wikimedia\TestingAccessWrapper;
4 
19 class WANObjectCacheTest extends PHPUnit\Framework\TestCase {
20 
21  use MediaWikiCoversValidator;
22  use PHPUnit4And6Compat;
23 
25  private $cache;
27  private $internalCache;
28 
29  protected function setUp() {
30  parent::setUp();
31 
32  $this->cache = new TimeAdjustableWANObjectCache( [
33  'cache' => new TimeAdjustableHashBagOStuff(),
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  $priorTime = microtime( true ); // reference time
228  $cache->setTime( $priorTime );
229 
230  $cache->addTime( 1 );
231 
232  $wasSet = 0;
234  $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
235  );
236  $this->assertEquals( $value, $v, "Value returned" );
237  $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
238  $this->assertEquals( $value, $priorValue, "Has prior value" );
239  $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
240  $t1 = $cache->getCheckKeyTime( $cKey1 );
241  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
242  $t2 = $cache->getCheckKeyTime( $cKey2 );
243  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
244 
245  $priorTime = $cache->addTime( 0.01 );
246 
247  $wasSet = 0;
249  $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
250  );
251  $this->assertEquals( $value, $v, "Value returned" );
252  $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
253  $t1 = $cache->getCheckKeyTime( $cKey1 );
254  $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
255  $t2 = $cache->getCheckKeyTime( $cKey2 );
256  $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
257 
258  $curTTL = null;
259  $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
260  if ( $versioned ) {
261  $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
262  } else {
263  $this->assertEquals( $value, $v, "Value returned" );
264  }
265  $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
266 
267  $wasSet = 0;
268  $key = wfRandomString();
269  $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
270  $this->assertEquals( $value, $v, "Value returned" );
271  $cache->delete( $key );
272  $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
273  $this->assertEquals( $value, $v, "Value still returned after deleted" );
274  $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
275 
276  $oldValReceived = -1;
277  $oldAsOfReceived = -1;
278  $checkFunc = function ( $oldVal, &$ttl, array $setOpts, $oldAsOf )
279  use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) {
280  ++$wasSet;
281  $oldValReceived = $oldVal;
282  $oldAsOfReceived = $oldAsOf;
283 
284  return 'xxx' . $wasSet;
285  };
286 
287  $now = microtime( true ); // reference time
288 
289  $wasSet = 0;
290  $key = wfRandomString();
292  $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
293  $this->assertEquals( 'xxx1', $v, "Value returned" );
294  $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
295  $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
296 
297  $cache->addTime( 40 );
299  $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
300  $this->assertEquals( 'xxx2', $v, "Value still returned after expired" );
301  $this->assertEquals( 2, $wasSet, "Value recalculated while expired" );
302  $this->assertEquals( 'xxx1', $oldValReceived, "Callback got stale value" );
303  $this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" );
304 
305  $cache->addTime( 260 );
307  $key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts );
308  $this->assertEquals( 'xxx3', $v, "Value still returned after expired" );
309  $this->assertEquals( 3, $wasSet, "Value recalculated while expired" );
310  $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
311  $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
312 
313  $wasSet = 0;
314  $key = wfRandomString();
315  $checkKey = $cache->makeKey( 'template', 'X' );
316  $cache->setTime( $now - $cache::HOLDOFF_TTL - 1 );
317  $cache->touchCheckKey( $checkKey ); // init check key
318  $cache->setTime( $now );
320  $key,
321  $cache::TTL_INDEFINITE,
322  $checkFunc,
323  [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
324  );
325  $this->assertEquals( 'xxx1', $v, "Value returned" );
326  $this->assertEquals( 1, $wasSet, "Value computed" );
327  $this->assertEquals( false, $oldValReceived, "Callback got no stale value" );
328  $this->assertEquals( null, $oldAsOfReceived, "Callback got no stale value" );
329 
330  $cache->addTime( $cache::TTL_HOUR ); // some time passes
332  $key,
333  $cache::TTL_INDEFINITE,
334  $checkFunc,
335  [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
336  );
337  $this->assertEquals( 'xxx1', $v, "Cached value returned" );
338  $this->assertEquals( 1, $wasSet, "Cached value returned" );
339 
340  $cache->touchCheckKey( $checkKey ); // make key stale
341  $cache->addTime( 0.01 ); // ~1 week left of grace (barely stale to avoid refreshes)
342 
344  $key,
345  $cache::TTL_INDEFINITE,
346  $checkFunc,
347  [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
348  );
349  $this->assertEquals( 'xxx1', $v, "Value still returned after expired (in grace)" );
350  $this->assertEquals( 1, $wasSet, "Value still returned after expired (in grace)" );
351 
352  // Change of refresh increase to unity as staleness approaches graceTTL
353 
354  $cache->addTime( $cache::TTL_WEEK ); // 8 days of being stale
356  $key,
357  $cache::TTL_INDEFINITE,
358  $checkFunc,
359  [ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts
360  );
361  $this->assertEquals( 'xxx2', $v, "Value was recomputed (past grace)" );
362  $this->assertEquals( 2, $wasSet, "Value was recomputed (past grace)" );
363  $this->assertEquals( 'xxx1', $oldValReceived, "Callback got post-grace stale value" );
364  $this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" );
365  }
366 
367  public static function getWithSetCallback_provider() {
368  return [
369  [ [], false ],
370  [ [ 'version' => 1 ], true ]
371  ];
372  }
373 
374  public function testPreemtiveRefresh() {
375  $value = 'KatCafe';
376  $wasSet = 0;
377  $func = function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value )
378  {
379  ++$wasSet;
380  return $value;
381  };
382 
384  'cache' => new HashBagOStuff(),
385  'pool' => 'empty',
386  ] );
387 
388  $wasSet = 0;
389  $key = wfRandomString();
390  $opts = [ 'lowTTL' => 30 ];
391  $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
392  $this->assertEquals( $value, $v, "Value returned" );
393  $this->assertEquals( 1, $wasSet, "Value calculated" );
394  $v = $cache->getWithSetCallback( $key, 20, $func, $opts );
395  $this->assertEquals( 2, $wasSet, "Value re-calculated" );
396 
397  $wasSet = 0;
398  $key = wfRandomString();
399  $opts = [ 'lowTTL' => 1 ];
400  $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
401  $this->assertEquals( $value, $v, "Value returned" );
402  $this->assertEquals( 1, $wasSet, "Value calculated" );
403  $v = $cache->getWithSetCallback( $key, 30, $func, $opts );
404  $this->assertEquals( 1, $wasSet, "Value cached" );
405 
406  $asycList = [];
407  $asyncHandler = function ( $callback ) use ( &$asycList ) {
408  $asycList[] = $callback;
409  };
411  'cache' => new TimeAdjustableHashBagOStuff(),
412  'pool' => 'empty',
413  'asyncHandler' => $asyncHandler
414  ] );
415 
416  $now = microtime( true ); // reference time
417  $cache->setTime( $now );
418 
419  $wasSet = 0;
420  $key = wfRandomString();
421  $checkKey = wfRandomString();
422  $opts = [ 'lowTTL' => 100 ];
423  $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
424  $this->assertEquals( $value, $v, "Value returned" );
425  $this->assertEquals( 1, $wasSet, "Value calculated" );
426  $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
427  $this->assertEquals( 1, $wasSet, "Cached value used" );
428  $this->assertEquals( $v, $value, "Value cached" );
429 
430  $cache->setTime( $now + 250 );
431  $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
432  $this->assertEquals( $value, $v, "Value returned" );
433  $this->assertEquals( 1, $wasSet, "Stale value used" );
434  $this->assertEquals( 1, count( $asycList ), "Refresh deferred." );
435  $value = 'NewCatsInTown'; // change callback return value
436  $asycList[0](); // run the refresh callback
437  $asycList = [];
438  $this->assertEquals( 2, $wasSet, "Value calculated at later time" );
439  $this->assertEquals( 0, count( $asycList ), "No deferred refreshes added." );
440  $v = $cache->getWithSetCallback( $key, 300, $func, $opts );
441  $this->assertEquals( $value, $v, "New value stored" );
442 
444  'cache' => new TimeAdjustableHashBagOStuff(),
445  'pool' => 'empty'
446  ] );
447 
448  $cache->setTime( $now );
449 
450  $wasSet = 0;
451  $key = wfRandomString();
452  $opts = [ 'hotTTR' => 900 ];
453  $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
454  $this->assertEquals( $value, $v, "Value returned" );
455  $this->assertEquals( 1, $wasSet, "Value calculated" );
456  $cache->setTime( $now + 30 );
457  $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
458  $this->assertEquals( 1, $wasSet, "Value cached" );
459 
460  $wasSet = 0;
461  $key = wfRandomString();
462  $opts = [ 'hotTTR' => 10 ];
463  $cache->setTime( $now );
464  $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
465  $this->assertEquals( $value, $v, "Value returned" );
466  $this->assertEquals( 1, $wasSet, "Value calculated" );
467  $cache->setTime( $now + 30 );
468  $v = $cache->getWithSetCallback( $key, 60, $func, $opts );
469  $this->assertEquals( $value, $v, "Value returned" );
470  $this->assertEquals( 2, $wasSet, "Value re-calculated" );
471  }
472 
478  $this->setExpectedException( InvalidArgumentException::class );
479  $this->cache->getWithSetCallback( 'key', 30, 'invalid callback' );
480  }
481 
490  public function testGetMultiWithSetCallback( array $extOpts, $versioned ) {
492 
493  $keyA = wfRandomString();
494  $keyB = wfRandomString();
495  $keyC = wfRandomString();
496  $cKey1 = wfRandomString();
497  $cKey2 = wfRandomString();
498 
499  $priorValue = null;
500  $priorAsOf = null;
501  $wasSet = 0;
502  $genFunc = function ( $id, $old, &$ttl, &$opts, $asOf ) use (
503  &$wasSet, &$priorValue, &$priorAsOf
504  ) {
505  ++$wasSet;
506  $priorValue = $old;
507  $priorAsOf = $asOf;
508  $ttl = 20; // override with another value
509  return "@$id$";
510  };
511 
512  $wasSet = 0;
513  $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
514  $value = "@3353$";
516  $keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts );
517  $this->assertEquals( $value, $v[$keyA], "Value returned" );
518  $this->assertEquals( 1, $wasSet, "Value regenerated" );
519  $this->assertFalse( $priorValue, "No prior value" );
520  $this->assertNull( $priorAsOf, "No prior value" );
521 
522  $curTTL = null;
523  $cache->get( $keyA, $curTTL );
524  $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
525  $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
526 
527  $wasSet = 0;
528  $value = "@efef$";
529  $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
531  $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
532  $this->assertEquals( $value, $v[$keyB], "Value returned" );
533  $this->assertEquals( 1, $wasSet, "Value regenerated" );
534  $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
536  $keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5, ] + $extOpts );
537  $this->assertEquals( $value, $v[$keyB], "Value returned" );
538  $this->assertEquals( 1, $wasSet, "Value not regenerated" );
539  $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
540 
541  $priorTime = microtime( true ); // reference time
542  $cache->setTime( $priorTime );
543 
544  $cache->addTime( 1 );
545 
546  $wasSet = 0;
547  $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
549  $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
550  );
551  $this->assertEquals( $value, $v[$keyB], "Value returned" );
552  $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
553  $this->assertEquals( $value, $priorValue, "Has prior value" );
554  $this->assertInternalType( 'float', $priorAsOf, "Has prior value" );
555  $t1 = $cache->getCheckKeyTime( $cKey1 );
556  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
557  $t2 = $cache->getCheckKeyTime( $cKey2 );
558  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
559 
560  $priorTime = $cache->addTime( 0.01 );
561  $value = "@43636$";
562  $wasSet = 0;
563  $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
565  $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
566  );
567  $this->assertEquals( $value, $v[$keyC], "Value returned" );
568  $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
569  $t1 = $cache->getCheckKeyTime( $cKey1 );
570  $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
571  $t2 = $cache->getCheckKeyTime( $cKey2 );
572  $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
573 
574  $curTTL = null;
575  $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
576  if ( $versioned ) {
577  $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
578  } else {
579  $this->assertEquals( $value, $v, "Value returned" );
580  }
581  $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
582 
583  $wasSet = 0;
584  $key = wfRandomString();
585  $keyedIds = new ArrayIterator( [ $key => 242424 ] );
587  $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
588  $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
589  $cache->delete( $key );
590  $keyedIds = new ArrayIterator( [ $key => 242424 ] );
592  $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
593  $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
594  $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
595 
596  $calls = 0;
597  $ids = [ 1, 2, 3, 4, 5, 6 ];
598  $keyFunc = function ( $id, WANObjectCache $wanCache ) {
599  return $wanCache->makeKey( 'test', $id );
600  };
601  $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
602  $genFunc = function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) {
603  ++$calls;
604 
605  return "val-{$id}";
606  };
607  $values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
608 
609  $this->assertEquals(
610  [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
611  array_values( $values ),
612  "Correct values in correct order"
613  );
614  $this->assertEquals(
615  array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
616  array_keys( $values ),
617  "Correct keys in correct order"
618  );
619  $this->assertEquals( count( $ids ), $calls );
620 
621  $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc );
622  $this->assertEquals( count( $ids ), $calls, "Values cached" );
623 
624  // Mock the BagOStuff to assure only one getMulti() call given process caching
625  $localBag = $this->getMockBuilder( HashBagOStuff::class )
626  ->setMethods( [ 'getMulti' ] )->getMock();
627  $localBag->expects( $this->exactly( 1 ) )->method( 'getMulti' )->willReturn( [
628  WANObjectCache::VALUE_KEY_PREFIX . 'k1' => 'val-id1',
629  WANObjectCache::VALUE_KEY_PREFIX . 'k2' => 'val-id2'
630  ] );
631  $wanCache = new WANObjectCache( [ 'cache' => $localBag, 'pool' => 'testcache-hash' ] );
632 
633  // Warm the process cache
634  $keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] );
635  $this->assertEquals(
636  [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
637  $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
638  );
639  // Use the process cache
640  $this->assertEquals(
641  [ 'k1' => 'val-id1', 'k2' => 'val-id2' ],
642  $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] )
643  );
644  }
645 
646  public static function getMultiWithSetCallback_provider() {
647  return [
648  [ [], false ],
649  [ [ 'version' => 1 ], true ]
650  ];
651  }
652 
660  public function testGetMultiWithUnionSetCallback( array $extOpts, $versioned ) {
662 
663  $keyA = wfRandomString();
664  $keyB = wfRandomString();
665  $keyC = wfRandomString();
666  $cKey1 = wfRandomString();
667  $cKey2 = wfRandomString();
668 
669  $wasSet = 0;
670  $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use (
671  &$wasSet, &$priorValue, &$priorAsOf
672  ) {
673  $newValues = [];
674  foreach ( $ids as $id ) {
675  ++$wasSet;
676  $newValues[$id] = "@$id$";
677  $ttls[$id] = 20; // override with another value
678  }
679 
680  return $newValues;
681  };
682 
683  $wasSet = 0;
684  $keyedIds = new ArrayIterator( [ $keyA => 3353 ] );
685  $value = "@3353$";
687  $keyedIds, 30, $genFunc, $extOpts );
688  $this->assertEquals( $value, $v[$keyA], "Value returned" );
689  $this->assertEquals( 1, $wasSet, "Value regenerated" );
690 
691  $curTTL = null;
692  $cache->get( $keyA, $curTTL );
693  $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
694  $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
695 
696  $wasSet = 0;
697  $value = "@efef$";
698  $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
700  $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
701  $this->assertEquals( $value, $v[$keyB], "Value returned" );
702  $this->assertEquals( 1, $wasSet, "Value regenerated" );
703  $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed yet in process cache" );
705  $keyedIds, 30, $genFunc, [ 'lowTTL' => 0 ] + $extOpts );
706  $this->assertEquals( $value, $v[$keyB], "Value returned" );
707  $this->assertEquals( 1, $wasSet, "Value not regenerated" );
708  $this->assertEquals( 0, $cache->getWarmupKeyMisses(), "Keys warmed in process cache" );
709 
710  $priorTime = microtime( true ); // reference time
711  $cache->setTime( $priorTime );
712 
713  $cache->addTime( 1 );
714 
715  $wasSet = 0;
716  $keyedIds = new ArrayIterator( [ $keyB => 'efef' ] );
718  $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
719  );
720  $this->assertEquals( $value, $v[$keyB], "Value returned" );
721  $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
722  $t1 = $cache->getCheckKeyTime( $cKey1 );
723  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
724  $t2 = $cache->getCheckKeyTime( $cKey2 );
725  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
726 
727  $priorTime = $cache->addTime( 0.01 );
728  $value = "@43636$";
729  $wasSet = 0;
730  $keyedIds = new ArrayIterator( [ $keyC => 43636 ] );
732  $keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
733  );
734  $this->assertEquals( $value, $v[$keyC], "Value returned" );
735  $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
736  $t1 = $cache->getCheckKeyTime( $cKey1 );
737  $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
738  $t2 = $cache->getCheckKeyTime( $cKey2 );
739  $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
740 
741  $curTTL = null;
742  $v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
743  if ( $versioned ) {
744  $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
745  } else {
746  $this->assertEquals( $value, $v, "Value returned" );
747  }
748  $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
749 
750  $wasSet = 0;
751  $key = wfRandomString();
752  $keyedIds = new ArrayIterator( [ $key => 242424 ] );
754  $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
755  $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value returned" );
756  $cache->delete( $key );
757  $keyedIds = new ArrayIterator( [ $key => 242424 ] );
759  $keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts );
760  $this->assertEquals( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" );
761  $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
762 
763  $calls = 0;
764  $ids = [ 1, 2, 3, 4, 5, 6 ];
765  $keyFunc = function ( $id, WANObjectCache $wanCache ) {
766  return $wanCache->makeKey( 'test', $id );
767  };
768  $keyedIds = $cache->makeMultiKeys( $ids, $keyFunc );
769  $genFunc = function ( array $ids, array &$ttls, array &$setOpts ) use ( &$calls ) {
770  $newValues = [];
771  foreach ( $ids as $id ) {
772  ++$calls;
773  $newValues[$id] = "val-{$id}";
774  }
775 
776  return $newValues;
777  };
778  $values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
779 
780  $this->assertEquals(
781  [ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ],
782  array_values( $values ),
783  "Correct values in correct order"
784  );
785  $this->assertEquals(
786  array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $this->cache ) ),
787  array_keys( $values ),
788  "Correct keys in correct order"
789  );
790  $this->assertEquals( count( $ids ), $calls );
791 
792  $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc );
793  $this->assertEquals( count( $ids ), $calls, "Values cached" );
794  }
795 
796  public static function getMultiWithUnionSetCallback_provider() {
797  return [
798  [ [], false ],
799  [ [ 'version' => 1 ], true ]
800  ];
801  }
802 
807  public function testLockTSE() {
809  $key = wfRandomString();
811 
812  $calls = 0;
813  $func = function () use ( &$calls, $value, $cache, $key ) {
814  ++$calls;
815  return $value;
816  };
817 
818  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
819  $this->assertEquals( $value, $ret );
820  $this->assertEquals( 1, $calls, 'Value was populated' );
821 
822  // Acquire the mutex to verify that getWithSetCallback uses lockTSE properly
823  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
824 
825  $checkKeys = [ wfRandomString() ]; // new check keys => force misses
826  $ret = $cache->getWithSetCallback( $key, 30, $func,
827  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
828  $this->assertEquals( $value, $ret, 'Old value used' );
829  $this->assertEquals( 1, $calls, 'Callback was not used' );
830 
831  $cache->delete( $key );
832  $ret = $cache->getWithSetCallback( $key, 30, $func,
833  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
834  $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
835  $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
836 
837  $ret = $cache->getWithSetCallback( $key, 30, $func,
838  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
839  $this->assertEquals( $value, $ret, 'Callback was not used; used interim (mutex failed)' );
840  $this->assertEquals( 2, $calls, 'Callback was not used; used interim (mutex failed)' );
841  }
842 
848  public function testLockTSESlow() {
850  $key = wfRandomString();
852 
853  $calls = 0;
854  $func = function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value, $cache, $key ) {
855  ++$calls;
856  $setOpts['since'] = microtime( true ) - 10;
857  // Immediately kill any mutex rather than waiting a second
858  $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
859  return $value;
860  };
861 
862  // Value should be marked as stale due to snapshot lag
863  $curTTL = null;
864  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
865  $this->assertEquals( $value, $ret );
866  $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
867  $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
868  $this->assertEquals( 1, $calls, 'Value was generated' );
869 
870  // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
871  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
872  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
873  $this->assertEquals( $value, $ret );
874  $this->assertEquals( 1, $calls, 'Callback was not used' );
875  }
876 
881  public function testBusyValue() {
883  $key = wfRandomString();
885  $busyValue = wfRandomString();
886 
887  $calls = 0;
888  $func = function () use ( &$calls, $value, $cache, $key ) {
889  ++$calls;
890  // Immediately kill any mutex rather than waiting a second
891  $cache->delete( $cache::MUTEX_KEY_PREFIX . $key );
892  return $value;
893  };
894 
895  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
896  $this->assertEquals( $value, $ret );
897  $this->assertEquals( 1, $calls, 'Value was populated' );
898 
899  // Acquire a lock to verify that getWithSetCallback uses busyValue properly
900  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
901 
902  $checkKeys = [ wfRandomString() ]; // new check keys => force misses
903  $ret = $cache->getWithSetCallback( $key, 30, $func,
904  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
905  $this->assertEquals( $value, $ret, 'Callback used' );
906  $this->assertEquals( 2, $calls, 'Callback used' );
907 
908  $ret = $cache->getWithSetCallback( $key, 30, $func,
909  [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
910  $this->assertEquals( $value, $ret, 'Old value used' );
911  $this->assertEquals( 2, $calls, 'Callback was not used' );
912 
913  $cache->delete( $key ); // no value at all anymore and still locked
914  $ret = $cache->getWithSetCallback( $key, 30, $func,
915  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
916  $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
917  $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
918 
919  $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
920  $ret = $cache->getWithSetCallback( $key, 30, $func,
921  [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
922  $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
923  $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
924 
925  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
926  $ret = $cache->getWithSetCallback( $key, 30, $func,
927  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
928  $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
929  $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
930  }
931 
935  public function testGetMulti() {
937 
938  $value1 = [ 'this' => 'is', 'a' => 'test' ];
939  $value2 = [ 'this' => 'is', 'another' => 'test' ];
940 
941  $key1 = wfRandomString();
942  $key2 = wfRandomString();
943  $key3 = wfRandomString();
944 
945  $cache->set( $key1, $value1, 5 );
946  $cache->set( $key2, $value2, 10 );
947 
948  $curTTLs = [];
949  $this->assertEquals(
950  [ $key1 => $value1, $key2 => $value2 ],
951  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
952  'Result array populated'
953  );
954 
955  $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
956  $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
957  $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
958 
959  $cKey1 = wfRandomString();
960  $cKey2 = wfRandomString();
961 
962  $priorTime = microtime( true ); // reference time
963  $cache->setTime( $priorTime );
964 
965  $cache->addTime( 1 );
966 
967  $curTTLs = [];
968  $this->assertEquals(
969  [ $key1 => $value1, $key2 => $value2 ],
970  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
971  "Result array populated even with new check keys"
972  );
973  $t1 = $cache->getCheckKeyTime( $cKey1 );
974  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
975  $t2 = $cache->getCheckKeyTime( $cKey2 );
976  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
977  $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
978  $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
979  $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
980 
981  $cache->addTime( 1 );
982 
983  $curTTLs = [];
984  $this->assertEquals(
985  [ $key1 => $value1, $key2 => $value2 ],
986  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
987  "Result array still populated even with new check keys"
988  );
989  $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
990  $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
991  $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
992  }
993 
998  public function testGetMultiCheckKeys() {
1000 
1001  $checkAll = wfRandomString();
1002  $check1 = wfRandomString();
1003  $check2 = wfRandomString();
1004  $check3 = wfRandomString();
1005  $value1 = wfRandomString();
1006  $value2 = wfRandomString();
1007 
1008  $cache->setTime( microtime( true ) );
1009 
1010  // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
1011  // several seconds during the test to assert the behaviour.
1012  foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
1014  }
1015 
1016  $cache->addTime( 0.100 );
1017 
1018  $cache->set( 'key1', $value1, 10 );
1019  $cache->set( 'key2', $value2, 10 );
1020 
1021  $curTTLs = [];
1022  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1023  'key1' => $check1,
1024  $checkAll,
1025  'key2' => $check2,
1026  'key3' => $check3,
1027  ] );
1028  $this->assertEquals(
1029  [ 'key1' => $value1, 'key2' => $value2 ],
1030  $result,
1031  'Initial values'
1032  );
1033  $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
1034  $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
1035  $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
1036  $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
1037 
1038  $cache->addTime( 0.100 );
1039  $cache->touchCheckKey( $check1 );
1040 
1041  $curTTLs = [];
1042  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1043  'key1' => $check1,
1044  $checkAll,
1045  'key2' => $check2,
1046  'key3' => $check3,
1047  ] );
1048  $this->assertEquals(
1049  [ 'key1' => $value1, 'key2' => $value2 ],
1050  $result,
1051  'key1 expired by check1, but value still provided'
1052  );
1053  $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
1054  $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
1055 
1056  $cache->touchCheckKey( $checkAll );
1057 
1058  $curTTLs = [];
1059  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
1060  'key1' => $check1,
1061  $checkAll,
1062  'key2' => $check2,
1063  'key3' => $check3,
1064  ] );
1065  $this->assertEquals(
1066  [ 'key1' => $value1, 'key2' => $value2 ],
1067  $result,
1068  'All keys expired by checkAll, but value still provided'
1069  );
1070  $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
1071  $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
1072  }
1073 
1078  public function testCheckKeyInitHoldoff() {
1079  $cache = $this->cache;
1080 
1081  for ( $i = 0; $i < 500; ++$i ) {
1082  $key = wfRandomString();
1083  $checkKey = wfRandomString();
1084  // miss, set, hit
1085  $cache->get( $key, $curTTL, [ $checkKey ] );
1086  $cache->set( $key, 'val', 10 );
1087  $curTTL = null;
1088  $v = $cache->get( $key, $curTTL, [ $checkKey ] );
1089 
1090  $this->assertEquals( 'val', $v );
1091  $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
1092  }
1093 
1094  for ( $i = 0; $i < 500; ++$i ) {
1095  $key = wfRandomString();
1096  $checkKey = wfRandomString();
1097  // set, hit
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 (set/hit)" );
1104  }
1105  }
1106 
1112  public function testDelete() {
1113  $key = wfRandomString();
1114  $value = wfRandomString();
1115  $this->cache->set( $key, $value );
1116 
1117  $curTTL = null;
1118  $v = $this->cache->get( $key, $curTTL );
1119  $this->assertEquals( $value, $v, "Key was created with value" );
1120  $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1121 
1122  $this->cache->delete( $key );
1123 
1124  $curTTL = null;
1125  $v = $this->cache->get( $key, $curTTL );
1126  $this->assertFalse( $v, "Deleted key has false value" );
1127  $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
1128 
1129  $this->cache->set( $key, $value . 'more' );
1130  $v = $this->cache->get( $key, $curTTL );
1131  $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
1132  $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
1133 
1134  $this->cache->set( $key, $value );
1135  $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
1136 
1137  $curTTL = null;
1138  $v = $this->cache->get( $key, $curTTL );
1139  $this->assertFalse( $v, "Deleted key has false value" );
1140  $this->assertNull( $curTTL, "Deleted key has null current TTL" );
1141 
1142  $this->cache->set( $key, $value );
1143  $v = $this->cache->get( $key, $curTTL );
1144  $this->assertEquals( $value, $v, "Key was created with value" );
1145  $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
1146  }
1147 
1155  public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
1156  $cache = $this->cache;
1157 
1158  $key = wfRandomString();
1159  $valueV1 = wfRandomString();
1160  $valueV2 = [ wfRandomString() ];
1161 
1162  $wasSet = 0;
1163  $funcV1 = function () use ( &$wasSet, $valueV1 ) {
1164  ++$wasSet;
1165 
1166  return $valueV1;
1167  };
1168 
1169  $priorValue = false;
1170  $priorAsOf = null;
1171  $funcV2 = function ( $oldValue, &$ttl, $setOpts, $oldAsOf )
1172  use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) {
1173  $priorValue = $oldValue;
1174  $priorAsOf = $oldAsOf;
1175  ++$wasSet;
1176 
1177  return $valueV2; // new array format
1178  };
1179 
1180  // Set the main key (version N if versioned)
1181  $wasSet = 0;
1182  $v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1183  $this->assertEquals( $valueV1, $v, "Value returned" );
1184  $this->assertEquals( 1, $wasSet, "Value regenerated" );
1185  $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts );
1186  $this->assertEquals( 1, $wasSet, "Value not regenerated" );
1187  $this->assertEquals( $valueV1, $v, "Value not regenerated" );
1188 
1189  if ( $versioned ) {
1190  // Set the key for version N+1 format
1191  $verOpts = [ 'version' => $extOpts['version'] + 1 ];
1192  } else {
1193  // Start versioning now with the unversioned key still there
1194  $verOpts = [ 'version' => 1 ];
1195  }
1196 
1197  // Value goes to secondary key since V1 already used $key
1198  $wasSet = 0;
1199  $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1200  $this->assertEquals( $valueV2, $v, "Value returned" );
1201  $this->assertEquals( 1, $wasSet, "Value regenerated" );
1202  $this->assertEquals( false, $priorValue, "Old value not given due to old format" );
1203  $this->assertEquals( null, $priorAsOf, "Old value not given due to old format" );
1204 
1205  $wasSet = 0;
1206  $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1207  $this->assertEquals( $valueV2, $v, "Value not regenerated (secondary key)" );
1208  $this->assertEquals( 0, $wasSet, "Value not regenerated (secondary key)" );
1209 
1210  // Clear out the older or unversioned key
1211  $cache->delete( $key, 0 );
1212 
1213  // Set the key for next/first versioned format
1214  $wasSet = 0;
1215  $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1216  $this->assertEquals( $valueV2, $v, "Value returned" );
1217  $this->assertEquals( 1, $wasSet, "Value regenerated" );
1218 
1219  $v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts );
1220  $this->assertEquals( $valueV2, $v, "Value not regenerated (main key)" );
1221  $this->assertEquals( 1, $wasSet, "Value not regenerated (main key)" );
1222  }
1223 
1224  public static function getWithSetCallback_versions_provider() {
1225  return [
1226  [ [], false ],
1227  [ [ 'version' => 1 ], true ]
1228  ];
1229  }
1230 
1235  public function testInterimHoldOffCaching() {
1236  $cache = $this->cache;
1237 
1238  $value = 'CRL-40-940';
1239  $wasCalled = 0;
1240  $func = function () use ( &$wasCalled, $value ) {
1241  $wasCalled++;
1242 
1243  return $value;
1244  };
1245 
1247 
1248  $key = wfRandomString( 32 );
1249  $v = $cache->getWithSetCallback( $key, 60, $func );
1250  $v = $cache->getWithSetCallback( $key, 60, $func );
1251  $this->assertEquals( 1, $wasCalled, 'Value cached' );
1252  $cache->delete( $key );
1253  $v = $cache->getWithSetCallback( $key, 60, $func );
1254  $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1255  $v = $cache->getWithSetCallback( $key, 60, $func );
1256  $this->assertEquals( 3, $wasCalled, 'Value regenerated (got mutex)' ); // sets interim
1257  // Lock up the mutex so interim cache is used
1258  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1259  $v = $cache->getWithSetCallback( $key, 60, $func );
1260  $this->assertEquals( 3, $wasCalled, 'Value interim cached (failed mutex)' );
1261  $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
1262 
1263  $cache->useInterimHoldOffCaching( false );
1264 
1265  $wasCalled = 0;
1266  $key = wfRandomString( 32 );
1267  $v = $cache->getWithSetCallback( $key, 60, $func );
1268  $v = $cache->getWithSetCallback( $key, 60, $func );
1269  $this->assertEquals( 1, $wasCalled, 'Value cached' );
1270  $cache->delete( $key );
1271  $v = $cache->getWithSetCallback( $key, 60, $func );
1272  $this->assertEquals( 2, $wasCalled, 'Value regenerated (got mutex)' );
1273  $v = $cache->getWithSetCallback( $key, 60, $func );
1274  $this->assertEquals( 3, $wasCalled, 'Value still regenerated (got mutex)' );
1275  $v = $cache->getWithSetCallback( $key, 60, $func );
1276  $this->assertEquals( 4, $wasCalled, 'Value still regenerated (got mutex)' );
1277  // Lock up the mutex so interim cache is used
1278  $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
1279  $v = $cache->getWithSetCallback( $key, 60, $func );
1280  $this->assertEquals( 5, $wasCalled, 'Value still regenerated (failed mutex)' );
1281  }
1282 
1291  public function testTouchKeys() {
1292  $cache = $this->cache;
1293  $key = wfRandomString();
1294 
1295  $priorTime = microtime( true ); // reference time
1296  $cache->setTime( $priorTime );
1297 
1298  $newTime = $cache->addTime( 0.100 );
1299  $t0 = $cache->getCheckKeyTime( $key );
1300  $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
1301 
1302  $priorTime = $newTime;
1303  $newTime = $cache->addTime( 0.100 );
1304  $cache->touchCheckKey( $key );
1305  $t1 = $cache->getCheckKeyTime( $key );
1306  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
1307 
1308  $t2 = $cache->getCheckKeyTime( $key );
1309  $this->assertEquals( $t1, $t2, 'Check key time did not change' );
1310 
1311  $cache->addTime( 0.100 );
1312  $cache->touchCheckKey( $key );
1313  $t3 = $cache->getCheckKeyTime( $key );
1314  $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
1315 
1316  $t4 = $cache->getCheckKeyTime( $key );
1317  $this->assertEquals( $t3, $t4, 'Check key time did not change' );
1318 
1319  $cache->addTime( 0.100 );
1320  $cache->resetCheckKey( $key );
1321  $t5 = $cache->getCheckKeyTime( $key );
1322  $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
1323 
1324  $t6 = $cache->getCheckKeyTime( $key );
1325  $this->assertEquals( $t5, $t6, 'Check key time did not change' );
1326  }
1327 
1331  public function testGetWithSeveralCheckKeys() {
1332  $key = wfRandomString();
1333  $tKey1 = wfRandomString();
1334  $tKey2 = wfRandomString();
1335  $value = 'meow';
1336 
1337  // Two check keys are newer (given hold-off) than $key, another is older
1338  $this->internalCache->set(
1340  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
1341  );
1342  $this->internalCache->set(
1344  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
1345  );
1346  $this->internalCache->set(
1348  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
1349  );
1350  $this->cache->set( $key, $value, 30 );
1351 
1352  $curTTL = null;
1353  $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1354  $this->assertEquals( $value, $v, "Value matches" );
1355  $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
1356  $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
1357  }
1358 
1363  public function testReap() {
1364  $vKey1 = wfRandomString();
1365  $vKey2 = wfRandomString();
1366  $tKey1 = wfRandomString();
1367  $tKey2 = wfRandomString();
1368  $value = 'moo';
1369 
1370  $knownPurge = time() - 60;
1371  $goodTime = microtime( true ) - 5;
1372  $badTime = microtime( true ) - 300;
1373 
1374  $this->internalCache->set(
1376  [
1379  WANObjectCache::FLD_TTL => 3600,
1380  WANObjectCache::FLD_TIME => $goodTime
1381  ]
1382  );
1383  $this->internalCache->set(
1385  [
1388  WANObjectCache::FLD_TTL => 3600,
1389  WANObjectCache::FLD_TIME => $badTime
1390  ]
1391  );
1392  $this->internalCache->set(
1395  );
1396  $this->internalCache->set(
1399  );
1400 
1401  $this->assertEquals( $value, $this->cache->get( $vKey1 ) );
1402  $this->assertEquals( $value, $this->cache->get( $vKey2 ) );
1403  $this->cache->reap( $vKey1, $knownPurge, $bad1 );
1404  $this->cache->reap( $vKey2, $knownPurge, $bad2 );
1405 
1406  $this->assertFalse( $bad1 );
1407  $this->assertTrue( $bad2 );
1408 
1409  $this->cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1410  $this->cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1411  $this->assertFalse( $tBad1 );
1412  $this->assertTrue( $tBad2 );
1413  }
1414 
1418  public function testReap_fail() {
1419  $backend = $this->getMockBuilder( EmptyBagOStuff::class )
1420  ->setMethods( [ 'get', 'changeTTL' ] )->getMock();
1421  $backend->expects( $this->once() )->method( 'get' )
1422  ->willReturn( [
1424  WANObjectCache::FLD_VALUE => 'value',
1425  WANObjectCache::FLD_TTL => 3600,
1426  WANObjectCache::FLD_TIME => 300,
1427  ] );
1428  $backend->expects( $this->once() )->method( 'changeTTL' )
1429  ->willReturn( false );
1430 
1431  $wanCache = new WANObjectCache( [
1432  'cache' => $backend,
1433  'pool' => 'testcache-hash',
1434  'relayer' => new EventRelayerNull( [] )
1435  ] );
1436 
1437  $isStale = null;
1438  $ret = $wanCache->reap( 'key', 360, $isStale );
1439  $this->assertTrue( $isStale, 'value was stale' );
1440  $this->assertFalse( $ret, 'changeTTL failed' );
1441  }
1442 
1446  public function testSetWithLag() {
1447  $value = 1;
1448 
1449  $key = wfRandomString();
1450  $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
1451  $this->cache->set( $key, $value, 30, $opts );
1452  $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
1453 
1454  $key = wfRandomString();
1455  $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
1456  $this->cache->set( $key, $value, 30, $opts );
1457  $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
1458 
1459  $key = wfRandomString();
1460  $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
1461  $this->cache->set( $key, $value, 30, $opts );
1462  $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
1463  }
1464 
1468  public function testWritePending() {
1469  $value = 1;
1470 
1471  $key = wfRandomString();
1472  $opts = [ 'pending' => true ];
1473  $this->cache->set( $key, $value, 30, $opts );
1474  $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
1475  }
1476 
1477  public function testMcRouterSupport() {
1478  $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1479  ->setMethods( [ 'set', 'delete' ] )->getMock();
1480  $localBag->expects( $this->never() )->method( 'set' );
1481  $localBag->expects( $this->never() )->method( 'delete' );
1482  $wanCache = new WANObjectCache( [
1483  'cache' => $localBag,
1484  'pool' => 'testcache-hash',
1485  'relayer' => new EventRelayerNull( [] ),
1486  'mcrouterAware' => true,
1487  'region' => 'pmtpa',
1488  'cluster' => 'mw-wan'
1489  ] );
1490  $valFunc = function () {
1491  return 1;
1492  };
1493 
1494  // None of these should use broadcasting commands (e.g. SET, DELETE)
1495  $wanCache->get( 'x' );
1496  $wanCache->get( 'x', $ctl, [ 'check1' ] );
1497  $wanCache->getMulti( [ 'x', 'y' ] );
1498  $wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] );
1499  $wanCache->getWithSetCallback( 'p', 30, $valFunc );
1500  $wanCache->getCheckKeyTime( 'zzz' );
1501  $wanCache->reap( 'x', time() - 300 );
1502  $wanCache->reap( 'zzz', time() - 300 );
1503  }
1504 
1506  $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1507  ->setMethods( [ 'set' ] )->getMock();
1508  $wanCache = new WANObjectCache( [
1509  'cache' => $localBag,
1510  'pool' => 'testcache-hash',
1511  'relayer' => new EventRelayerNull( [] ),
1512  'mcrouterAware' => true,
1513  'region' => 'pmtpa',
1514  'cluster' => 'mw-wan'
1515  ] );
1516 
1517  $localBag->expects( $this->once() )->method( 'set' )
1518  ->with( "/*/mw-wan/" . $wanCache::VALUE_KEY_PREFIX . "test" );
1519 
1520  $wanCache->delete( 'test' );
1521  }
1522 
1524  $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1525  ->setMethods( [ 'set' ] )->getMock();
1526  $wanCache = new WANObjectCache( [
1527  'cache' => $localBag,
1528  'pool' => 'testcache-hash',
1529  'relayer' => new EventRelayerNull( [] ),
1530  'mcrouterAware' => true,
1531  'region' => 'pmtpa',
1532  'cluster' => 'mw-wan'
1533  ] );
1534 
1535  $localBag->expects( $this->once() )->method( 'set' )
1536  ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1537 
1538  $wanCache->touchCheckKey( 'test' );
1539  }
1540 
1542  $localBag = $this->getMockBuilder( EmptyBagOStuff::class )
1543  ->setMethods( [ 'delete' ] )->getMock();
1544  $wanCache = new WANObjectCache( [
1545  'cache' => $localBag,
1546  'pool' => 'testcache-hash',
1547  'relayer' => new EventRelayerNull( [] ),
1548  'mcrouterAware' => true,
1549  'region' => 'pmtpa',
1550  'cluster' => 'mw-wan'
1551  ] );
1552 
1553  $localBag->expects( $this->once() )->method( 'delete' )
1554  ->with( "/*/mw-wan/" . $wanCache::TIME_KEY_PREFIX . "test" );
1555 
1556  $wanCache->resetCheckKey( 'test' );
1557  }
1558 
1568  public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) {
1569  $mtime = $ago ? time() - $ago : $ago;
1570  $margin = 5;
1571  $ttl = $this->cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1572 
1573  $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1574  $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1575 
1576  $ttl = $this->cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor );
1577 
1578  $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1579  $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1580  }
1581 
1582  public static function provideAdaptiveTTL() {
1583  return [
1584  [ 3600, 900, 30, 0.2, 720 ],
1585  [ 3600, 500, 30, 0.2, 500 ],
1586  [ 3600, 86400, 800, 0.2, 800 ],
1587  [ false, 86400, 800, 0.2, 800 ],
1588  [ null, 86400, 800, 0.2, 800 ]
1589  ];
1590  }
1591 
1596  public function testNewEmpty() {
1597  $this->assertInstanceOf(
1600  );
1601  }
1602 
1606  public function testSetLogger() {
1607  $this->assertSame( null, $this->cache->setLogger( new Psr\Log\NullLogger ) );
1608  }
1609 
1613  public function testGetQoS() {
1614  $backend = $this->getMockBuilder( HashBagOStuff::class )
1615  ->setMethods( [ 'getQoS' ] )->getMock();
1616  $backend->expects( $this->once() )->method( 'getQoS' )
1617  ->willReturn( BagOStuff::QOS_UNKNOWN );
1618  $wanCache = new WANObjectCache( [ 'cache' => $backend ] );
1619 
1620  $this->assertSame(
1621  $wanCache::QOS_UNKNOWN,
1622  $wanCache->getQoS( $wanCache::ATTR_EMULATION )
1623  );
1624  }
1625 
1629  public function testMakeKey() {
1630  $backend = $this->getMockBuilder( HashBagOStuff::class )
1631  ->setMethods( [ 'makeKey' ] )->getMock();
1632  $backend->expects( $this->once() )->method( 'makeKey' )
1633  ->willReturn( 'special' );
1634 
1635  $wanCache = new WANObjectCache( [
1636  'cache' => $backend,
1637  'pool' => 'testcache-hash',
1638  'relayer' => new EventRelayerNull( [] )
1639  ] );
1640 
1641  $this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) );
1642  }
1643 
1647  public function testMakeGlobalKey() {
1648  $backend = $this->getMockBuilder( HashBagOStuff::class )
1649  ->setMethods( [ 'makeGlobalKey' ] )->getMock();
1650  $backend->expects( $this->once() )->method( 'makeGlobalKey' )
1651  ->willReturn( 'special' );
1652 
1653  $wanCache = new WANObjectCache( [
1654  'cache' => $backend,
1655  'pool' => 'testcache-hash',
1656  'relayer' => new EventRelayerNull( [] )
1657  ] );
1658 
1659  $this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) );
1660  }
1661 
1662  public static function statsKeyProvider() {
1663  return [
1664  [ 'domain:page:5', 'page' ],
1665  [ 'domain:main-key', 'main-key' ],
1666  [ 'domain:page:history', 'page' ],
1667  [ 'missingdomainkey', 'missingdomainkey' ]
1668  ];
1669  }
1670 
1675  public function testStatsKeyClass( $key, $class ) {
1676  $wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [
1677  'cache' => new HashBagOStuff,
1678  'pool' => 'testcache-hash',
1679  'relayer' => new EventRelayerNull( [] )
1680  ] ) );
1681 
1682  $this->assertEquals( $class, $wanCache->determineKeyClass( $key ) );
1683  }
1684 }
1685 
1687  private $timeOverride = 0;
1688 
1689  public function setTime( $time ) {
1690  $this->timeOverride = $time;
1691  }
1692 
1693  protected function getCurrentTime() {
1694  return $this->timeOverride ?: parent::getCurrentTime();
1695  }
1696 }
1697 
1699  private $timeOverride = 0;
1700 
1701  public function setTime( $time ) {
1702  $this->timeOverride = $time;
1703  if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1704  $this->cache->setTime( $time );
1705  }
1706  }
1707 
1708  public function addTime( $time ) {
1709  $this->timeOverride += $time;
1710  if ( $this->cache instanceof TimeAdjustableHashBagOStuff ) {
1711  $this->cache->setTime( $this->timeOverride );
1712  }
1713 
1714  return $this->timeOverride;
1715  }
1716 
1717  protected function getCurrentTime() {
1718  return $this->timeOverride ?: parent::getCurrentTime();
1719  }
1720 }
1721 
1723  const CLOCK_SKEW = 1;
1724 
1725  protected function worthRefreshExpiring( $curTTL, $lowTTL ) {
1726  return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL );
1727  }
1728 }
1729 
1731  protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) {
1732  return ( ( $now - $asOf ) > $timeTillRefresh );
1733  }
1734 }
WANObjectCacheTest\testStaleSet
testStaleSet()
WANObjectCache::set()
Definition: WANObjectCacheTest.php:117
WANObjectCacheTest\testSetWithLag
testSetWithLag()
WANObjectCache::set()
Definition: WANObjectCacheTest.php:1446
WANObjectCacheTest\testSetOver
testSetOver()
WANObjectCache::set()
Definition: WANObjectCacheTest.php:104
PopularityRefreshingWANObjectCache
Definition: WANObjectCacheTest.php:1730
object
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 For a description of the see design txt $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:25
WANObjectCacheTest\getWithSetCallback_versions_provider
static getWithSetCallback_versions_provider()
Definition: WANObjectCacheTest.php:1224
TimeAdjustableWANObjectCache\addTime
addTime( $time)
Definition: WANObjectCacheTest.php:1708
WANObjectCache\VALUE_KEY_PREFIX
const VALUE_KEY_PREFIX
Definition: WANObjectCache.php:185
WANObjectCacheTest\testGetMultiWithSetCallback
testGetMultiWithSetCallback(array $extOpts, $versioned)
getMultiWithSetCallback_provider WANObjectCache::getMultiWithSetCallback WANObjectCache::makeMultiKey...
Definition: WANObjectCacheTest.php:490
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
HashBagOStuff
Simple store for keeping values in an associative array for the current process.
Definition: HashBagOStuff.php:31
TimeAdjustableWANObjectCache
Definition: WANObjectCacheTest.php:1698
WANObjectCacheTest\testAdaptiveTTL
testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL)
provideAdaptiveTTL WANObjectCache::adaptiveTTL()
Definition: WANObjectCacheTest.php:1568
captcha-old.count
count
Definition: captcha-old.py:249
WANObjectCache\FLD_VERSION
const FLD_VERSION
Definition: WANObjectCache.php:169
WANObjectCache\FLD_VALUE
const FLD_VALUE
Definition: WANObjectCache.php:170
WANObjectCacheTest\testGetWithSetCallback
testGetWithSetCallback(array $extOpts, $versioned)
getWithSetCallback_provider WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback...
Definition: WANObjectCacheTest.php:188
$result
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. '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! 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! 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:1985
PopularityRefreshingWANObjectCache\worthRefreshPopular
worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now)
Check if a key is due for randomized regeneration due to its popularity.
Definition: WANObjectCacheTest.php:1731
TimeAdjustableHashBagOStuff\getCurrentTime
getCurrentTime()
Definition: WANObjectCacheTest.php:1693
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
EventRelayerNull
No-op class for publishing messages into a PubSub system.
Definition: EventRelayerNull.php:24
WANObjectCache\makeMultiKeys
makeMultiKeys(array $entities, callable $keyFunc)
Definition: WANObjectCache.php:1625
WANObjectCacheTest\provideAdaptiveTTL
static provideAdaptiveTTL()
Definition: WANObjectCacheTest.php:1582
WANObjectCache\getMultiWithSetCallback
getMultiWithSetCallback(ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[])
Method to fetch multiple cache keys at once with regeneration.
Definition: WANObjectCache.php:1380
WANObjectCacheTest\getWithSetCallback_provider
static getWithSetCallback_provider()
Definition: WANObjectCacheTest.php:367
WANObjectCache\set
set( $key, $value, $ttl=0, array $opts=[])
Set the value of a key in cache.
Definition: WANObjectCache.php:495
WANObjectCacheTest\$cache
TimeAdjustableWANObjectCache $cache
Definition: WANObjectCacheTest.php:25
BagOStuff
interface is intended to be more or less compatible with the PHP memcached client.
Definition: BagOStuff.php:47
WANObjectCacheTest\testGetWithSeveralCheckKeys
testGetWithSeveralCheckKeys()
WANObjectCache::getMulti()
Definition: WANObjectCacheTest.php:1331
WANObjectCache\getMultiWithUnionSetCallback
getMultiWithUnionSetCallback(ArrayIterator $keyedIds, $ttl, callable $callback, array $opts=[])
Method to fetch/regenerate multiple cache keys at once.
Definition: WANObjectCache.php:1474
WANObjectCacheTest\testWritePending
testWritePending()
WANObjectCache::set()
Definition: WANObjectCacheTest.php:1468
WANObjectCacheTest\testSetAndGet
testSetAndGet( $value, $ttl)
provideSetAndGet WANObjectCache::set() WANObjectCache::get() WANObjectCache::makeKey()
Definition: WANObjectCacheTest.php:51
NearExpiringWANObjectCache
Definition: WANObjectCacheTest.php:1722
cache
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
WANObjectCacheTest\testSetLogger
testSetLogger()
WANObjectCache::setLogger.
Definition: WANObjectCacheTest.php:1606
php
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:35
WANObjectCache\getCheckKeyTime
getCheckKeyTime( $key)
Fetch the value of a timestamp "check" key.
Definition: WANObjectCache.php:643
WANObjectCacheTest\testProcessCache
testProcessCache()
Definition: WANObjectCacheTest.php:125
NearExpiringWANObjectCache\worthRefreshExpiring
worthRefreshExpiring( $curTTL, $lowTTL)
Check if a key is nearing expiration and thus due for randomized regeneration.
Definition: WANObjectCacheTest.php:1725
WANObjectCache\newEmpty
static newEmpty()
Get an instance that wraps EmptyBagOStuff.
Definition: WANObjectCache.php:253
WANObjectCacheTest\testInterimHoldOffCaching
testInterimHoldOffCaching()
WANObjectCache::useInterimHoldOffCaching WANObjectCache::getInterimValue.
Definition: WANObjectCacheTest.php:1235
WANObjectCache\TIME_KEY_PREFIX
const TIME_KEY_PREFIX
Definition: WANObjectCache.php:187
WANObjectCacheTest\testGetMultiWithUnionSetCallback
testGetMultiWithUnionSetCallback(array $extOpts, $versioned)
getMultiWithUnionSetCallback_provider WANObjectCache::getMultiWithUnionSetCallback() WANObjectCache::...
Definition: WANObjectCacheTest.php:660
WANObjectCache\FLD_TIME
const FLD_TIME
Definition: WANObjectCache.php:172
WANObjectCacheTest\testMakeGlobalKey
testMakeGlobalKey()
WANObjectCache::makeGlobalKey.
Definition: WANObjectCacheTest.php:1647
WANObjectCacheTest\testGetNotExists
testGetNotExists()
WANObjectCache::get() WANObjectCache::makeGlobalKey()
Definition: WANObjectCacheTest.php:92
WANObjectCache\getWithSetCallback
getWithSetCallback( $key, $ttl, $callback, array $opts=[])
Method to fetch/regenerate cache keys.
Definition: WANObjectCache.php:1054
WANObjectCacheTest\testDelete
testDelete()
WANObjectCache::delete WANObjectCache::relayDelete WANObjectCache::relayPurge.
Definition: WANObjectCacheTest.php:1112
WANObjectCacheTest\testGetWithSetCallback_invalidCallback
testGetWithSetCallback_invalidCallback()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
Definition: WANObjectCacheTest.php:477
WANObjectCacheTest\testMcRouterSupportBroadcastTouchCK
testMcRouterSupportBroadcastTouchCK()
Definition: WANObjectCacheTest.php:1523
WANObjectCache\useInterimHoldOffCaching
useInterimHoldOffCaching( $enabled)
Enable or disable the use of brief caching for tombstoned keys.
Definition: WANObjectCache.php:1698
WANObjectCache\touchCheckKey
touchCheckKey( $key, $holdoff=self::HOLDOFF_TTL)
Purge a "check" key from all datacenters, invalidating keys that use it.
Definition: WANObjectCache.php:773
TimeAdjustableHashBagOStuff\setTime
setTime( $time)
Definition: WANObjectCacheTest.php:1689
$time
see documentation in includes Linker php for Linker::makeImageLink & $time
Definition: hooks.txt:1795
WANObjectCacheTest\testReap_fail
testReap_fail()
WANObjectCache::reap()
Definition: WANObjectCacheTest.php:1418
WANObjectCacheTest\$internalCache
BagOStuff $internalCache
Definition: WANObjectCacheTest.php:27
WANObjectCacheTest\setUp
setUp()
Definition: WANObjectCacheTest.php:29
IExpiringStore\QOS_UNKNOWN
const QOS_UNKNOWN
Definition: IExpiringStore.php:58
WANObjectCacheTest\testCheckKeyInitHoldoff
testCheckKeyInitHoldoff()
WANObjectCache::get() WANObjectCache::processCheckKeys()
Definition: WANObjectCacheTest.php:1078
WANObjectCache\get
get( $key, &$curTTL=null, array $checkKeys=[], &$asOf=null)
Fetch the value of a key from cache.
Definition: WANObjectCache.php:300
$value
$value
Definition: styleTest.css.php:45
WANObjectCacheTest\testGetMultiCheckKeys
testGetMultiCheckKeys()
WANObjectCache::getMulti() WANObjectCache::processCheckKeys()
Definition: WANObjectCacheTest.php:998
WANObjectCacheTest\testLockTSESlow
testLockTSESlow()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback() WANObjectCache::set()
Definition: WANObjectCacheTest.php:848
TimeAdjustableWANObjectCache\getCurrentTime
getCurrentTime()
Definition: WANObjectCacheTest.php:1717
WANObjectCacheTest\testBusyValue
testBusyValue()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
Definition: WANObjectCacheTest.php:881
WANObjectCache
Multi-datacenter aware caching interface.
Definition: WANObjectCache.php:87
WANObjectCache\VERSION
const VERSION
Cache format version number.
Definition: WANObjectCache.php:167
WANObjectCacheTest\testStatsKeyClass
testStatsKeyClass( $key, $class)
statsKeyProvider WANObjectCache::determineKeyClass
Definition: WANObjectCacheTest.php:1675
TimeAdjustableWANObjectCache\$timeOverride
$timeOverride
Definition: WANObjectCacheTest.php:1699
$ret
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:1987
WANObjectCache\makeKey
makeKey( $class, $component=null)
Definition: WANObjectCache.php:1604
WANObjectCacheTest\testPreemtiveRefresh
testPreemtiveRefresh()
Definition: WANObjectCacheTest.php:374
WANObjectCache\getMulti
getMulti(array $keys, &$curTTLs=[], array $checkKeys=[], array &$asOfs=[])
Fetch the value of several keys from cache.
Definition: WANObjectCache.php:322
WANObjectCacheTest\testMcRouterSupportBroadcastResetCK
testMcRouterSupportBroadcastResetCK()
Definition: WANObjectCacheTest.php:1541
WANObjectCache\resetCheckKey
resetCheckKey( $key)
Delete a "check" key from all datacenters, invalidating keys that use it.
Definition: WANObjectCache.php:805
WANObjectCacheTest\testTouchKeys
testTouchKeys()
WANObjectCache::touchCheckKey WANObjectCache::resetCheckKey WANObjectCache::getCheckKeyTime WANObject...
Definition: WANObjectCacheTest.php:1291
WANObjectCacheTest\testLockTSE
testLockTSE()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
Definition: WANObjectCacheTest.php:807
WANObjectCacheTest\testMcRouterSupport
testMcRouterSupport()
Definition: WANObjectCacheTest.php:1477
WANObjectCacheTest\testGetQoS
testGetQoS()
WANObjectCache::getQoS.
Definition: WANObjectCacheTest.php:1613
WANObjectCacheTest\testNewEmpty
testNewEmpty()
WANObjectCache::__construct WANObjectCache::newEmpty.
Definition: WANObjectCacheTest.php:1596
WANObjectCache\PURGE_VAL_PREFIX
const PURGE_VAL_PREFIX
Definition: WANObjectCache.php:190
WANObjectCache\getWarmupKeyMisses
getWarmupKeyMisses()
Definition: WANObjectCache.php:1792
as
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
Definition: distributors.txt:9
WANObjectCacheTest\statsKeyProvider
static statsKeyProvider()
Definition: WANObjectCacheTest.php:1662
TimeAdjustableWANObjectCache\setTime
setTime( $time)
Definition: WANObjectCacheTest.php:1701
WANObjectCacheTest\testReap
testReap()
WANObjectCache::reap() WANObjectCache::reapCheckKey()
Definition: WANObjectCacheTest.php:1363
$keys
$keys
Definition: testCompression.php:67
WANObjectCacheTest\provideSetAndGet
static provideSetAndGet()
Definition: WANObjectCacheTest.php:74
WANObjectCacheTest\testGetMulti
testGetMulti()
WANObjectCache::getMulti()
Definition: WANObjectCacheTest.php:935
WANObjectCache\HOLDOFF_NONE
const HOLDOFF_NONE
Idiom for delete() for "no hold-off".
Definition: WANObjectCache.php:154
true
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:1987
TimeAdjustableHashBagOStuff\$timeOverride
$timeOverride
Definition: WANObjectCacheTest.php:1687
WANObjectCacheTest\testMakeKey
testMakeKey()
WANObjectCache::makeKey.
Definition: WANObjectCacheTest.php:1629
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
$t
$t
Definition: testCompression.php:69
WANObjectCacheTest\getMultiWithSetCallback_provider
static getMultiWithSetCallback_provider()
Definition: WANObjectCacheTest.php:646
WANObjectCacheTest\testMcRouterSupportBroadcastDelete
testMcRouterSupportBroadcastDelete()
Definition: WANObjectCacheTest.php:1505
NearExpiringWANObjectCache\CLOCK_SKEW
const CLOCK_SKEW
Definition: WANObjectCacheTest.php:1723
WANObjectCacheTest\getMultiWithUnionSetCallback_provider
static getMultiWithUnionSetCallback_provider()
Definition: WANObjectCacheTest.php:796
WANObjectCache\FLD_TTL
const FLD_TTL
Definition: WANObjectCache.php:171
WANObjectCache\delete
delete( $key, $ttl=self::HOLDOFF_TTL)
Purge a key from all datacenters.
Definition: WANObjectCache.php:610
WANObjectCacheTest\testGetWithSetCallback_versions
testGetWithSetCallback_versions(array $extOpts, $versioned)
getWithSetCallback_versions_provider WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSe...
Definition: WANObjectCacheTest.php:1155
array
the array() calling protocol came about after MediaWiki 1.4rc1.
TimeAdjustableHashBagOStuff
Definition: WANObjectCacheTest.php:1686
WANObjectCacheTest
WANObjectCache::wrap WANObjectCache::unwrap WANObjectCache::worthRefreshExpiring WANObjectCache::wort...
Definition: WANObjectCacheTest.php:19
wfRandomString
wfRandomString( $length=32)
Get a random string containing a number of pseudo-random hex characters.
Definition: GlobalFunctions.php:305