3 use Wikimedia\TestingAccessWrapper;
16 'pool' =>
'testcache-hash',
20 $wanCache = TestingAccessWrapper::newFromObject( $this->
cache );
22 $this->internalCache = $wanCache->cache;
38 $this->
cache->get( $key, $curTTL, [], $asOf );
39 $this->assertNull( $curTTL,
"Current TTL is null" );
40 $this->assertNull( $asOf,
"Current as-of-time is infinite" );
42 $t = microtime(
true );
45 $this->assertEquals(
$value, $this->
cache->get( $key, $curTTL, [], $asOf ) );
46 if ( is_infinite( $ttl ) || $ttl == 0 ) {
47 $this->assertTrue( is_infinite( $curTTL ),
"Current TTL is infinite" );
49 $this->assertGreaterThan( 0, $curTTL,
"Current TTL > 0" );
50 $this->assertLessThanOrEqual( $ttl, $curTTL,
"Current TTL < nominal TTL" );
52 $this->assertGreaterThanOrEqual(
$t - 1, $asOf,
"As-of-time in range of set() time" );
53 $this->assertLessThanOrEqual(
$t + 1, $asOf,
"As-of-time in range of set() time" );
79 $this->assertFalse(
$value,
"Non-existing key has false value" );
80 $this->assertNull( $curTTL,
"Non-existing key has null current TTL" );
88 for ( $i = 0; $i < 3; ++$i ) {
92 $this->assertEquals( $this->
cache->get( $key ),
$value );
102 $this->
cache->set( $key,
$value, 3, [
'since' => microtime(
true ) - 30 ] );
104 $this->assertFalse( $this->
cache->get( $key ),
"Stale set() value ignored" );
109 $callback =
function ()
use ( &$hit ) {
114 $groups = [
'thiscache:1',
'thatcache:1',
'somecache:1' ];
116 foreach (
$keys as $i => $key ) {
117 $this->
cache->getWithSetCallback(
118 $key, 100, $callback, [
'pcTTL' => 5,
'pcGroup' => $groups[$i] ] );
120 $this->assertEquals( 3, $hit );
122 foreach (
$keys as $i => $key ) {
123 $this->
cache->getWithSetCallback(
124 $key, 100, $callback, [
'pcTTL' => 5,
'pcGroup' => $groups[$i] ] );
126 $this->assertEquals( 3, $hit,
"Values cached" );
128 foreach (
$keys as $i => $key ) {
129 $this->
cache->getWithSetCallback(
130 "$key-2", 100, $callback, [
'pcTTL' => 5,
'pcGroup' => $groups[$i] ] );
132 $this->assertEquals( 6, $hit );
134 foreach (
$keys as $i => $key ) {
135 $this->
cache->getWithSetCallback(
136 "$key-2", 100, $callback, [
'pcTTL' => 5,
'pcGroup' => $groups[$i] ] );
138 $this->assertEquals( 6, $hit,
"New values cached" );
140 foreach (
$keys as $i => $key ) {
141 $this->
cache->delete( $key );
142 $this->
cache->getWithSetCallback(
143 $key, 100, $callback, [
'pcTTL' => 5,
'pcGroup' => $groups[$i] ] );
145 $this->assertEquals( 9, $hit,
"Values evicted" );
147 $key = reset(
$keys );
149 $this->
cache->getWithSetCallback( $key, 100, $callback, [
'pcTTL' => 5 ] );
150 $this->
cache->getWithSetCallback( $key, 100, $callback, [
'pcTTL' => 5 ] );
151 $this->assertEquals( 10, $hit,
"Value cached" );
152 $outerCallback =
function ()
use ( &$callback, $key ) {
153 $v = $this->
cache->getWithSetCallback( $key, 100, $callback, [
'pcTTL' => 5 ] );
157 $this->
cache->getWithSetCallback( $key, 100, $outerCallback );
158 $this->assertEquals( 11, $hit,
"Nested callback value process cache skipped" );
179 $func =
function ( $old, &$ttl, &$opts, $asOf )
180 use ( &$wasSet, &$priorValue, &$priorAsOf,
$value )
191 $this->assertEquals(
$value, $v,
"Value returned" );
192 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
193 $this->assertFalse( $priorValue,
"No prior value" );
194 $this->assertNull( $priorAsOf,
"No prior value" );
198 $this->assertLessThanOrEqual( 20, $curTTL,
'Current TTL between 19-20 (overriden)' );
199 $this->assertGreaterThanOrEqual( 19, $curTTL,
'Current TTL between 19-20 (overriden)' );
206 $this->assertEquals(
$value, $v,
"Value returned" );
207 $this->assertEquals( 0, $wasSet,
"Value not regenerated" );
209 $priorTime = microtime(
true );
213 $key, 30, $func, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
215 $this->assertEquals(
$value, $v,
"Value returned" );
216 $this->assertEquals( 1, $wasSet,
"Value regenerated due to check keys" );
217 $this->assertEquals(
$value, $priorValue,
"Has prior value" );
218 $this->assertInternalType(
'float', $priorAsOf,
"Has prior value" );
220 $this->assertGreaterThanOrEqual( $priorTime, $t1,
'Check keys generated on miss' );
222 $this->assertGreaterThanOrEqual( $priorTime, $t2,
'Check keys generated on miss' );
224 $priorTime = microtime(
true );
227 $key, 30, $func, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
229 $this->assertEquals(
$value, $v,
"Value returned" );
230 $this->assertEquals( 1, $wasSet,
"Value regenerated due to still-recent check keys" );
232 $this->assertLessThanOrEqual( $priorTime, $t1,
'Check keys did not change again' );
234 $this->assertLessThanOrEqual( $priorTime, $t2,
'Check keys did not change again' );
237 $v =
$cache->
get( $key, $curTTL, [ $cKey1, $cKey2 ] );
239 $this->assertEquals(
$value, $v[$cache::VFLD_DATA],
"Value returned" );
241 $this->assertEquals(
$value, $v,
"Value returned" );
243 $this->assertLessThanOrEqual( 0, $curTTL,
"Value has current TTL < 0 due to check keys" );
248 $this->assertEquals(
$value, $v,
"Value returned" );
251 $this->assertEquals(
$value, $v,
"Value still returned after deleted" );
252 $this->assertEquals( 1, $wasSet,
"Value process cached while deleted" );
258 [ [
'version' => 1 ],
true ]
281 $genFunc =
function ( $id, $old, &$ttl, &$opts, $asOf )
use (
282 &$wasSet, &$priorValue, &$priorAsOf
292 $keyedIds =
new ArrayIterator( [ $keyA => 3353 ] );
295 $keyedIds, 30, $genFunc, [
'lockTSE' => 5 ] + $extOpts );
296 $this->assertEquals(
$value, $v[$keyA],
"Value returned" );
297 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
298 $this->assertFalse( $priorValue,
"No prior value" );
299 $this->assertNull( $priorAsOf,
"No prior value" );
303 $this->assertLessThanOrEqual( 20, $curTTL,
'Current TTL between 19-20 (overriden)' );
304 $this->assertGreaterThanOrEqual( 19, $curTTL,
'Current TTL between 19-20 (overriden)' );
308 $keyedIds =
new ArrayIterator( [ $keyB =>
'efef' ] );
310 $keyedIds, 30, $genFunc, [
'lowTTL' => 0,
'lockTSE' => 5, ] + $extOpts );
311 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
312 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
315 $keyedIds, 30, $genFunc, [
'lowTTL' => 0,
'lockTSE' => 5, ] + $extOpts );
316 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
317 $this->assertEquals( 1, $wasSet,
"Value not regenerated" );
320 $priorTime = microtime(
true );
323 $keyedIds =
new ArrayIterator( [ $keyB =>
'efef' ] );
325 $keyedIds, 30, $genFunc, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
327 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
328 $this->assertEquals( 1, $wasSet,
"Value regenerated due to check keys" );
329 $this->assertEquals(
$value, $priorValue,
"Has prior value" );
330 $this->assertInternalType(
'float', $priorAsOf,
"Has prior value" );
332 $this->assertGreaterThanOrEqual( $priorTime, $t1,
'Check keys generated on miss' );
334 $this->assertGreaterThanOrEqual( $priorTime, $t2,
'Check keys generated on miss' );
336 $priorTime = microtime(
true );
339 $keyedIds =
new ArrayIterator( [ $keyC => 43636 ] );
341 $keyedIds, 30, $genFunc, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
343 $this->assertEquals(
$value, $v[$keyC],
"Value returned" );
344 $this->assertEquals( 1, $wasSet,
"Value regenerated due to still-recent check keys" );
346 $this->assertLessThanOrEqual( $priorTime, $t1,
'Check keys did not change again' );
348 $this->assertLessThanOrEqual( $priorTime, $t2,
'Check keys did not change again' );
351 $v =
$cache->
get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
353 $this->assertEquals(
$value, $v[$cache::VFLD_DATA],
"Value returned" );
355 $this->assertEquals(
$value, $v,
"Value returned" );
357 $this->assertLessThanOrEqual( 0, $curTTL,
"Value has current TTL < 0 due to check keys" );
361 $keyedIds =
new ArrayIterator( [ $key => 242424 ] );
363 $keyedIds, 30, $genFunc, [
'pcTTL' => 5 ] + $extOpts );
364 $this->assertEquals(
"@{$keyedIds[$key]}$", $v[$key],
"Value returned" );
366 $keyedIds =
new ArrayIterator( [ $key => 242424 ] );
368 $keyedIds, 30, $genFunc, [
'pcTTL' => 5 ] + $extOpts );
369 $this->assertEquals(
"@{$keyedIds[$key]}$", $v[$key],
"Value still returned after deleted" );
370 $this->assertEquals( 1, $wasSet,
"Value process cached while deleted" );
373 $ids = [ 1, 2, 3, 4, 5, 6 ];
375 return $wanCache->makeKey(
'test', $id );
378 $genFunc =
function ( $id, $oldValue, &$ttl,
array &$setops )
use ( &$calls ) {
386 [
"val-1",
"val-2",
"val-3",
"val-4",
"val-5",
"val-6" ],
387 array_values( $values ),
388 "Correct values in correct order"
391 array_map( $keyFunc, $ids, array_fill( 0,
count( $ids ), $this->
cache ) ),
392 array_keys( $values ),
393 "Correct keys in correct order"
395 $this->assertEquals(
count( $ids ), $calls );
398 $this->assertEquals(
count( $ids ), $calls,
"Values cached" );
401 $localBag = $this->getMockBuilder(
'HashBagOStuff' )
402 ->setMethods( [
'getMulti' ] )->getMock();
403 $localBag->expects( $this->exactly( 1 ) )->method(
'getMulti' )->willReturn( [
407 $wanCache =
new WANObjectCache( [
'cache' => $localBag,
'pool' =>
'testcache-hash' ] );
410 $keyedIds =
new ArrayIterator( [
'k1' =>
'id1',
'k2' =>
'id2' ] );
412 [
'k1' =>
'val-id1',
'k2' =>
'val-id2' ],
413 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [
'pcTTL' => 5 ] )
417 [
'k1' =>
'val-id1',
'k2' =>
'val-id2' ],
418 $wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [
'pcTTL' => 5 ] )
425 [ [
'version' => 1 ],
true ]
447 &$wasSet, &$priorValue, &$priorAsOf
450 foreach ( $ids
as $id ) {
452 $newValues[$id] =
"@$id$";
460 $keyedIds =
new ArrayIterator( [ $keyA => 3353 ] );
463 $keyedIds, 30, $genFunc, $extOpts );
464 $this->assertEquals(
$value, $v[$keyA],
"Value returned" );
465 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
469 $this->assertLessThanOrEqual( 20, $curTTL,
'Current TTL between 19-20 (overriden)' );
470 $this->assertGreaterThanOrEqual( 19, $curTTL,
'Current TTL between 19-20 (overriden)' );
474 $keyedIds =
new ArrayIterator( [ $keyB =>
'efef' ] );
476 $keyedIds, 30, $genFunc, [
'lowTTL' => 0 ] + $extOpts );
477 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
478 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
481 $keyedIds, 30, $genFunc, [
'lowTTL' => 0 ] + $extOpts );
482 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
483 $this->assertEquals( 1, $wasSet,
"Value not regenerated" );
486 $priorTime = microtime(
true );
489 $keyedIds =
new ArrayIterator( [ $keyB =>
'efef' ] );
491 $keyedIds, 30, $genFunc, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
493 $this->assertEquals(
$value, $v[$keyB],
"Value returned" );
494 $this->assertEquals( 1, $wasSet,
"Value regenerated due to check keys" );
496 $this->assertGreaterThanOrEqual( $priorTime, $t1,
'Check keys generated on miss' );
498 $this->assertGreaterThanOrEqual( $priorTime, $t2,
'Check keys generated on miss' );
500 $priorTime = microtime(
true );
503 $keyedIds =
new ArrayIterator( [ $keyC => 43636 ] );
505 $keyedIds, 30, $genFunc, [
'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts
507 $this->assertEquals(
$value, $v[$keyC],
"Value returned" );
508 $this->assertEquals( 1, $wasSet,
"Value regenerated due to still-recent check keys" );
510 $this->assertLessThanOrEqual( $priorTime, $t1,
'Check keys did not change again' );
512 $this->assertLessThanOrEqual( $priorTime, $t2,
'Check keys did not change again' );
515 $v =
$cache->
get( $keyC, $curTTL, [ $cKey1, $cKey2 ] );
517 $this->assertEquals(
$value, $v[$cache::VFLD_DATA],
"Value returned" );
519 $this->assertEquals(
$value, $v,
"Value returned" );
521 $this->assertLessThanOrEqual( 0, $curTTL,
"Value has current TTL < 0 due to check keys" );
525 $keyedIds =
new ArrayIterator( [ $key => 242424 ] );
527 $keyedIds, 30, $genFunc, [
'pcTTL' => 5 ] + $extOpts );
528 $this->assertEquals(
"@{$keyedIds[$key]}$", $v[$key],
"Value returned" );
530 $keyedIds =
new ArrayIterator( [ $key => 242424 ] );
532 $keyedIds, 30, $genFunc, [
'pcTTL' => 5 ] + $extOpts );
533 $this->assertEquals(
"@{$keyedIds[$key]}$", $v[$key],
"Value still returned after deleted" );
534 $this->assertEquals( 1, $wasSet,
"Value process cached while deleted" );
537 $ids = [ 1, 2, 3, 4, 5, 6 ];
539 return $wanCache->makeKey(
'test', $id );
544 foreach ( $ids
as $id ) {
546 $newValues[$id] =
"val-{$id}";
554 [
"val-1",
"val-2",
"val-3",
"val-4",
"val-5",
"val-6" ],
555 array_values( $values ),
556 "Correct values in correct order"
559 array_map( $keyFunc, $ids, array_fill( 0,
count( $ids ), $this->
cache ) ),
560 array_keys( $values ),
561 "Correct keys in correct order"
563 $this->assertEquals(
count( $ids ), $calls );
566 $this->assertEquals(
count( $ids ), $calls,
"Values cached" );
572 [ [
'version' => 1 ],
true ]
595 $this->assertEquals( 1, $calls,
'Value was populated' );
598 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
602 [
'lockTSE' => 5,
'checkKeys' => $checkKeys ] );
603 $this->assertEquals(
$value,
$ret,
'Old value used' );
604 $this->assertEquals( 1, $calls,
'Callback was not used' );
608 [
'lockTSE' => 5,
'checkKeys' => $checkKeys ] );
609 $this->assertEquals(
$value,
$ret,
'Callback was used; interim saved' );
610 $this->assertEquals( 2, $calls,
'Callback was used; interim saved' );
613 [
'lockTSE' => 5,
'checkKeys' => $checkKeys ] );
614 $this->assertEquals(
$value,
$ret,
'Callback was not used; used interim' );
615 $this->assertEquals( 2, $calls,
'Callback was not used; used interim' );
628 $func =
function ( $oldValue, &$ttl, &$setOpts )
use ( &$calls,
$value,
$cache, $key ) {
630 $setOpts[
'since'] = microtime(
true ) - 10;
640 $this->assertEquals(
$value,
$cache->
get( $key, $curTTL ),
'Value was populated' );
641 $this->assertLessThan( 0, $curTTL,
'Value has negative curTTL' );
642 $this->assertEquals( 1, $calls,
'Value was generated' );
645 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
648 $this->assertEquals( 1, $calls,
'Callback was not used' );
671 $this->assertEquals( 1, $calls,
'Value was populated' );
674 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
678 [
'busyValue' => $busyValue,
'checkKeys' => $checkKeys ] );
679 $this->assertEquals(
$value,
$ret,
'Callback used' );
680 $this->assertEquals( 2, $calls,
'Callback used' );
683 [
'lockTSE' => 30,
'busyValue' => $busyValue,
'checkKeys' => $checkKeys ] );
684 $this->assertEquals(
$value,
$ret,
'Old value used' );
685 $this->assertEquals( 2, $calls,
'Callback was not used' );
689 [
'busyValue' => $busyValue,
'checkKeys' => $checkKeys ] );
690 $this->assertEquals( $busyValue,
$ret,
'Callback was not used; used busy value' );
691 $this->assertEquals( 2, $calls,
'Callback was not used; used busy value' );
693 $this->internalCache->delete( $cache::MUTEX_KEY_PREFIX . $key );
695 [
'lockTSE' => 30,
'busyValue' => $busyValue,
'checkKeys' => $checkKeys ] );
696 $this->assertEquals(
$value,
$ret,
'Callback was used; saved interim' );
697 $this->assertEquals( 3, $calls,
'Callback was used; saved interim' );
699 $this->internalCache->add( $cache::MUTEX_KEY_PREFIX . $key, 1, 0 );
701 [
'busyValue' => $busyValue,
'checkKeys' => $checkKeys ] );
702 $this->assertEquals(
$value,
$ret,
'Callback was not used; used interim' );
703 $this->assertEquals( 3, $calls,
'Callback was not used; used interim' );
712 $value1 = [
'this' =>
'is',
'a' =>
'test' ];
713 $value2 = [
'this' =>
'is',
'another' =>
'test' ];
724 [ $key1 => $value1, $key2 => $value2 ],
726 'Result array populated'
729 $this->assertEquals( 2,
count( $curTTLs ),
"Two current TTLs in array" );
730 $this->assertGreaterThan( 0, $curTTLs[$key1],
"Key 1 has current TTL > 0" );
731 $this->assertGreaterThan( 0, $curTTLs[$key2],
"Key 2 has current TTL > 0" );
736 $priorTime = microtime(
true );
740 [ $key1 => $value1, $key2 => $value2 ],
741 $cache->
getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
742 "Result array populated even with new check keys"
745 $this->assertGreaterThanOrEqual( $priorTime, $t1,
'Check key 1 generated on miss' );
747 $this->assertGreaterThanOrEqual( $priorTime, $t2,
'Check key 2 generated on miss' );
748 $this->assertEquals( 2,
count( $curTTLs ),
"Current TTLs array set" );
749 $this->assertLessThanOrEqual( 0, $curTTLs[$key1],
'Key 1 has current TTL <= 0' );
750 $this->assertLessThanOrEqual( 0, $curTTLs[$key2],
'Key 2 has current TTL <= 0' );
755 [ $key1 => $value1, $key2 => $value2 ],
756 $cache->
getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ),
757 "Result array still populated even with new check keys"
759 $this->assertEquals( 2,
count( $curTTLs ),
"Current TTLs still array set" );
760 $this->assertLessThan( 0, $curTTLs[$key1],
'Key 1 has negative current TTL' );
761 $this->assertLessThan( 0, $curTTLs[$key2],
'Key 2 has negative current TTL' );
780 foreach ( [ $checkAll, $check1, $check2 ]
as $checkKey ) {
796 [
'key1' => $value1,
'key2' => $value2 ],
800 $this->assertGreaterThanOrEqual( 9.5, $curTTLs[
'key1'],
'Initial ttls' );
801 $this->assertLessThanOrEqual( 10.5, $curTTLs[
'key1'],
'Initial ttls' );
802 $this->assertGreaterThanOrEqual( 9.5, $curTTLs[
'key2'],
'Initial ttls' );
803 $this->assertLessThanOrEqual( 10.5, $curTTLs[
'key2'],
'Initial ttls' );
815 [
'key1' => $value1,
'key2' => $value2 ],
817 'key1 expired by check1, but value still provided'
819 $this->assertLessThan( 0, $curTTLs[
'key1'],
'key1 TTL expired' );
820 $this->assertGreaterThan( 0, $curTTLs[
'key2'],
'key2 still valid' );
832 [
'key1' => $value1,
'key2' => $value2 ],
834 'All keys expired by checkAll, but value still provided'
836 $this->assertLessThan( 0, $curTTLs[
'key1'],
'key1 expired by checkAll' );
837 $this->assertLessThan( 0, $curTTLs[
'key2'],
'key2 expired by checkAll' );
847 for ( $i = 0; $i < 500; ++$i ) {
851 $cache->
get( $key, $curTTL, [ $checkKey ] );
854 $v =
$cache->
get( $key, $curTTL, [ $checkKey ] );
856 $this->assertEquals(
'val', $v );
857 $this->assertLessThan( 0, $curTTL,
"Step $i: CTL < 0 (miss/set/hit)" );
860 for ( $i = 0; $i < 500; ++$i ) {
866 $v =
$cache->
get( $key, $curTTL, [ $checkKey ] );
868 $this->assertEquals(
'val', $v );
869 $this->assertLessThan( 0, $curTTL,
"Step $i: CTL < 0 (set/hit)" );
882 $v = $this->
cache->get( $key, $curTTL );
883 $this->assertEquals(
$value, $v,
"Key was created with value" );
884 $this->assertGreaterThan( 0, $curTTL,
"Existing key has current TTL > 0" );
886 $this->
cache->delete( $key );
889 $v = $this->
cache->get( $key, $curTTL );
890 $this->assertFalse( $v,
"Deleted key has false value" );
891 $this->assertLessThan( 0, $curTTL,
"Deleted key has current TTL < 0" );
894 $v = $this->
cache->get( $key, $curTTL );
895 $this->assertFalse( $v,
"Deleted key is tombstoned and has false value" );
896 $this->assertLessThan( 0, $curTTL,
"Deleted key is tombstoned and has current TTL < 0" );
902 $v = $this->
cache->get( $key, $curTTL );
903 $this->assertFalse( $v,
"Deleted key has false value" );
904 $this->assertNull( $curTTL,
"Deleted key has null current TTL" );
907 $v = $this->
cache->get( $key, $curTTL );
908 $this->assertEquals(
$value, $v,
"Key was created with value" );
909 $this->assertGreaterThan( 0, $curTTL,
"Existing key has current TTL > 0" );
924 $func =
function ( $old, &$ttl )
use ( &$wasSet,
$value ) {
932 $this->assertEquals(
$value, $v,
"Value returned" );
933 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
935 $this->assertEquals( 1, $wasSet,
"Value not regenerated" );
938 $verOpts = [
'version' => $extOpts[
'version'] + 1 ];
942 $this->assertEquals(
$value, $v,
"Value returned" );
943 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
947 $this->assertEquals(
$value, $v,
"Value returned" );
948 $this->assertEquals( 0, $wasSet,
"Value not regenerated" );
953 $this->assertEquals( 0, $wasSet,
"Value not regenerated" );
958 $this->assertEquals(
$value, $v,
"Value returned" );
959 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
963 $verOpts = [
'version' => $extOpts[
'version'] + 1 ];
965 $this->assertEquals(
$value, $v,
"Value returned" );
966 $this->assertEquals( 1, $wasSet,
"Value regenerated" );
973 [ [
'version' => 1 ],
true ]
985 $priorTime = microtime(
true );
987 $t0 = $this->
cache->getCheckKeyTime( $key );
988 $this->assertGreaterThanOrEqual( $priorTime, $t0,
'Check key auto-created' );
990 $priorTime = microtime(
true );
992 $this->
cache->touchCheckKey( $key );
993 $t1 = $this->
cache->getCheckKeyTime( $key );
994 $this->assertGreaterThanOrEqual( $priorTime, $t1,
'Check key created' );
996 $t2 = $this->
cache->getCheckKeyTime( $key );
997 $this->assertEquals( $t1, $t2,
'Check key time did not change' );
1000 $this->
cache->touchCheckKey( $key );
1001 $t3 = $this->
cache->getCheckKeyTime( $key );
1002 $this->assertGreaterThan( $t2, $t3,
'Check key time increased' );
1004 $t4 = $this->
cache->getCheckKeyTime( $key );
1005 $this->assertEquals( $t3, $t4,
'Check key time did not change' );
1008 $this->
cache->resetCheckKey( $key );
1009 $t5 = $this->
cache->getCheckKeyTime( $key );
1010 $this->assertGreaterThan( $t4, $t5,
'Check key time increased' );
1012 $t6 = $this->
cache->getCheckKeyTime( $key );
1013 $this->assertEquals( $t5, $t6,
'Check key time did not change' );
1026 $this->internalCache->set(
1030 $this->internalCache->set(
1034 $this->internalCache->set(
1041 $v = $this->
cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] );
1042 $this->assertEquals(
$value, $v,
"Value matches" );
1043 $this->assertLessThan( -4.9, $curTTL,
"Correct CTL" );
1044 $this->assertGreaterThan( -5.1, $curTTL,
"Correct CTL" );
1058 $knownPurge = time() - 60;
1059 $goodTime = microtime(
true ) - 5;
1060 $badTime = microtime(
true ) - 300;
1062 $this->internalCache->set(
1071 $this->internalCache->set(
1080 $this->internalCache->set(
1084 $this->internalCache->set(
1089 $this->assertEquals(
$value, $this->
cache->get( $vKey1 ) );
1090 $this->assertEquals(
$value, $this->
cache->get( $vKey2 ) );
1091 $this->
cache->reap( $vKey1, $knownPurge, $bad1 );
1092 $this->
cache->reap( $vKey2, $knownPurge, $bad2 );
1094 $this->assertFalse( $bad1 );
1095 $this->assertTrue( $bad2 );
1097 $this->
cache->reapCheckKey( $tKey1, $knownPurge, $tBad1 );
1098 $this->
cache->reapCheckKey( $tKey2, $knownPurge, $tBad2 );
1099 $this->assertFalse( $tBad1 );
1100 $this->assertTrue( $tBad2 );
1110 $opts = [
'lag' => 300,
'since' => microtime(
true ) ];
1112 $this->assertEquals(
$value, $this->
cache->get( $key ),
"Rep-lagged value written." );
1115 $opts = [
'lag' => 0,
'since' => microtime(
true ) - 300 ];
1117 $this->assertEquals(
false, $this->
cache->get( $key ),
"Trx-lagged value not written." );
1120 $opts = [
'lag' => 5,
'since' => microtime(
true ) - 5 ];
1122 $this->assertEquals(
false, $this->
cache->get( $key ),
"Lagged value not written." );
1132 $opts = [
'pending' =>
true ];
1134 $this->assertEquals(
false, $this->
cache->get( $key ),
"Pending value not written." );
1138 $localBag = $this->getMockBuilder(
'EmptyBagOStuff' )
1139 ->setMethods( [
'set',
'delete' ] )->getMock();
1140 $localBag->expects( $this->never() )->method(
'set' );
1141 $localBag->expects( $this->never() )->method(
'delete' );
1143 'cache' => $localBag,
1144 'pool' =>
'testcache-hash',
1147 $valFunc =
function () {
1152 $wanCache->get(
'x' );
1153 $wanCache->get(
'x', $ctl, [
'check1' ] );
1154 $wanCache->getMulti( [
'x',
'y' ] );
1155 $wanCache->getMulti( [
'x',
'y' ], $ctls, [
'check2' ] );
1156 $wanCache->getWithSetCallback(
'p', 30, $valFunc );
1157 $wanCache->getCheckKeyTime(
'zzz' );
1158 $wanCache->reap(
'x', time() - 300 );
1159 $wanCache->reap(
'zzz', time() - 300 );
1172 $mtime = $ago ? time() - $ago : $ago;
1174 $ttl = $this->
cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor );
1176 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1177 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1179 $ttl = $this->
cache->adaptiveTTL( (
string)$mtime, $maxTTL, $minTTL, $factor );
1181 $this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl );
1182 $this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl );
1187 [ 3600, 900, 30, 0.2, 720 ],
1188 [ 3600, 500, 30, 0.2, 500 ],
1189 [ 3600, 86400, 800, 0.2, 800 ],
1190 [
false, 86400, 800, 0.2, 800 ],
1191 [
null, 86400, 800, 0.2, 800 ]
1200 ->setMethods( [
'makeKey' ] )->getMock();
1201 $backend->expects( $this->once() )->method(
'makeKey' )
1202 ->willReturn(
'special' );
1205 'cache' => $backend,
1206 'pool' =>
'testcache-hash',
1210 $this->assertSame(
'special', $wanCache->makeKey(
'a',
'b' ) );
1218 ->setMethods( [
'makeGlobalKey' ] )->getMock();
1219 $backend->expects( $this->once() )->method(
'makeGlobalKey' )
1220 ->willReturn(
'special' );
1223 'cache' => $backend,
1224 'pool' =>
'testcache-hash',
1228 $this->assertSame(
'special', $wanCache->makeGlobalKey(
'a',
'b' ) );