MediaWiki  master
WANObjectCacheTest.php
Go to the documentation of this file.
1 <?php
2 
5  private $cache;
7  private $internalCache;
8 
9  protected function setUp() {
10  parent::setUp();
11 
12  if ( $this->getCliArg( 'use-wanobjectcache' ) ) {
13  $name = $this->getCliArg( 'use-wanobjectcache' );
14 
16  } else {
17  $this->cache = new WANObjectCache( [
18  'cache' => new HashBagOStuff(),
19  'pool' => 'testcache-hash',
20  'relayer' => new EventRelayerNull( [] )
21  ] );
22  }
23 
24  $wanCache = TestingAccessWrapper::newFromObject( $this->cache );
25  $this->internalCache = $wanCache->cache;
26  }
27 
35  public function testSetAndGet( $value, $ttl ) {
36  $curTTL = null;
37  $asOf = null;
38  $key = wfRandomString();
39 
40  $this->cache->get( $key, $curTTL, [], $asOf );
41  $this->assertNull( $curTTL, "Current TTL is null" );
42  $this->assertNull( $asOf, "Current as-of-time is infinite" );
43 
44  $t = microtime( true );
45  $this->cache->set( $key, $value, $ttl );
46 
47  $this->assertEquals( $value, $this->cache->get( $key, $curTTL, [], $asOf ) );
48  if ( is_infinite( $ttl ) || $ttl == 0 ) {
49  $this->assertTrue( is_infinite( $curTTL ), "Current TTL is infinite" );
50  } else {
51  $this->assertGreaterThan( 0, $curTTL, "Current TTL > 0" );
52  $this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" );
53  }
54  $this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" );
55  $this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" );
56  }
57 
58  public static function provideSetAndGet() {
59  return [
60  [ 14141, 3 ],
61  [ 3535.666, 3 ],
62  [ [], 3 ],
63  [ null, 3 ],
64  [ '0', 3 ],
65  [ (object)[ 'meow' ], 3 ],
66  [ INF, 3 ],
67  [ '', 3 ],
68  [ 'pizzacat', INF ],
69  ];
70  }
71 
75  public function testGetNotExists() {
76  $key = wfRandomString();
77  $curTTL = null;
78  $value = $this->cache->get( $key, $curTTL );
79 
80  $this->assertFalse( $value, "Non-existing key has false value" );
81  $this->assertNull( $curTTL, "Non-existing key has null current TTL" );
82  }
83 
87  public function testSetOver() {
88  $key = wfRandomString();
89  for ( $i = 0; $i < 3; ++$i ) {
91  $this->cache->set( $key, $value, 3 );
92 
93  $this->assertEquals( $this->cache->get( $key ), $value );
94  }
95  }
96 
100  public function testStaleSet() {
101  $key = wfRandomString();
103  $this->cache->set( $key, $value, 3, [ 'since' => microtime( true ) - 30 ] );
104 
105  $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" );
106  }
107 
115  public function testGetWithSetCallback( array $extOpts, $versioned ) {
117 
118  $key = wfRandomString();
120  $cKey1 = wfRandomString();
121  $cKey2 = wfRandomString();
122 
123  $wasSet = 0;
124  $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
125  ++$wasSet;
126  $ttl = 20; // override with another value
127  return $value;
128  };
129 
130  $wasSet = 0;
131  $v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts );
132  $this->assertEquals( $value, $v, "Value returned" );
133  $this->assertEquals( 1, $wasSet, "Value regenerated" );
134 
135  $curTTL = null;
136  $cache->get( $key, $curTTL );
137  $this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overriden)' );
138  $this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overriden)' );
139 
140  $wasSet = 0;
141  $v = $cache->getWithSetCallback( $key, 30, $func, [
142  'lowTTL' => 0,
143  'lockTSE' => 5,
144  ] + $extOpts );
145  $this->assertEquals( $value, $v, "Value returned" );
146  $this->assertEquals( 0, $wasSet, "Value not regenerated" );
147 
148  $priorTime = microtime( true );
149  usleep( 1 );
150  $wasSet = 0;
152  $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
153  );
154  $this->assertEquals( $value, $v, "Value returned" );
155  $this->assertEquals( 1, $wasSet, "Value regenerated due to check keys" );
156  $t1 = $cache->getCheckKeyTime( $cKey1 );
157  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' );
158  $t2 = $cache->getCheckKeyTime( $cKey2 );
159  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' );
160 
161  $priorTime = microtime( true );
162  $wasSet = 0;
164  $key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
165  );
166  $this->assertEquals( $value, $v, "Value returned" );
167  $this->assertEquals( 1, $wasSet, "Value regenerated due to still-recent check keys" );
168  $t1 = $cache->getCheckKeyTime( $cKey1 );
169  $this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' );
170  $t2 = $cache->getCheckKeyTime( $cKey2 );
171  $this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' );
172 
173  $curTTL = null;
174  $v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] );
175  if ( $versioned ) {
176  $this->assertEquals( $value, $v[$cache::VFLD_DATA], "Value returned" );
177  } else {
178  $this->assertEquals( $value, $v, "Value returned" );
179  }
180  $this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" );
181 
182  $wasSet = 0;
183  $key = wfRandomString();
184  $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
185  $this->assertEquals( $value, $v, "Value returned" );
186  $cache->delete( $key );
187  $v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts );
188  $this->assertEquals( $value, $v, "Value still returned after deleted" );
189  $this->assertEquals( 1, $wasSet, "Value process cached while deleted" );
190  }
191 
192  public static function getWithSetCallback_provider() {
193  return [
194  [ [], false ],
195  [ [ 'version' => 1 ], true ]
196  ];
197  }
198 
203  public function testLockTSE() {
205  $key = wfRandomString();
207 
208  $calls = 0;
209  $func = function() use ( &$calls, $value ) {
210  ++$calls;
211  return $value;
212  };
213 
214  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
215  $this->assertEquals( $value, $ret );
216  $this->assertEquals( 1, $calls, 'Value was populated' );
217 
218  // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
219  $this->internalCache->lock( $key, 0 );
220 
221  $checkKeys = [ wfRandomString() ]; // new check keys => force misses
222  $ret = $cache->getWithSetCallback( $key, 30, $func,
223  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
224  $this->assertEquals( $value, $ret, 'Old value used' );
225  $this->assertEquals( 1, $calls, 'Callback was not used' );
226 
227  $cache->delete( $key );
228  $ret = $cache->getWithSetCallback( $key, 30, $func,
229  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
230  $this->assertEquals( $value, $ret, 'Callback was used; interim saved' );
231  $this->assertEquals( 2, $calls, 'Callback was used; interim saved' );
232 
233  $ret = $cache->getWithSetCallback( $key, 30, $func,
234  [ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] );
235  $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
236  $this->assertEquals( 2, $calls, 'Callback was not used; used interim' );
237  }
238 
243  public function testLockTSESlow() {
245  $key = wfRandomString();
247 
248  $calls = 0;
249  $func = function( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) {
250  ++$calls;
251  $setOpts['since'] = microtime( true ) - 10;
252  return $value;
253  };
254 
255  // Value should be marked as stale due to snapshot lag
256  $curTTL = null;
257  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
258  $this->assertEquals( $value, $ret );
259  $this->assertEquals( $value, $cache->get( $key, $curTTL ), 'Value was populated' );
260  $this->assertLessThan( 0, $curTTL, 'Value has negative curTTL' );
261  $this->assertEquals( 1, $calls, 'Value was generated' );
262 
263  // Acquire a lock to verify that getWithSetCallback uses lockTSE properly
264  $this->internalCache->lock( $key, 0 );
265  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] );
266  $this->assertEquals( $value, $ret );
267  $this->assertEquals( 1, $calls, 'Callback was not used' );
268  }
269 
274  public function testBusyValue() {
276  $key = wfRandomString();
278  $busyValue = wfRandomString();
279 
280  $calls = 0;
281  $func = function() use ( &$calls, $value ) {
282  ++$calls;
283  return $value;
284  };
285 
286  $ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] );
287  $this->assertEquals( $value, $ret );
288  $this->assertEquals( 1, $calls, 'Value was populated' );
289 
290  // Acquire a lock to verify that getWithSetCallback uses busyValue properly
291  $this->internalCache->lock( $key, 0 );
292 
293  $checkKeys = [ wfRandomString() ]; // new check keys => force misses
294  $ret = $cache->getWithSetCallback( $key, 30, $func,
295  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
296  $this->assertEquals( $value, $ret, 'Callback used' );
297  $this->assertEquals( 2, $calls, 'Callback used' );
298 
299  $ret = $cache->getWithSetCallback( $key, 30, $func,
300  [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
301  $this->assertEquals( $value, $ret, 'Old value used' );
302  $this->assertEquals( 2, $calls, 'Callback was not used' );
303 
304  $cache->delete( $key ); // no value at all anymore and still locked
305  $ret = $cache->getWithSetCallback( $key, 30, $func,
306  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
307  $this->assertEquals( $busyValue, $ret, 'Callback was not used; used busy value' );
308  $this->assertEquals( 2, $calls, 'Callback was not used; used busy value' );
309 
310  $this->internalCache->unlock( $key );
311  $ret = $cache->getWithSetCallback( $key, 30, $func,
312  [ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
313  $this->assertEquals( $value, $ret, 'Callback was used; saved interim' );
314  $this->assertEquals( 3, $calls, 'Callback was used; saved interim' );
315 
316  $this->internalCache->lock( $key, 0 );
317  $ret = $cache->getWithSetCallback( $key, 30, $func,
318  [ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] );
319  $this->assertEquals( $value, $ret, 'Callback was not used; used interim' );
320  $this->assertEquals( 3, $calls, 'Callback was not used; used interim' );
321  }
322 
326  public function testGetMulti() {
328 
329  $value1 = [ 'this' => 'is', 'a' => 'test' ];
330  $value2 = [ 'this' => 'is', 'another' => 'test' ];
331 
332  $key1 = wfRandomString();
333  $key2 = wfRandomString();
334  $key3 = wfRandomString();
335 
336  $cache->set( $key1, $value1, 5 );
337  $cache->set( $key2, $value2, 10 );
338 
339  $curTTLs = [];
340  $this->assertEquals(
341  [ $key1 => $value1, $key2 => $value2 ],
342  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ),
343  'Result array populated'
344  );
345 
346  $this->assertEquals( 2, count( $curTTLs ), "Two current TTLs in array" );
347  $this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" );
348  $this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" );
349 
350  $cKey1 = wfRandomString();
351  $cKey2 = wfRandomString();
352 
353  $priorTime = microtime( true );
354  usleep( 1 );
355  $curTTLs = [];
356  $this->assertEquals(
357  [ $key1 => $value1, $key2 => $value2 ],
358  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
359  "Result array populated even with new check keys"
360  );
361  $t1 = $cache->getCheckKeyTime( $cKey1 );
362  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key 1 generated on miss' );
363  $t2 = $cache->getCheckKeyTime( $cKey2 );
364  $this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check key 2 generated on miss' );
365  $this->assertEquals( 2, count( $curTTLs ), "Current TTLs array set" );
366  $this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' );
367  $this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' );
368 
369  usleep( 1 );
370  $curTTLs = [];
371  $this->assertEquals(
372  [ $key1 => $value1, $key2 => $value2 ],
373  $cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
374  "Result array still populated even with new check keys"
375  );
376  $this->assertEquals( 2, count( $curTTLs ), "Current TTLs still array set" );
377  $this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' );
378  $this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' );
379  }
380 
385  public function testGetMultiCheckKeys() {
387 
388  $checkAll = wfRandomString();
389  $check1 = wfRandomString();
390  $check2 = wfRandomString();
391  $check3 = wfRandomString();
392  $value1 = wfRandomString();
393  $value2 = wfRandomString();
394 
395  // Fake initial check key to be set in the past. Otherwise we'd have to sleep for
396  // several seconds during the test to assert the behaviour.
397  foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) {
399  }
400  usleep( 100 );
401 
402  $cache->set( 'key1', $value1, 10 );
403  $cache->set( 'key2', $value2, 10 );
404 
405  $curTTLs = [];
406  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
407  'key1' => $check1,
408  $checkAll,
409  'key2' => $check2,
410  'key3' => $check3,
411  ] );
412  $this->assertEquals(
413  [ 'key1' => $value1, 'key2' => $value2 ],
414  $result,
415  'Initial values'
416  );
417  $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' );
418  $this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' );
419  $this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' );
420  $this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' );
421 
422  $cache->touchCheckKey( $check1 );
423 
424  $curTTLs = [];
425  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
426  'key1' => $check1,
427  $checkAll,
428  'key2' => $check2,
429  'key3' => $check3,
430  ] );
431  $this->assertEquals(
432  [ 'key1' => $value1, 'key2' => $value2 ],
433  $result,
434  'key1 expired by check1, but value still provided'
435  );
436  $this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' );
437  $this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' );
438 
439  $cache->touchCheckKey( $checkAll );
440 
441  $curTTLs = [];
442  $result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [
443  'key1' => $check1,
444  $checkAll,
445  'key2' => $check2,
446  'key3' => $check3,
447  ] );
448  $this->assertEquals(
449  [ 'key1' => $value1, 'key2' => $value2 ],
450  $result,
451  'All keys expired by checkAll, but value still provided'
452  );
453  $this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' );
454  $this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' );
455  }
456 
461  public function testCheckKeyInitHoldoff() {
463 
464  for ( $i = 0; $i < 500; ++$i ) {
465  $key = wfRandomString();
466  $checkKey = wfRandomString();
467  // miss, set, hit
468  $cache->get( $key, $curTTL, [ $checkKey ] );
469  $cache->set( $key, 'val', 10 );
470  $curTTL = null;
471  $v = $cache->get( $key, $curTTL, [ $checkKey ] );
472 
473  $this->assertEquals( 'val', $v );
474  $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (miss/set/hit)" );
475  }
476 
477  for ( $i = 0; $i < 500; ++$i ) {
478  $key = wfRandomString();
479  $checkKey = wfRandomString();
480  // set, hit
481  $cache->set( $key, 'val', 10 );
482  $curTTL = null;
483  $v = $cache->get( $key, $curTTL, [ $checkKey ] );
484 
485  $this->assertEquals( 'val', $v );
486  $this->assertLessThan( 0, $curTTL, "Step $i: CTL < 0 (set/hit)" );
487  }
488  }
489 
493  public function testDelete() {
494  $key = wfRandomString();
496  $this->cache->set( $key, $value );
497 
498  $curTTL = null;
499  $v = $this->cache->get( $key, $curTTL );
500  $this->assertEquals( $value, $v, "Key was created with value" );
501  $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
502 
503  $this->cache->delete( $key );
504 
505  $curTTL = null;
506  $v = $this->cache->get( $key, $curTTL );
507  $this->assertFalse( $v, "Deleted key has false value" );
508  $this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" );
509 
510  $this->cache->set( $key, $value . 'more' );
511  $v = $this->cache->get( $key, $curTTL );
512  $this->assertFalse( $v, "Deleted key is tombstoned and has false value" );
513  $this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" );
514 
515  $this->cache->set( $key, $value );
516  $this->cache->delete( $key, WANObjectCache::HOLDOFF_NONE );
517 
518  $curTTL = null;
519  $v = $this->cache->get( $key, $curTTL );
520  $this->assertFalse( $v, "Deleted key has false value" );
521  $this->assertNull( $curTTL, "Deleted key has null current TTL" );
522 
523  $this->cache->set( $key, $value );
524  $v = $this->cache->get( $key, $curTTL );
525  $this->assertEquals( $value, $v, "Key was created with value" );
526  $this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" );
527  }
528 
534  public function testGetWithSetCallback_versions( array $extOpts, $versioned ) {
536 
537  $key = wfRandomString();
539 
540  $wasSet = 0;
541  $func = function( $old, &$ttl ) use ( &$wasSet, $value ) {
542  ++$wasSet;
543  return $value;
544  };
545 
546  // Set the main key (version N if versioned)
547  $wasSet = 0;
548  $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
549  $this->assertEquals( $value, $v, "Value returned" );
550  $this->assertEquals( 1, $wasSet, "Value regenerated" );
551  $cache->getWithSetCallback( $key, 30, $func, $extOpts );
552  $this->assertEquals( 1, $wasSet, "Value not regenerated" );
553  // Set the key for version N+1 (if versioned)
554  if ( $versioned ) {
555  $verOpts = [ 'version' => $extOpts['version'] + 1 ];
556 
557  $wasSet = 0;
558  $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
559  $this->assertEquals( $value, $v, "Value returned" );
560  $this->assertEquals( 1, $wasSet, "Value regenerated" );
561 
562  $wasSet = 0;
563  $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
564  $this->assertEquals( $value, $v, "Value returned" );
565  $this->assertEquals( 0, $wasSet, "Value not regenerated" );
566  }
567 
568  $wasSet = 0;
569  $cache->getWithSetCallback( $key, 30, $func, $extOpts );
570  $this->assertEquals( 0, $wasSet, "Value not regenerated" );
571 
572  $wasSet = 0;
573  $cache->delete( $key );
574  $v = $cache->getWithSetCallback( $key, 30, $func, $extOpts );
575  $this->assertEquals( $value, $v, "Value returned" );
576  $this->assertEquals( 1, $wasSet, "Value regenerated" );
577 
578  if ( $versioned ) {
579  $wasSet = 0;
580  $verOpts = [ 'version' => $extOpts['version'] + 1 ];
581  $v = $cache->getWithSetCallback( $key, 30, $func, $verOpts + $extOpts );
582  $this->assertEquals( $value, $v, "Value returned" );
583  $this->assertEquals( 1, $wasSet, "Value regenerated" );
584  }
585  }
586 
587  public static function getWithSetCallback_versions_provider() {
588  return [
589  [ [], false ],
590  [ [ 'version' => 1 ], true ]
591  ];
592  }
593 
599  public function testTouchKeys() {
600  $key = wfRandomString();
601 
602  $priorTime = microtime( true );
603  usleep( 100 );
604  $t0 = $this->cache->getCheckKeyTime( $key );
605  $this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' );
606 
607  $priorTime = microtime( true );
608  usleep( 100 );
609  $this->cache->touchCheckKey( $key );
610  $t1 = $this->cache->getCheckKeyTime( $key );
611  $this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' );
612 
613  $t2 = $this->cache->getCheckKeyTime( $key );
614  $this->assertEquals( $t1, $t2, 'Check key time did not change' );
615 
616  usleep( 100 );
617  $this->cache->touchCheckKey( $key );
618  $t3 = $this->cache->getCheckKeyTime( $key );
619  $this->assertGreaterThan( $t2, $t3, 'Check key time increased' );
620 
621  $t4 = $this->cache->getCheckKeyTime( $key );
622  $this->assertEquals( $t3, $t4, 'Check key time did not change' );
623 
624  usleep( 100 );
625  $this->cache->resetCheckKey( $key );
626  $t5 = $this->cache->getCheckKeyTime( $key );
627  $this->assertGreaterThan( $t4, $t5, 'Check key time increased' );
628 
629  $t6 = $this->cache->getCheckKeyTime( $key );
630  $this->assertEquals( $t5, $t6, 'Check key time did not change' );
631  }
632 
636  public function testGetWithSeveralCheckKeys() {
637  $key = wfRandomString();
638  $tKey1 = wfRandomString();
639  $tKey2 = wfRandomString();
640  $value = 'meow';
641 
642  // Two check keys are newer (given hold-off) than $key, another is older
643  $this->internalCache->set(
645  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 3 )
646  );
647  $this->internalCache->set(
649  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 5 )
650  );
651  $this->internalCache->set(
653  WANObjectCache::PURGE_VAL_PREFIX . ( microtime( true ) - 30 )
654  );
655  $this->cache->set( $key, $value, 30 );
656 
657  $curTTL = null;
658  $v = $this->cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
659  $this->assertEquals( $value, $v, "Value matches" );
660  $this->assertLessThan( -4.9, $curTTL, "Correct CTL" );
661  $this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" );
662  }
663 
667  public function testSetWithLag() {
668  $value = 1;
669 
670  $key = wfRandomString();
671  $opts = [ 'lag' => 300, 'since' => microtime( true ) ];
672  $this->cache->set( $key, $value, 30, $opts );
673  $this->assertEquals( $value, $this->cache->get( $key ), "Rep-lagged value written." );
674 
675  $key = wfRandomString();
676  $opts = [ 'lag' => 0, 'since' => microtime( true ) - 300 ];
677  $this->cache->set( $key, $value, 30, $opts );
678  $this->assertEquals( false, $this->cache->get( $key ), "Trx-lagged value not written." );
679 
680  $key = wfRandomString();
681  $opts = [ 'lag' => 5, 'since' => microtime( true ) - 5 ];
682  $this->cache->set( $key, $value, 30, $opts );
683  $this->assertEquals( false, $this->cache->get( $key ), "Lagged value not written." );
684  }
685 
689  public function testWritePending() {
690  $value = 1;
691 
692  $key = wfRandomString();
693  $opts = [ 'pending' => true ];
694  $this->cache->set( $key, $value, 30, $opts );
695  $this->assertEquals( false, $this->cache->get( $key ), "Pending value not written." );
696  }
697 }
set($key, $value, $ttl=0, array $opts=[])
Set the value of a key in cache.
the array() calling protocol came about after MediaWiki 1.4rc1.
testGetWithSetCallback_versions(array $extOpts, $versioned)
getWithSetCallback_versions_provider
static getWithSetCallback_provider()
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:189
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
testCheckKeyInitHoldoff()
WANObjectCache::get() WANObjectCache::processCheckKeys()
testGetMultiCheckKeys()
WANObjectCache::getMulti() WANObjectCache::processCheckKeys()
testSetAndGet($value, $ttl)
provideSetAndGet WANObjectCache::set() WANObjectCache::get()
touchCheckKey($key, $holdoff=self::HOLDOFF_TTL)
Purge a "check" key from all datacenters, invalidating keys that use it.
$value
get($key, &$curTTL=null, array $checkKeys=[], &$asOf=null)
Fetch the value of a key from cache.
testTouchKeys()
WANObjectCache::touchCheckKey() WANObjectCache::resetCheckKey() WANObjectCache::getCheckKeyTime() ...
Multi-datacenter aware caching interface.
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
wfRandomString($length=32)
Get a random string containing a number of pseudo-random hex characters.
No-op class for publishing messages into a PubSub system.
getWithSetCallback($key, $ttl, $callback, array $opts=[])
Method to fetch/regenerate cache keys.
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 '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:Associative array mapping language codes to prefixed links of the form"language:title".&$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:1814
you have access to all of the normal MediaWiki so you can get a DB use the cache
Definition: maintenance.txt:52
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:1816
testLockTSE()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
testStaleSet()
WANObjectCache::set()
static getWithSetCallback_versions_provider()
testLockTSESlow()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
WANObjectCache $cache
getCheckKeyTime($key)
Fetch the value of a timestamp "check" key.
testSetOver()
WANObjectCache::set()
testGetMulti()
WANObjectCache::getMulti()
testGetWithSetCallback(array $extOpts, $versioned)
getWithSetCallback_provider WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback...
testSetWithLag()
WANObjectCache::set()
const HOLDOFF_NONE
Idiom for delete() for "no hold-off".
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:1816
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
testGetWithSeveralCheckKeys()
WANObjectCache::getMulti()
Simple store for keeping values in an associative array for the current process.
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
delete($key, $ttl=self::HOLDOFF_TTL)
Purge a key from all datacenters.
static getWANInstance($id)
Get a cached instance of the specified type of WAN cache object.
testWritePending()
WANObjectCache::set()
getMulti(array $keys, &$curTTLs=[], array $checkKeys=[], array &$asOfs=[])
Fetch the value of several keys from cache.
testBusyValue()
WANObjectCache::getWithSetCallback() WANObjectCache::doGetWithSetCallback()
testGetNotExists()
WANObjectCache::get()
static newFromObject($object)
Return the same object, without access restrictions.
testDelete()
WANObjectCache::delete()
Allows to change the fields on the form that will be generated $name
Definition: hooks.txt:310