| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
namespace Wikimedia\Tests\ObjectCache; |
| 6 |
|
| 7 |
use ArrayIterator; |
| 8 |
use MediaWikiUnitTestCase; |
| 9 |
use Psr\Log\NullLogger; |
| 10 |
use UnexpectedValueException; |
| 11 |
use Wikimedia\ObjectCache\BagOStuff; |
| 12 |
use Wikimedia\ObjectCache\EmptyBagOStuff; |
| 13 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 14 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 15 |
use Wikimedia\TestingAccessWrapper; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class WANObjectCacheTest extends MediaWikiUnitTestCase { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
private function newWanCache( array $params = [] ) { |
| 27 |
if ( isset( $params['broadcastRoutingPrefix'] ) ) { |
| 28 |
|
| 29 |
$bag = new McrouterHashBagOStuff(); |
| 30 |
} elseif ( isset( $params['serialize'] ) ) { |
| 31 |
$bag = new SerialHashBagOStuff(); |
| 32 |
} else { |
| 33 |
$bag = new HashBagOStuff(); |
| 34 |
} |
| 35 |
|
| 36 |
$cache = new WANObjectCache( [ 'cache' => $bag ] + $params ); |
| 37 |
|
| 38 |
return [ $cache, $bag ]; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function testSetAndGet( $value, $ttl ) { |
| 45 |
[ $cache ] = $this->newWanCache(); |
| 46 |
|
| 47 |
$curTTL = null; |
| 48 |
$asOf = null; |
| 49 |
$key = $cache->makeKey( 'x', wfRandomString() ); |
| 50 |
|
| 51 |
$cache->get( $key, $curTTL, [], $asOf ); |
| 52 |
$this->assertSame( null, $curTTL, "Current TTL (absent)" ); |
| 53 |
$this->assertSame( null, $asOf, "Current as-of-time (absent)" ); |
| 54 |
|
| 55 |
$t = microtime( true ); |
| 56 |
|
| 57 |
$cache->set( $key, $value, $cache::TTL_UNCACHEABLE ); |
| 58 |
$cache->get( $key, $curTTL, [], $asOf ); |
| 59 |
$this->assertSame( null, $curTTL, "Current TTL (TTL_UNCACHEABLE)" ); |
| 60 |
$this->assertSame( null, $asOf, "Current as-of-time (TTL_UNCACHEABLE)" ); |
| 61 |
|
| 62 |
$cache->set( $key, $value, $ttl ); |
| 63 |
|
| 64 |
$this->assertSame( $value, $cache->get( $key, $curTTL, [], $asOf ) ); |
| 65 |
if ( $ttl === INF || $ttl === WANObjectCache::TTL_INDEFINITE ) { |
| 66 |
$this->assertSame( INF, $curTTL, "Current TTL" ); |
| 67 |
} else { |
| 68 |
$this->assertGreaterThan( 0, $curTTL, "Current TTL" ); |
| 69 |
$this->assertLessThanOrEqual( $ttl, $curTTL, "Current TTL < nominal TTL" ); |
| 70 |
} |
| 71 |
$this->assertGreaterThanOrEqual( $t - 1, $asOf, "As-of-time in range of set() time" ); |
| 72 |
$this->assertLessThanOrEqual( $t + 1, $asOf, "As-of-time in range of set() time" ); |
| 73 |
} |
| 74 |
|
| 75 |
public static function provideSetAndGet() { |
| 76 |
$a1 = [ 1 ]; |
| 77 |
$a2 = [ 'a' => &$a1 ]; |
| 78 |
|
| 79 |
$o1 = (object)[ 'v' => 1 ]; |
| 80 |
$o2 = (object)[ 'a' => &$o1 ]; |
| 81 |
|
| 82 |
$co = (object)[ 'p' => 93 ]; |
| 83 |
$co->f =& $co; |
| 84 |
|
| 85 |
return [ |
| 86 |
|
| 87 |
[ 14141, 3 ], |
| 88 |
[ 3535.666, 3 ], |
| 89 |
[ [], 3 ], |
| 90 |
[ '0', 3 ], |
| 91 |
[ (object)[ 'meow' ], 3 ], |
| 92 |
[ INF, 3 ], |
| 93 |
[ '', 3 ], |
| 94 |
[ 'pizzacat1', INF ], |
| 95 |
[ 'pizzacat2', WANObjectCache::TTL_INDEFINITE ], |
| 96 |
[ null, 80 ], |
| 97 |
[ $a2, 3 ], |
| 98 |
[ $o2, 3 ], |
| 99 |
[ $co, 3 ] |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function testGetNotExists() { |
| 104 |
[ $cache ] = $this->newWanCache(); |
| 105 |
|
| 106 |
$key = $cache->makeGlobalKey( 'y', wfRandomString(), 'p' ); |
| 107 |
$curTTL = null; |
| 108 |
$value = $cache->get( $key, $curTTL ); |
| 109 |
|
| 110 |
$this->assertSame( false, $value, "Return value" ); |
| 111 |
$this->assertSame( null, $curTTL, "current TTL" ); |
| 112 |
} |
| 113 |
|
| 114 |
public function testSetOver() { |
| 115 |
[ $cache ] = $this->newWanCache(); |
| 116 |
|
| 117 |
$key = wfRandomString(); |
| 118 |
for ( $i = 0; $i < 3; ++$i ) { |
| 119 |
$value = wfRandomString(); |
| 120 |
$cache->set( $key, $value, 3 ); |
| 121 |
|
| 122 |
$this->assertSame( $cache->get( $key ), $value ); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public static function provideStaleSetParams() { |
| 127 |
return [ |
| 128 |
|
| 129 |
|
| 130 |
[ 30, 0.0, false ], |
| 131 |
[ 30, 2, false ], |
| 132 |
[ 30, 10, false ], |
| 133 |
[ 30, 20, false ], |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
[ 30, 28, true ], |
| 139 |
|
| 140 |
[ 30, null, true ], |
| 141 |
]; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function testStaleSet( $ago, $walltime, $cacheable ) { |
| 151 |
[ $cache ] = $this->newWanCache(); |
| 152 |
$mockWallClock = 1549343530.0; |
| 153 |
$cache->setMockTime( $mockWallClock ); |
| 154 |
|
| 155 |
$key = wfRandomString(); |
| 156 |
$value = wfRandomString(); |
| 157 |
|
| 158 |
$cache->set( |
| 159 |
$key, |
| 160 |
$value, |
| 161 |
$cache::TTL_MINUTE, |
| 162 |
[ 'since' => $mockWallClock - $ago, 'walltime' => $walltime ] |
| 163 |
); |
| 164 |
|
| 165 |
$this->assertSame( |
| 166 |
$cacheable ? $value : false, |
| 167 |
$cache->get( $key ), |
| 168 |
"Stale set() value ignored" |
| 169 |
); |
| 170 |
} |
| 171 |
|
| 172 |
public function testProcessCacheTTL() { |
| 173 |
[ $cache ] = $this->newWanCache(); |
| 174 |
$mockWallClock = 1549343530.0; |
| 175 |
$cache->setMockTime( $mockWallClock ); |
| 176 |
|
| 177 |
$key = "mykey-" . wfRandomString(); |
| 178 |
|
| 179 |
$hits = 0; |
| 180 |
$callback = static function ( $oldValue, &$ttl, &$setOpts ) use ( &$hits ) { |
| 181 |
++$hits; |
| 182 |
return 42; |
| 183 |
}; |
| 184 |
|
| 185 |
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); |
| 186 |
$cache->delete( $key, $cache::HOLDOFF_TTL_NONE ); |
| 187 |
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); |
| 188 |
$this->assertSame( 1, $hits, "Value process cached" ); |
| 189 |
|
| 190 |
$mockWallClock += 6; |
| 191 |
$cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); |
| 192 |
$this->assertSame( 2, $hits, "Value expired in process cache" ); |
| 193 |
} |
| 194 |
|
| 195 |
public function testProcessCacheLruAndDelete() { |
| 196 |
[ $cache ] = $this->newWanCache(); |
| 197 |
$mockWallClock = 1549343530.0; |
| 198 |
$cache->setMockTime( $mockWallClock ); |
| 199 |
|
| 200 |
$hit = 0; |
| 201 |
$fn = static function () use ( &$hit ) { |
| 202 |
++$hit; |
| 203 |
return 42; |
| 204 |
}; |
| 205 |
$keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ]; |
| 206 |
$keysB = [ wfRandomString(), wfRandomString(), wfRandomString() ]; |
| 207 |
$pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ]; |
| 208 |
|
| 209 |
foreach ( $keysA as $i => $key ) { |
| 210 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 211 |
} |
| 212 |
$this->assertSame( 3, $hit, "Values not cached yet" ); |
| 213 |
|
| 214 |
foreach ( $keysA as $i => $key ) { |
| 215 |
|
| 216 |
$cache->delete( $key ); |
| 217 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 218 |
} |
| 219 |
$this->assertSame( 3, $hit, "Values cached; not cleared by delete()" ); |
| 220 |
|
| 221 |
foreach ( $keysB as $i => $key ) { |
| 222 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 223 |
} |
| 224 |
$this->assertSame( 6, $hit, "New values not cached yet" ); |
| 225 |
|
| 226 |
foreach ( $keysB as $i => $key ) { |
| 227 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 228 |
} |
| 229 |
$this->assertSame( 6, $hit, "New values cached" ); |
| 230 |
|
| 231 |
foreach ( $keysA as $i => $key ) { |
| 232 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 233 |
} |
| 234 |
$this->assertSame( 9, $hit, "Prior values evicted by new values" ); |
| 235 |
} |
| 236 |
|
| 237 |
public function testProcessCacheInterimKeys() { |
| 238 |
[ $cache ] = $this->newWanCache(); |
| 239 |
$mockWallClock = 1549343530.0; |
| 240 |
$cache->setMockTime( $mockWallClock ); |
| 241 |
|
| 242 |
$hit = 0; |
| 243 |
$fn = static function () use ( &$hit ) { |
| 244 |
++$hit; |
| 245 |
return 42; |
| 246 |
}; |
| 247 |
$keysA = [ wfRandomString(), wfRandomString(), wfRandomString() ]; |
| 248 |
$pcg = [ 'thiscache:1', 'thatcache:1', 'somecache:1' ]; |
| 249 |
|
| 250 |
|
| 251 |
foreach ( $keysA as $key ) { |
| 252 |
$cache->delete( $key ); |
| 253 |
} |
| 254 |
|
| 255 |
$mockWallClock += 1; |
| 256 |
foreach ( $keysA as $i => $key ) { |
| 257 |
|
| 258 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5, 'pcGroup' => $pcg[$i] ] ); |
| 259 |
} |
| 260 |
$this->assertSame( 3, $hit ); |
| 261 |
|
| 262 |
|
| 263 |
$key = reset( $keysA ); |
| 264 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] ); |
| 265 |
$this->assertSame( 3, $hit, "Value recently interim-cached" ); |
| 266 |
|
| 267 |
$mockWallClock += 1; |
| 268 |
$cache->clearProcessCache(); |
| 269 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] ); |
| 270 |
$this->assertSame( 4, $hit, "Value calculated (interim key not recent and reset)" ); |
| 271 |
$cache->getWithSetCallback( $key, 100, $fn, [ 'pcTTL' => 5 ] ); |
| 272 |
$this->assertSame( 4, $hit, "Value process cached" ); |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
public function testProcessCacheNesting() { |
| 278 |
[ $cache ] = $this->newWanCache(); |
| 279 |
$mockWallClock = 1549343530.0; |
| 280 |
$cache->setMockTime( $mockWallClock ); |
| 281 |
|
| 282 |
$keyOuter = "outer-" . wfRandomString(); |
| 283 |
$keyInner = "inner-" . wfRandomString(); |
| 284 |
|
| 285 |
$innerHit = 0; |
| 286 |
$innerFn = static function () use ( &$innerHit ) { |
| 287 |
++$innerHit; |
| 288 |
return 42; |
| 289 |
}; |
| 290 |
|
| 291 |
$outerHit = 0; |
| 292 |
$outerFn = static function () use ( $keyInner, $innerFn, $cache, &$outerHit ) { |
| 293 |
++$outerHit; |
| 294 |
$v = $cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] ); |
| 295 |
|
| 296 |
return 43 + $v; |
| 297 |
}; |
| 298 |
|
| 299 |
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] ); |
| 300 |
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] ); |
| 301 |
|
| 302 |
$this->assertSame( 1, $innerHit, "Inner callback value cached" ); |
| 303 |
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE ); |
| 304 |
$mockWallClock += 1; |
| 305 |
|
| 306 |
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] ); |
| 307 |
$this->assertSame( 1, $innerHit, "Inner callback process cached" ); |
| 308 |
|
| 309 |
|
| 310 |
$cache->getWithSetCallback( $keyOuter, 100, $outerFn ); |
| 311 |
|
| 312 |
$this->assertSame( 1, $outerHit, "Outer callback value not yet cached" ); |
| 313 |
$this->assertSame( 2, $innerHit, "Inner callback value process cache skipped" ); |
| 314 |
|
| 315 |
$cache->getWithSetCallback( $keyOuter, 100, $outerFn ); |
| 316 |
|
| 317 |
$this->assertSame( 1, $outerHit, "Outer callback value cached" ); |
| 318 |
|
| 319 |
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE ); |
| 320 |
$cache->delete( $keyOuter, $cache::HOLDOFF_TTL_NONE ); |
| 321 |
$mockWallClock += 1; |
| 322 |
$cache->clearProcessCache(); |
| 323 |
$cache->getWithSetCallback( $keyOuter, 100, $outerFn ); |
| 324 |
|
| 325 |
$this->assertSame( 2, $outerHit, "Outer callback value not yet cached" ); |
| 326 |
$this->assertSame( 3, $innerHit, "Inner callback value not yet cached" ); |
| 327 |
|
| 328 |
$cache->delete( $keyInner, $cache::HOLDOFF_TTL_NONE ); |
| 329 |
$mockWallClock += 1; |
| 330 |
$cache->getWithSetCallback( $keyInner, 100, $innerFn, [ 'pcTTL' => 5 ] ); |
| 331 |
|
| 332 |
$this->assertSame( 3, $innerHit, "Inner callback value process cached" ); |
| 333 |
} |
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public function testGetWithSetCallback( array $extOpts ) { |
| 340 |
[ $cache ] = $this->newWanCache(); |
| 341 |
|
| 342 |
$key = wfRandomString(); |
| 343 |
$value = wfRandomString(); |
| 344 |
$cKey1 = wfRandomString(); |
| 345 |
$cKey2 = wfRandomString(); |
| 346 |
|
| 347 |
$priorValue = null; |
| 348 |
$priorAsOf = null; |
| 349 |
$wasSet = 0; |
| 350 |
$func = static function ( $old, &$ttl, &$opts, $asOf ) |
| 351 |
use ( &$wasSet, &$priorValue, &$priorAsOf, $value ) { |
| 352 |
++$wasSet; |
| 353 |
$priorValue = $old; |
| 354 |
$priorAsOf = $asOf; |
| 355 |
$ttl = 20; |
| 356 |
return $value; |
| 357 |
}; |
| 358 |
|
| 359 |
$mockWallClock = 1549343530.0; |
| 360 |
$priorTime = $mockWallClock; |
| 361 |
$cache->setMockTime( $mockWallClock ); |
| 362 |
|
| 363 |
$wasSet = 0; |
| 364 |
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] + $extOpts ); |
| 365 |
$this->assertSame( $value, $v, "Value returned" ); |
| 366 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 367 |
$this->assertSame( false, $priorValue, "No prior value" ); |
| 368 |
$this->assertSame( null, $priorAsOf, "No prior value" ); |
| 369 |
|
| 370 |
$curTTL = null; |
| 371 |
$cache->get( $key, $curTTL ); |
| 372 |
$this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overridden)' ); |
| 373 |
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overridden)' ); |
| 374 |
|
| 375 |
$wasSet = 0; |
| 376 |
$v = $cache->getWithSetCallback( |
| 377 |
$key, 30, $func, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts ); |
| 378 |
$this->assertSame( $value, $v, "Value returned" ); |
| 379 |
$this->assertSame( 0, $wasSet, "Value not regenerated" ); |
| 380 |
|
| 381 |
$mockWallClock += 1; |
| 382 |
|
| 383 |
$wasSet = 0; |
| 384 |
$v = $cache->getWithSetCallback( |
| 385 |
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts |
| 386 |
); |
| 387 |
$this->assertSame( $value, $v, "Value returned" ); |
| 388 |
$this->assertSame( 1, $wasSet, "Value regenerated due to check keys" ); |
| 389 |
$this->assertSame( $value, $priorValue, "Has prior value" ); |
| 390 |
$this->assertIsFloat( $priorAsOf, "Has prior value" ); |
| 391 |
$t1 = $cache->getCheckKeyTime( $cKey1 ); |
| 392 |
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' ); |
| 393 |
$t2 = $cache->getCheckKeyTime( $cKey2 ); |
| 394 |
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' ); |
| 395 |
|
| 396 |
$mockWallClock += 1; |
| 397 |
$priorTime = $mockWallClock; |
| 398 |
$wasSet = 0; |
| 399 |
$v = $cache->getWithSetCallback( |
| 400 |
$key, 30, $func, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts |
| 401 |
); |
| 402 |
$this->assertSame( $value, $v, "Value returned" ); |
| 403 |
$this->assertSame( 1, $wasSet, "Value regenerated due to still-recent check keys" ); |
| 404 |
$t1 = $cache->getCheckKeyTime( $cKey1 ); |
| 405 |
$this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' ); |
| 406 |
$t2 = $cache->getCheckKeyTime( $cKey2 ); |
| 407 |
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' ); |
| 408 |
|
| 409 |
$curTTL = null; |
| 410 |
$v = $cache->get( $key, $curTTL, [ $cKey1, $cKey2 ] ); |
| 411 |
$this->assertSame( $value, $v, "Value returned" ); |
| 412 |
$this->assertGreaterThan( 0, $curTTL, "Value has current TTL > 0 due to T344191" ); |
| 413 |
|
| 414 |
$wasSet = 0; |
| 415 |
$key = wfRandomString(); |
| 416 |
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts ); |
| 417 |
$this->assertSame( $value, $v, "Value returned" ); |
| 418 |
$cache->delete( $key ); |
| 419 |
$v = $cache->getWithSetCallback( $key, 30, $func, [ 'pcTTL' => 5 ] + $extOpts ); |
| 420 |
$this->assertSame( $value, $v, "Value still returned after deleted" ); |
| 421 |
$this->assertSame( 1, $wasSet, "Value process cached while deleted" ); |
| 422 |
|
| 423 |
$oldValReceived = -1; |
| 424 |
$oldAsOfReceived = -1; |
| 425 |
$checkFunc = static function ( $oldVal, &$ttl, array $setOpts, $oldAsOf ) |
| 426 |
use ( &$oldValReceived, &$oldAsOfReceived, &$wasSet ) { |
| 427 |
++$wasSet; |
| 428 |
$oldValReceived = $oldVal; |
| 429 |
$oldAsOfReceived = $oldAsOf; |
| 430 |
|
| 431 |
return 'xxx' . $wasSet; |
| 432 |
}; |
| 433 |
|
| 434 |
$mockWallClock = 1549343530.0; |
| 435 |
$priorTime = $mockWallClock; |
| 436 |
|
| 437 |
$wasSet = 0; |
| 438 |
$key = wfRandomString(); |
| 439 |
$v = $cache->getWithSetCallback( |
| 440 |
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts ); |
| 441 |
$this->assertSame( 'xxx1', $v, "Value returned" ); |
| 442 |
$this->assertSame( false, $oldValReceived, "Callback got no stale value" ); |
| 443 |
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" ); |
| 444 |
|
| 445 |
$mockWallClock += 40; |
| 446 |
$v = $cache->getWithSetCallback( |
| 447 |
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts ); |
| 448 |
$this->assertSame( 'xxx2', $v, "Value still returned after expired" ); |
| 449 |
$this->assertSame( 2, $wasSet, "Value recalculated while expired" ); |
| 450 |
$this->assertSame( 'xxx1', $oldValReceived, "Callback got stale value" ); |
| 451 |
$this->assertNotEquals( null, $oldAsOfReceived, "Callback got stale value" ); |
| 452 |
|
| 453 |
$mockWallClock += 260; |
| 454 |
$v = $cache->getWithSetCallback( |
| 455 |
$key, 30, $checkFunc, [ 'staleTTL' => 50 ] + $extOpts ); |
| 456 |
$this->assertSame( 'xxx3', $v, "Value still returned after expired" ); |
| 457 |
$this->assertSame( 3, $wasSet, "Value recalculated while expired" ); |
| 458 |
$this->assertSame( false, $oldValReceived, "Callback got no stale value" ); |
| 459 |
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" ); |
| 460 |
|
| 461 |
$mockWallClock = ( $priorTime - $cache::HOLDOFF_TTL - 1 ); |
| 462 |
$wasSet = 0; |
| 463 |
$key = wfRandomString(); |
| 464 |
$checkKey = $cache->makeKey( 'template', 'X' ); |
| 465 |
$cache->touchCheckKey( $checkKey ); |
| 466 |
$mockWallClock = $priorTime; |
| 467 |
$v = $cache->getWithSetCallback( |
| 468 |
$key, |
| 469 |
$cache::TTL_INDEFINITE, |
| 470 |
$checkFunc, |
| 471 |
[ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts |
| 472 |
); |
| 473 |
$this->assertSame( 'xxx1', $v, "Value returned" ); |
| 474 |
$this->assertSame( 1, $wasSet, "Value computed" ); |
| 475 |
$this->assertSame( false, $oldValReceived, "Callback got no stale value" ); |
| 476 |
$this->assertSame( null, $oldAsOfReceived, "Callback got no stale value" ); |
| 477 |
|
| 478 |
$mockWallClock += $cache::TTL_HOUR; |
| 479 |
$v = $cache->getWithSetCallback( |
| 480 |
$key, |
| 481 |
$cache::TTL_INDEFINITE, |
| 482 |
$checkFunc, |
| 483 |
[ |
| 484 |
'graceTTL' => $cache::TTL_WEEK, |
| 485 |
'checkKeys' => [ $checkKey ], |
| 486 |
'ageNew' => -1 |
| 487 |
] + $extOpts |
| 488 |
); |
| 489 |
$this->assertSame( 'xxx1', $v, "Cached value returned" ); |
| 490 |
$this->assertSame( 1, $wasSet, "Cached value returned" ); |
| 491 |
|
| 492 |
$cache->touchCheckKey( $checkKey ); |
| 493 |
$mockWallClock += 0.01; |
| 494 |
|
| 495 |
$v = $cache->getWithSetCallback( |
| 496 |
$key, |
| 497 |
$cache::TTL_INDEFINITE, |
| 498 |
$checkFunc, |
| 499 |
[ |
| 500 |
'graceTTL' => $cache::TTL_WEEK, |
| 501 |
'checkKeys' => [ $checkKey ], |
| 502 |
'ageNew' => -1, |
| 503 |
] + $extOpts |
| 504 |
); |
| 505 |
$this->assertSame( 'xxx1', $v, "Value still returned after expired (in grace)" ); |
| 506 |
$this->assertSame( 1, $wasSet, "Value still returned after expired (in grace)" ); |
| 507 |
|
| 508 |
|
| 509 |
$mockWallClock += $cache::TTL_WEEK; |
| 510 |
$v = $cache->getWithSetCallback( |
| 511 |
$key, |
| 512 |
$cache::TTL_INDEFINITE, |
| 513 |
$checkFunc, |
| 514 |
[ 'graceTTL' => $cache::TTL_WEEK, 'checkKeys' => [ $checkKey ] ] + $extOpts |
| 515 |
); |
| 516 |
$this->assertSame( 'xxx2', $v, "Value was recomputed (past grace)" ); |
| 517 |
$this->assertSame( 2, $wasSet, "Value was recomputed (past grace)" ); |
| 518 |
$this->assertSame( 'xxx1', $oldValReceived, "Callback got post-grace stale value" ); |
| 519 |
$this->assertNotEquals( null, $oldAsOfReceived, "Callback got post-grace stale value" ); |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public function testGetWithSetCallback_touched( array $extOpts ) { |
| 527 |
[ $cache ] = $this->newWanCache(); |
| 528 |
|
| 529 |
$mockWallClock = 1549343530.0; |
| 530 |
$cache->setMockTime( $mockWallClock ); |
| 531 |
|
| 532 |
$checkFunc = static function ( $oldVal, &$ttl, array $setOpts, $oldAsOf ) |
| 533 |
use ( &$wasSet ) { |
| 534 |
++$wasSet; |
| 535 |
|
| 536 |
return 'xxx' . $wasSet; |
| 537 |
}; |
| 538 |
|
| 539 |
$key = wfRandomString(); |
| 540 |
$wasSet = 0; |
| 541 |
$touched = null; |
| 542 |
$touchedCallback = static function () use ( &$touched ) { |
| 543 |
return $touched; |
| 544 |
}; |
| 545 |
$v = $cache->getWithSetCallback( |
| 546 |
$key, |
| 547 |
$cache::TTL_INDEFINITE, |
| 548 |
$checkFunc, |
| 549 |
[ 'touchedCallback' => $touchedCallback ] + $extOpts |
| 550 |
); |
| 551 |
$mockWallClock += 60; |
| 552 |
$v = $cache->getWithSetCallback( |
| 553 |
$key, |
| 554 |
$cache::TTL_INDEFINITE, |
| 555 |
$checkFunc, |
| 556 |
[ 'touchedCallback' => $touchedCallback ] + $extOpts |
| 557 |
); |
| 558 |
$this->assertSame( 'xxx1', $v, "Value was computed once" ); |
| 559 |
$this->assertSame( 1, $wasSet, "Value was computed once" ); |
| 560 |
|
| 561 |
$touched = $mockWallClock - 10; |
| 562 |
$v = $cache->getWithSetCallback( |
| 563 |
$key, |
| 564 |
$cache::TTL_INDEFINITE, |
| 565 |
$checkFunc, |
| 566 |
[ 'touchedCallback' => $touchedCallback ] + $extOpts |
| 567 |
); |
| 568 |
$v = $cache->getWithSetCallback( |
| 569 |
$key, |
| 570 |
$cache::TTL_INDEFINITE, |
| 571 |
$checkFunc, |
| 572 |
[ 'touchedCallback' => $touchedCallback ] + $extOpts |
| 573 |
); |
| 574 |
$this->assertSame( 'xxx2', $v, "Value was recomputed once" ); |
| 575 |
$this->assertSame( 2, $wasSet, "Value was recomputed once" ); |
| 576 |
} |
| 577 |
|
| 578 |
public static function getWithSetCallback_provider() { |
| 579 |
return [ |
| 580 |
[ [], false ], |
| 581 |
[ [ 'version' => 1 ], true ] |
| 582 |
]; |
| 583 |
} |
| 584 |
|
| 585 |
public function testPreemptiveRefresh() { |
| 586 |
|
| 587 |
$this->markTestSkippedIfPhp( '>=', '8.2' ); |
| 588 |
|
| 589 |
$value = 'KatCafe'; |
| 590 |
$wasSet = 0; |
| 591 |
$func = static function ( $old, &$ttl, &$opts, $asOf ) use ( &$wasSet, &$value ) |
| 592 |
{ |
| 593 |
++$wasSet; |
| 594 |
return $value; |
| 595 |
}; |
| 596 |
|
| 597 |
$cache = new NearExpiringWANObjectCache( [ 'cache' => new HashBagOStuff() ] ); |
| 598 |
$mockWallClock = 1549343530.0; |
| 599 |
$cache->setMockTime( $mockWallClock ); |
| 600 |
|
| 601 |
$wasSet = 0; |
| 602 |
$key = wfRandomString(); |
| 603 |
$opts = [ 'lowTTL' => 30 ]; |
| 604 |
$v = $cache->getWithSetCallback( $key, 20, $func, $opts ); |
| 605 |
$this->assertSame( $value, $v, "Value returned" ); |
| 606 |
$this->assertSame( 1, $wasSet, "Value calculated" ); |
| 607 |
|
| 608 |
$mockWallClock += 1; |
| 609 |
$v = $cache->getWithSetCallback( $key, 20, $func, $opts ); |
| 610 |
$this->assertSame( 2, $wasSet, "Value re-calculated" ); |
| 611 |
|
| 612 |
$wasSet = 0; |
| 613 |
$key = wfRandomString(); |
| 614 |
$opts = [ 'lowTTL' => 1 ]; |
| 615 |
$v = $cache->getWithSetCallback( $key, 30, $func, $opts ); |
| 616 |
$this->assertSame( $value, $v, "Value returned" ); |
| 617 |
$this->assertSame( 1, $wasSet, "Value calculated" ); |
| 618 |
$v = $cache->getWithSetCallback( $key, 30, $func, $opts ); |
| 619 |
$this->assertSame( 1, $wasSet, "Value cached" ); |
| 620 |
|
| 621 |
$asycList = []; |
| 622 |
$asyncHandler = static function ( $callback ) use ( &$asycList ) { |
| 623 |
$asycList[] = $callback; |
| 624 |
}; |
| 625 |
$cache = new NearExpiringWANObjectCache( [ |
| 626 |
'cache' => new HashBagOStuff(), |
| 627 |
'asyncHandler' => $asyncHandler |
| 628 |
] ); |
| 629 |
|
| 630 |
$mockWallClock = 1549343530.0; |
| 631 |
$priorTime = $mockWallClock; |
| 632 |
$cache->setMockTime( $mockWallClock ); |
| 633 |
|
| 634 |
$wasSet = 0; |
| 635 |
$key = wfRandomString(); |
| 636 |
$opts = [ 'lowTTL' => 100 ]; |
| 637 |
$v = $cache->getWithSetCallback( $key, 300, $func, $opts ); |
| 638 |
$this->assertSame( $value, $v, "Value returned" ); |
| 639 |
$this->assertSame( 1, $wasSet, "Value calculated" ); |
| 640 |
$v = $cache->getWithSetCallback( $key, 300, $func, $opts ); |
| 641 |
$this->assertSame( 1, $wasSet, "Cached value used" ); |
| 642 |
$this->assertSame( $v, $value, "Value cached" ); |
| 643 |
|
| 644 |
$mockWallClock += 250; |
| 645 |
$v = $cache->getWithSetCallback( $key, 300, $func, $opts ); |
| 646 |
$this->assertSame( $value, $v, "Value returned" ); |
| 647 |
$this->assertSame( 1, $wasSet, "Stale value used" ); |
| 648 |
$this->assertCount( 1, $asycList, "Refresh deferred." ); |
| 649 |
$value = 'NewCatsInTown'; |
| 650 |
$asycList[0](); |
| 651 |
$asycList = []; |
| 652 |
$this->assertSame( 2, $wasSet, "Value calculated at later time" ); |
| 653 |
$this->assertSame( [], $asycList, "No deferred refreshes added." ); |
| 654 |
$v = $cache->getWithSetCallback( $key, 300, $func, $opts ); |
| 655 |
$this->assertSame( $value, $v, "New value stored" ); |
| 656 |
|
| 657 |
$cache = new PopularityRefreshingWANObjectCache( [ |
| 658 |
'cache' => new HashBagOStuff() |
| 659 |
] ); |
| 660 |
|
| 661 |
$mockWallClock = $priorTime; |
| 662 |
$cache->setMockTime( $mockWallClock ); |
| 663 |
|
| 664 |
$wasSet = 0; |
| 665 |
$key = wfRandomString(); |
| 666 |
$opts = [ 'hotTTR' => 900 ]; |
| 667 |
$v = $cache->getWithSetCallback( $key, 60, $func, $opts ); |
| 668 |
$this->assertSame( $value, $v, "Value returned" ); |
| 669 |
$this->assertSame( 1, $wasSet, "Value calculated" ); |
| 670 |
|
| 671 |
$mockWallClock += 30; |
| 672 |
|
| 673 |
$v = $cache->getWithSetCallback( $key, 60, $func, $opts ); |
| 674 |
$this->assertSame( 1, $wasSet, "Value cached" ); |
| 675 |
|
| 676 |
$mockWallClock = $priorTime; |
| 677 |
$wasSet = 0; |
| 678 |
$key = wfRandomString(); |
| 679 |
$opts = [ 'hotTTR' => 10 ]; |
| 680 |
$v = $cache->getWithSetCallback( $key, 60, $func, $opts ); |
| 681 |
$this->assertSame( $value, $v, "Value returned" ); |
| 682 |
$this->assertSame( 1, $wasSet, "Value calculated" ); |
| 683 |
|
| 684 |
$mockWallClock += 30; |
| 685 |
|
| 686 |
$v = $cache->getWithSetCallback( $key, 60, $func, $opts ); |
| 687 |
$this->assertSame( $value, $v, "Value returned" ); |
| 688 |
$this->assertSame( 2, $wasSet, "Value re-calculated" ); |
| 689 |
} |
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
public function testGetMultiWithSetCallback( array $extOpts ) { |
| 696 |
[ $cache ] = $this->newWanCache(); |
| 697 |
|
| 698 |
$keyA = wfRandomString(); |
| 699 |
$keyB = wfRandomString(); |
| 700 |
$keyC = wfRandomString(); |
| 701 |
$cKey1 = wfRandomString(); |
| 702 |
$cKey2 = wfRandomString(); |
| 703 |
|
| 704 |
$priorValue = null; |
| 705 |
$priorAsOf = null; |
| 706 |
$wasSet = 0; |
| 707 |
$genFunc = static function ( $id, $old, &$ttl, &$opts, $asOf ) use ( |
| 708 |
&$wasSet, &$priorValue, &$priorAsOf |
| 709 |
) { |
| 710 |
++$wasSet; |
| 711 |
$priorValue = $old; |
| 712 |
$priorAsOf = $asOf; |
| 713 |
$ttl = 20; |
| 714 |
return "@$id$"; |
| 715 |
}; |
| 716 |
|
| 717 |
$mockWallClock = 1549343530.0; |
| 718 |
$priorTime = $mockWallClock; |
| 719 |
$cache->setMockTime( $mockWallClock ); |
| 720 |
|
| 721 |
$wasSet = 0; |
| 722 |
$keyedIds = new ArrayIterator( [ $keyA => 3353 ] ); |
| 723 |
$value = "@3353$"; |
| 724 |
$v = $cache->getMultiWithSetCallback( |
| 725 |
$keyedIds, 30, $genFunc, [ 'lockTSE' => 5 ] + $extOpts ); |
| 726 |
$this->assertSame( $value, $v[$keyA], "Value returned" ); |
| 727 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 728 |
$this->assertSame( false, $priorValue, "No prior value" ); |
| 729 |
$this->assertSame( null, $priorAsOf, "No prior value" ); |
| 730 |
|
| 731 |
$curTTL = null; |
| 732 |
$cache->get( $keyA, $curTTL ); |
| 733 |
$this->assertLessThanOrEqual( 20, $curTTL, 'Current TTL between 19-20 (overridden)' ); |
| 734 |
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Current TTL between 19-20 (overridden)' ); |
| 735 |
|
| 736 |
$wasSet = 0; |
| 737 |
$value = "@efef$"; |
| 738 |
$keyedIds = new ArrayIterator( [ $keyB => 'efef' ] ); |
| 739 |
$v = $cache->getMultiWithSetCallback( |
| 740 |
$keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts ); |
| 741 |
$this->assertSame( $value, $v[$keyB], "Value returned" ); |
| 742 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 743 |
$this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); |
| 744 |
|
| 745 |
$v = $cache->getMultiWithSetCallback( |
| 746 |
$keyedIds, 30, $genFunc, [ 'lowTTL' => 0, 'lockTSE' => 5 ] + $extOpts ); |
| 747 |
$this->assertSame( $value, $v[$keyB], "Value returned" ); |
| 748 |
$this->assertSame( 1, $wasSet, "Value not regenerated" ); |
| 749 |
$this->assertSame( 0, $cache->getWarmupKeyMisses(), "Keys warmed in warmup cache" ); |
| 750 |
|
| 751 |
$mockWallClock += 1; |
| 752 |
|
| 753 |
$cache->touchCheckKey( $cKey1 ); |
| 754 |
$cache->touchCheckKey( $cKey2 ); |
| 755 |
|
| 756 |
$wasSet = 0; |
| 757 |
$keyedIds = new ArrayIterator( [ $keyB => 'efef' ] ); |
| 758 |
$v = $cache->getMultiWithSetCallback( |
| 759 |
$keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts |
| 760 |
); |
| 761 |
$this->assertSame( $value, $v[$keyB], "Value returned" ); |
| 762 |
$this->assertSame( 1, $wasSet, "Value regenerated due to check keys" ); |
| 763 |
$this->assertSame( $value, $priorValue, "Has prior value" ); |
| 764 |
$this->assertIsFloat( $priorAsOf, "Has prior value" ); |
| 765 |
$t1 = $cache->getCheckKeyTime( $cKey1 ); |
| 766 |
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check keys generated on miss' ); |
| 767 |
$t2 = $cache->getCheckKeyTime( $cKey2 ); |
| 768 |
$this->assertGreaterThanOrEqual( $priorTime, $t2, 'Check keys generated on miss' ); |
| 769 |
|
| 770 |
$mockWallClock += 0.01; |
| 771 |
$priorTime = $mockWallClock; |
| 772 |
$value = "@43636$"; |
| 773 |
$wasSet = 0; |
| 774 |
$keyedIds = new ArrayIterator( [ $keyC => 43636 ] ); |
| 775 |
$v = $cache->getMultiWithSetCallback( |
| 776 |
$keyedIds, 30, $genFunc, [ 'checkKeys' => [ $cKey1, $cKey2 ] ] + $extOpts |
| 777 |
); |
| 778 |
$this->assertSame( $value, $v[$keyC], "Value returned" ); |
| 779 |
$this->assertSame( 1, $wasSet, "Value regenerated due to still-recent check keys" ); |
| 780 |
$t1 = $cache->getCheckKeyTime( $cKey1 ); |
| 781 |
$this->assertLessThanOrEqual( $priorTime, $t1, 'Check keys did not change again' ); |
| 782 |
$t2 = $cache->getCheckKeyTime( $cKey2 ); |
| 783 |
$this->assertLessThanOrEqual( $priorTime, $t2, 'Check keys did not change again' ); |
| 784 |
|
| 785 |
$curTTL = null; |
| 786 |
$v = $cache->get( $keyC, $curTTL, [ $cKey1, $cKey2 ] ); |
| 787 |
$this->assertSame( $value, $v, "Value returned" ); |
| 788 |
$this->assertLessThanOrEqual( 0, $curTTL, "Value has current TTL < 0 due to check keys" ); |
| 789 |
|
| 790 |
$wasSet = 0; |
| 791 |
$key = wfRandomString(); |
| 792 |
$keyedIds = new ArrayIterator( [ $key => 242424 ] ); |
| 793 |
$v = $cache->getMultiWithSetCallback( |
| 794 |
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); |
| 795 |
$this->assertSame( "@{$keyedIds[$key]}$", $v[$key], "Value returned" ); |
| 796 |
$cache->delete( $key ); |
| 797 |
$keyedIds = new ArrayIterator( [ $key => 242424 ] ); |
| 798 |
$v = $cache->getMultiWithSetCallback( |
| 799 |
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); |
| 800 |
$this->assertSame( "@{$keyedIds[$key]}$", $v[$key], "Value still returned after deleted" ); |
| 801 |
$this->assertSame( 1, $wasSet, "Value process cached while deleted" ); |
| 802 |
|
| 803 |
$calls = 0; |
| 804 |
$ids = [ 1, 2, 3, 4, 5, 6 ]; |
| 805 |
$keyFunc = static function ( $id, WANObjectCache $wanCache ) { |
| 806 |
return $wanCache->makeKey( 'test', $id ); |
| 807 |
}; |
| 808 |
$keyedIds = $cache->makeMultiKeys( $ids, $keyFunc ); |
| 809 |
$genFunc = static function ( $id, $oldValue, &$ttl, array &$setops ) use ( &$calls ) { |
| 810 |
++$calls; |
| 811 |
|
| 812 |
return "val-{$id}"; |
| 813 |
}; |
| 814 |
$values = $cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc ); |
| 815 |
|
| 816 |
$this->assertSame( |
| 817 |
[ "val-1", "val-2", "val-3", "val-4", "val-5", "val-6" ], |
| 818 |
array_values( $values ), |
| 819 |
"Correct values in correct order" |
| 820 |
); |
| 821 |
$this->assertSame( |
| 822 |
array_map( $keyFunc, $ids, array_fill( 0, count( $ids ), $cache ) ), |
| 823 |
array_keys( $values ), |
| 824 |
"Correct keys in correct order" |
| 825 |
); |
| 826 |
$this->assertSame( count( $ids ), $calls ); |
| 827 |
|
| 828 |
$cache->getMultiWithSetCallback( $keyedIds, 10, $genFunc ); |
| 829 |
$this->assertSame( count( $ids ), $calls, "Values cached" ); |
| 830 |
|
| 831 |
|
| 832 |
$localBag = $this->getMockBuilder( HashBagOStuff::class ) |
| 833 |
->onlyMethods( [ 'getMulti' ] )->getMock(); |
| 834 |
$localBag->expects( $this->once() )->method( 'getMulti' )->willReturn( [ |
| 835 |
'WANCache:v:' . 'k1' => 'val-id1', |
| 836 |
'WANCache:v:' . 'k2' => 'val-id2' |
| 837 |
] ); |
| 838 |
$wanCache = new WANObjectCache( [ 'cache' => $localBag ] ); |
| 839 |
|
| 840 |
|
| 841 |
$keyedIds = new ArrayIterator( [ 'k1' => 'id1', 'k2' => 'id2' ] ); |
| 842 |
$this->assertSame( |
| 843 |
[ 'k1' => 'val-id1', 'k2' => 'val-id2' ], |
| 844 |
$wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] ) |
| 845 |
); |
| 846 |
|
| 847 |
$this->assertSame( |
| 848 |
[ 'k1' => 'val-id1', 'k2' => 'val-id2' ], |
| 849 |
$wanCache->getMultiWithSetCallback( $keyedIds, 10, $genFunc, [ 'pcTTL' => 5 ] ) |
| 850 |
); |
| 851 |
} |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
public function testGetMultiWithSetCallbackRefresh( $expiring, $popular, array $idsByKey ) { |
| 860 |
$deferredCbs = []; |
| 861 |
$bag = new HashBagOStuff(); |
| 862 |
$cache = $this->getMockBuilder( WANObjectCache::class ) |
| 863 |
->onlyMethods( [ 'worthRefreshExpiring', 'worthRefreshPopular' ] ) |
| 864 |
->setConstructorArgs( [ |
| 865 |
[ |
| 866 |
'cache' => $bag, |
| 867 |
'asyncHandler' => static function ( $callback ) use ( &$deferredCbs ) { |
| 868 |
$deferredCbs[] = $callback; |
| 869 |
} |
| 870 |
] |
| 871 |
] ) |
| 872 |
->getMock(); |
| 873 |
|
| 874 |
$cache->method( 'worthRefreshExpiring' )->willReturn( $expiring ); |
| 875 |
$cache->method( 'worthRefreshPopular' )->willReturn( $popular ); |
| 876 |
|
| 877 |
$wasSet = 0; |
| 878 |
$keyedIds = new ArrayIterator( $idsByKey ); |
| 879 |
$genFunc = static function ( $id, $old, &$ttl, &$opts, $asOf ) use ( &$wasSet ) { |
| 880 |
++$wasSet; |
| 881 |
$ttl = 20; |
| 882 |
return "@$id$"; |
| 883 |
}; |
| 884 |
|
| 885 |
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc ); |
| 886 |
$this->assertSame( count( $idsByKey ), $wasSet, "Initial sets" ); |
| 887 |
$this->assertSame( [], $deferredCbs, "No deferred callbacks yet" ); |
| 888 |
foreach ( $idsByKey as $key => $id ) { |
| 889 |
$this->assertSame( "@$id$", $v[$key], "Initial cache value generation" ); |
| 890 |
} |
| 891 |
|
| 892 |
$wasSet = 0; |
| 893 |
$preemptiveRefresh = ( $expiring || $popular ); |
| 894 |
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc ); |
| 895 |
$this->assertSame( 0, $wasSet, "No values generated" ); |
| 896 |
$this->assertCount( |
| 897 |
$preemptiveRefresh ? count( $idsByKey ) : 0, |
| 898 |
$deferredCbs, |
| 899 |
"Deferred callbacks queued" |
| 900 |
); |
| 901 |
foreach ( $idsByKey as $key => $id ) { |
| 902 |
$this->assertSame( "@$id$", $v[$key], "Cached value reused; refresh scheduled" ); |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
$deferredCbsReady = $deferredCbs; |
| 907 |
$deferredCbs = []; |
| 908 |
foreach ( $deferredCbsReady as $deferredCb ) { |
| 909 |
$deferredCb(); |
| 910 |
} |
| 911 |
|
| 912 |
$this->assertSame( |
| 913 |
( $preemptiveRefresh ? count( $idsByKey ) : 0 ), |
| 914 |
$wasSet, |
| 915 |
"Deferred callback regenerations" |
| 916 |
); |
| 917 |
$this->assertSame( [], $deferredCbs, "Deferred callbacks queue empty" ); |
| 918 |
|
| 919 |
$wasSet = 0; |
| 920 |
$v = $cache->getMultiWithSetCallback( $keyedIds, 30, $genFunc ); |
| 921 |
$this->assertSame( |
| 922 |
0, |
| 923 |
$wasSet, |
| 924 |
"Deferred callbacks did not run again" |
| 925 |
); |
| 926 |
foreach ( $idsByKey as $key => $id ) { |
| 927 |
$this->assertSame( "@$id$", $v[$key], "Cached value OK after deferred refresh run" ); |
| 928 |
} |
| 929 |
} |
| 930 |
|
| 931 |
public static function getMultiWithSetCallbackRefresh_provider() { |
| 932 |
return [ |
| 933 |
[ true, true, [ 'a' => 1, 'b' => 2, 'c' => 3, 'd' => 4 ] ], |
| 934 |
[ true, false, [ 'a' => 'x', 'b' => 'y', 'c' => 'z', 'd' => 'w' ] ], |
| 935 |
[ false, true, [ 'a' => 'p', 'b' => 'q', 'c' => 'r', 'd' => 's' ] ], |
| 936 |
[ false, false, [ 'a' => '%', 'b' => '^', 'c' => '&', 'd' => 'ç' ] ] |
| 937 |
]; |
| 938 |
} |
| 939 |
|
| 940 |
public static function getMultiWithUnionSetCallback_provider() { |
| 941 |
yield 'default' => [ [] ]; |
| 942 |
yield 'versioned' => [ [ 'version' => 1 ] ]; |
| 943 |
} |
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
public function testGetMultiWithUnionSetCallback( array $extOpts ) { |
| 950 |
[ $cache ] = $this->newWanCache(); |
| 951 |
|
| 952 |
$wasSet = 0; |
| 953 |
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use ( |
| 954 |
&$wasSet |
| 955 |
) { |
| 956 |
$wasSet++; |
| 957 |
$newValues = []; |
| 958 |
foreach ( $ids as $id ) { |
| 959 |
$newValues[$id] = "@$id$"; |
| 960 |
|
| 961 |
$ttls[$id] = 20; |
| 962 |
} |
| 963 |
|
| 964 |
return $newValues; |
| 965 |
}; |
| 966 |
|
| 967 |
$mockWallClock = 1549343530.0; |
| 968 |
$t0 = $mockWallClock; |
| 969 |
$cache->setMockTime( $mockWallClock ); |
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
$wasSet = 0; |
| 974 |
$keyedIds = new ArrayIterator( [ 'keyA' => 'apple' ] ); |
| 975 |
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc, $extOpts ); |
| 976 |
$this->assertSame( '@apple$', $v['keyA'], 'Value returned' ); |
| 977 |
$this->assertSame( 1, $wasSet, 'Value regenerated' ); |
| 978 |
$curTTL = null; |
| 979 |
$cache->get( 'keyA', $curTTL ); |
| 980 |
$this->assertLessThanOrEqual( 20, $curTTL, 'Custom TTL between 19-20' ); |
| 981 |
$this->assertGreaterThanOrEqual( 19, $curTTL, 'Custom TTL between 19-20' ); |
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
$wasSet = 0; |
| 986 |
$v = $cache->getMultiWithUnionSetCallback( |
| 987 |
new ArrayIterator( [ 'keyB' => 'bat' ] ), |
| 988 |
30, |
| 989 |
$genFunc, |
| 990 |
[ 'lowTTL' => 0 ] + $extOpts |
| 991 |
); |
| 992 |
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' ); |
| 993 |
$this->assertSame( 1, $wasSet, 'Value regenerated' ); |
| 994 |
$this->assertSame( 0, $cache->getWarmupKeyMisses(), 'Warmup batch covered all fetches' ); |
| 995 |
$wasSet = 0; |
| 996 |
$v = $cache->getMultiWithUnionSetCallback( |
| 997 |
new ArrayIterator( [ 'keyB' => 'bat' ] ), |
| 998 |
30, |
| 999 |
$genFunc, |
| 1000 |
[ 'lowTTL' => 0 ] + $extOpts |
| 1001 |
); |
| 1002 |
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' ); |
| 1003 |
$this->assertSame( 0, $wasSet, 'Value not regenerated' ); |
| 1004 |
$this->assertSame( 0, $cache->getWarmupKeyMisses(), 'Warmup batch covered all fetches' ); |
| 1005 |
|
| 1006 |
$mockWallClock += 1; |
| 1007 |
$t1 = $mockWallClock; |
| 1008 |
|
| 1009 |
|
| 1010 |
$wasSet = 0; |
| 1011 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1012 |
new ArrayIterator( [ 'keyB' => 'bat' ] ), |
| 1013 |
30, |
| 1014 |
$genFunc, |
| 1015 |
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts |
| 1016 |
); |
| 1017 |
$this->assertSame( '@bat$', $v['keyB'], 'Value returned' ); |
| 1018 |
$this->assertSame( 1, $wasSet, 'Value regenerated due to check keys' ); |
| 1019 |
$time = $cache->getCheckKeyTime( 'check1' ); |
| 1020 |
$this->assertGreaterThanOrEqual( $t1, $time, 'Check key 1 was autocreated' ); |
| 1021 |
$time = $cache->getCheckKeyTime( 'check2' ); |
| 1022 |
$this->assertGreaterThanOrEqual( $t1, $time, 'Check key 2 was autocreated' ); |
| 1023 |
|
| 1024 |
$mockWallClock += 1; |
| 1025 |
$t2 = $mockWallClock; |
| 1026 |
|
| 1027 |
|
| 1028 |
$wasSet = 0; |
| 1029 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1030 |
new ArrayIterator( [ 'keyC' => 'cat' ] ), |
| 1031 |
30, |
| 1032 |
$genFunc, |
| 1033 |
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts |
| 1034 |
); |
| 1035 |
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' ); |
| 1036 |
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' ); |
| 1037 |
$time = $cache->getCheckKeyTime( 'check1' ); |
| 1038 |
$this->assertLessThanOrEqual( $t1, $time, 'Check key 1 did not change' ); |
| 1039 |
$time = $cache->getCheckKeyTime( 'check2' ); |
| 1040 |
$this->assertLessThanOrEqual( $t1, $time, 'Check key 2 did not change' ); |
| 1041 |
$curTTL = null; |
| 1042 |
$v = $cache->get( 'keyC', $curTTL, [ 'check1', 'check2' ] ); |
| 1043 |
$this->assertSame( '@cat$', $v, 'Value returned' ); |
| 1044 |
$this->assertGreaterThan( 0, $curTTL, 'No hold-off for new check key (T344191)' ); |
| 1045 |
|
| 1046 |
|
| 1047 |
$mockWallClock += 1; |
| 1048 |
$cache->touchCheckKey( 'check1' ); |
| 1049 |
$mockWallClock += 1; |
| 1050 |
$wasSet = 0; |
| 1051 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1052 |
new ArrayIterator( [ 'keyC' => 'cat' ] ), |
| 1053 |
30, |
| 1054 |
$genFunc, |
| 1055 |
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts |
| 1056 |
); |
| 1057 |
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' ); |
| 1058 |
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' ); |
| 1059 |
$curTTL = null; |
| 1060 |
$v = $cache->get( 'keyC', $curTTL, [ 'check1', 'check2' ] ); |
| 1061 |
$this->assertSame( '@cat$', $v, 'Value returned' ); |
| 1062 |
$this->assertLessThanOrEqual( 0, $curTTL, 'Value is expired during hold-off from new check key' ); |
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
$wasSet = 0; |
| 1068 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1069 |
new ArrayIterator( [ 'keyC' => 'cat' ] ), |
| 1070 |
30, |
| 1071 |
$genFunc, |
| 1072 |
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts |
| 1073 |
); |
| 1074 |
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' ); |
| 1075 |
$this->assertSame( 0, $wasSet, 'Value not regenerated within a second' ); |
| 1076 |
$mockWallClock += 1; |
| 1077 |
$wasSet = 0; |
| 1078 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1079 |
new ArrayIterator( [ 'keyC' => 'cat' ] ), |
| 1080 |
30, |
| 1081 |
$genFunc, |
| 1082 |
[ 'checkKeys' => [ 'check1', 'check2' ] ] + $extOpts |
| 1083 |
); |
| 1084 |
$this->assertSame( '@cat$', $v['keyC'], 'Value returned' ); |
| 1085 |
$this->assertSame( 1, $wasSet, 'Value regenerated due to check key hold-off' ); |
| 1086 |
|
| 1087 |
|
| 1088 |
$wasSet = 0; |
| 1089 |
$keyedIds = new ArrayIterator( [ 'keyD' => 'derk' ] ); |
| 1090 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1091 |
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); |
| 1092 |
$this->assertSame( '@derk$', $v['keyD'], 'Value returned' ); |
| 1093 |
$this->assertSame( 1, $wasSet, 'Value regenerated due to cache miss' ); |
| 1094 |
|
| 1095 |
$cache->delete( 'keyD' ); |
| 1096 |
$wasSet = 0; |
| 1097 |
$v = $cache->getMultiWithUnionSetCallback( |
| 1098 |
$keyedIds, 30, $genFunc, [ 'pcTTL' => 5 ] + $extOpts ); |
| 1099 |
$this->assertSame( '@derk$', $v['keyD'], 'Value returned from process cache' ); |
| 1100 |
$this->assertSame( 0, $wasSet, 'Value not regenerated' ); |
| 1101 |
|
| 1102 |
$ids = [ 2, 6, 4, 7 ]; |
| 1103 |
$keyedIds = $cache->makeMultiKeys( $ids, static function ( $id, WANObjectCache $cache ) { |
| 1104 |
return $cache->makeKey( 'test', $id ); |
| 1105 |
} ); |
| 1106 |
$wasSet = 0; |
| 1107 |
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use ( &$wasSet ) { |
| 1108 |
$newValues = []; |
| 1109 |
foreach ( $ids as $id ) { |
| 1110 |
$wasSet++; |
| 1111 |
$newValues[$id] = ( $id <= 6 ) ? "val-{$id}" : false; |
| 1112 |
} |
| 1113 |
return $newValues; |
| 1114 |
}; |
| 1115 |
|
| 1116 |
$values = $cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc ); |
| 1117 |
$this->assertSame( [ 'val-2', 'val-6', 'val-4', false ], array_values( $values ), 'Values in order' ); |
| 1118 |
$this->assertSame( |
| 1119 |
array_keys( iterator_to_array( $keyedIds ) ), |
| 1120 |
array_keys( $values ), |
| 1121 |
'Correct keys in correct order' |
| 1122 |
); |
| 1123 |
$this->assertSame( 4, $wasSet, 'Values generated' ); |
| 1124 |
|
| 1125 |
$wasSet = 0; |
| 1126 |
$cache->getMultiWithUnionSetCallback( $keyedIds, 10, $genFunc ); |
| 1127 |
$this->assertSame( [ 'val-2', 'val-6', 'val-4', false ], array_values( $values ), 'Values in order' ); |
| 1128 |
$this->assertSame( 1, $wasSet, 'Values not regenerated, except for the missing item 7' ); |
| 1129 |
} |
| 1130 |
|
| 1131 |
public static function provideCoalesceAndMcrouterSettings() { |
| 1132 |
return [ |
| 1133 |
[ [ 'coalesceScheme' => 'hash_tag' ], '{' ], |
| 1134 |
[ [ 'broadcastRoutingPrefix' => '/*/test/', 'coalesceScheme' => 'hash_stop' ], '|#|' ], |
| 1135 |
]; |
| 1136 |
} |
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
public function testGetMultiWithUnionSetCallbackRefresh( $expiring, $popular, array $idsByKey ) { |
| 1145 |
$deferredCbs = []; |
| 1146 |
$bag = new HashBagOStuff(); |
| 1147 |
$cache = $this->getMockBuilder( WANObjectCache::class ) |
| 1148 |
->onlyMethods( [ 'worthRefreshExpiring', 'worthRefreshPopular' ] ) |
| 1149 |
->setConstructorArgs( [ |
| 1150 |
[ |
| 1151 |
'cache' => $bag, |
| 1152 |
'asyncHandler' => static function ( $callback ) use ( &$deferredCbs ) { |
| 1153 |
$deferredCbs[] = $callback; |
| 1154 |
} |
| 1155 |
] |
| 1156 |
] ) |
| 1157 |
->getMock(); |
| 1158 |
|
| 1159 |
$cache->method( 'worthRefreshExpiring' )->willReturn( $expiring ); |
| 1160 |
$cache->method( 'worthRefreshPopular' )->willReturn( $popular ); |
| 1161 |
|
| 1162 |
$wasSet = 0; |
| 1163 |
$keyedIds = new ArrayIterator( $idsByKey ); |
| 1164 |
$genFunc = static function ( array $ids, array &$ttls, array &$setOpts ) use ( &$wasSet ) { |
| 1165 |
$newValues = []; |
| 1166 |
foreach ( $ids as $id ) { |
| 1167 |
++$wasSet; |
| 1168 |
$newValues[$id] = "@$id$"; |
| 1169 |
$ttls[$id] = 20; |
| 1170 |
} |
| 1171 |
|
| 1172 |
return $newValues; |
| 1173 |
}; |
| 1174 |
|
| 1175 |
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc ); |
| 1176 |
$this->assertSame( count( $idsByKey ), $wasSet, "Initial sets" ); |
| 1177 |
$this->assertSame( [], $deferredCbs, "No deferred callbacks yet" ); |
| 1178 |
foreach ( $idsByKey as $key => $id ) { |
| 1179 |
$this->assertSame( "@$id$", $v[$key], "Initial cache value generation" ); |
| 1180 |
} |
| 1181 |
|
| 1182 |
$preemptiveRefresh = ( $expiring || $popular ); |
| 1183 |
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc ); |
| 1184 |
$this->assertSame( count( $idsByKey ), $wasSet, "Deferred callbacks did not run yet" ); |
| 1185 |
$this->assertCount( |
| 1186 |
$preemptiveRefresh ? count( $idsByKey ) : 0, |
| 1187 |
$deferredCbs, |
| 1188 |
"Deferred callbacks queued" |
| 1189 |
); |
| 1190 |
foreach ( $idsByKey as $key => $id ) { |
| 1191 |
$this->assertSame( "@$id$", $v[$key], "Cached value reused; refresh scheduled" ); |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
$deferredCbsReady = $deferredCbs; |
| 1196 |
$deferredCbs = []; |
| 1197 |
foreach ( $deferredCbsReady as $deferredCb ) { |
| 1198 |
$deferredCb(); |
| 1199 |
} |
| 1200 |
|
| 1201 |
$this->assertSame( |
| 1202 |
count( $idsByKey ) * ( $preemptiveRefresh ? 2 : 1 ), |
| 1203 |
$wasSet, |
| 1204 |
"Deferred callback regenerations" |
| 1205 |
); |
| 1206 |
$this->assertSame( [], $deferredCbs, "Deferred callbacks queue empty" ); |
| 1207 |
|
| 1208 |
$v = $cache->getMultiWithUnionSetCallback( $keyedIds, 30, $genFunc ); |
| 1209 |
$this->assertSame( |
| 1210 |
count( $idsByKey ) * ( $preemptiveRefresh ? 2 : 1 ), |
| 1211 |
$wasSet, |
| 1212 |
"Deferred callbacks did not run again yet" |
| 1213 |
); |
| 1214 |
foreach ( $idsByKey as $key => $id ) { |
| 1215 |
$this->assertSame( "@$id$", $v[$key], "Cached value OK after deferred refresh run" ); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
public function testLockTSE( array $params ) { |
| 1223 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 1224 |
$key = wfRandomString(); |
| 1225 |
$value = wfRandomString(); |
| 1226 |
|
| 1227 |
$mockWallClock = 1549343530.0; |
| 1228 |
$cache->setMockTime( $mockWallClock ); |
| 1229 |
|
| 1230 |
$calls = 0; |
| 1231 |
$func = static function () use ( &$calls, $value ) { |
| 1232 |
++$calls; |
| 1233 |
return $value; |
| 1234 |
}; |
| 1235 |
|
| 1236 |
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'lockTSE' => 5 ] ); |
| 1237 |
$this->assertSame( $value, $ret ); |
| 1238 |
$this->assertSame( 1, $calls, 'Value was populated' ); |
| 1239 |
|
| 1240 |
|
| 1241 |
$this->setMutexKey( $bag, $key ); |
| 1242 |
|
| 1243 |
$checkKeys = [ wfRandomString() ]; |
| 1244 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1245 |
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] ); |
| 1246 |
$this->assertSame( $value, $ret, 'Old value used' ); |
| 1247 |
$this->assertSame( 1, $calls, 'Callback was not used' ); |
| 1248 |
|
| 1249 |
$cache->delete( $key ); |
| 1250 |
|
| 1251 |
$mockWallClock += 0.001; |
| 1252 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1253 |
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] ); |
| 1254 |
$this->assertSame( $value, $ret, 'Callback was used; interim saved' ); |
| 1255 |
$this->assertSame( 2, $calls, 'Callback was used; interim saved' ); |
| 1256 |
|
| 1257 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1258 |
[ 'lockTSE' => 5, 'checkKeys' => $checkKeys ] ); |
| 1259 |
$this->assertSame( $value, $ret, 'Callback was not used; used interim (mutex failed)' ); |
| 1260 |
$this->assertSame( 2, $calls, 'Callback was not used; used interim (mutex failed)' ); |
| 1261 |
} |
| 1262 |
|
| 1263 |
private function setMutexKey( BagOStuff $bag, $key ) { |
| 1264 |
|
| 1265 |
$bag->add( "WANCache:$key|#|m", 1 ); |
| 1266 |
$bag->add( "WANCache:{" . $key . "}:m", 1 ); |
| 1267 |
} |
| 1268 |
|
| 1269 |
private function clearMutexKey( BagOStuff $bag, $key ) { |
| 1270 |
|
| 1271 |
$bag->delete( "WANCache:$key|#|m" ); |
| 1272 |
$bag->delete( "WANCache:{" . $key . "}:m" ); |
| 1273 |
} |
| 1274 |
|
| 1275 |
private function setCheckKey( BagOStuff $bag, $key, $time ) { |
| 1276 |
|
| 1277 |
$bag->set( "WANCache:$key|#|t", "PURGED:$time" ); |
| 1278 |
$bag->set( "WANCache:{" . $key . "}:t", "PURGED:$time" ); |
| 1279 |
} |
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
public function testLockTSESlow( array $params ) { |
| 1285 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 1286 |
$key = 'myfirstkey'; |
| 1287 |
$key2 = 'mysecondkey'; |
| 1288 |
$value = 'some_slow_value'; |
| 1289 |
|
| 1290 |
$mockWallClock = 1549343530.0; |
| 1291 |
$cache->setMockTime( $mockWallClock ); |
| 1292 |
|
| 1293 |
$calls = 0; |
| 1294 |
$lastCallOldValue = null; |
| 1295 |
$func = static function ( $oldValue, &$ttl, &$setOpts ) use ( |
| 1296 |
&$calls, $value, &$mockWallClock, &$lastCallOldValue |
| 1297 |
) { |
| 1298 |
++$calls; |
| 1299 |
$lastCallOldValue = $oldValue; |
| 1300 |
|
| 1301 |
$setOpts['since'] = $mockWallClock; |
| 1302 |
$mockWallClock += 10; |
| 1303 |
return $value; |
| 1304 |
}; |
| 1305 |
|
| 1306 |
$curTTL = null; |
| 1307 |
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); |
| 1308 |
$this->assertSame( $value, $ret ); |
| 1309 |
$this->assertSame( $value, $cache->get( $key, $curTTL ), 'Value populated' ); |
| 1310 |
$this->assertEqualsWithDelta( 30.0, $curTTL, 0.01, 'Value has reduced logical TTL' ); |
| 1311 |
$this->assertSame( 1, $calls, 'Value was generated' ); |
| 1312 |
$this->assertSame( false, $lastCallOldValue, 'No old value for callback' ); |
| 1313 |
|
| 1314 |
|
| 1315 |
$mockWallClock += 32; |
| 1316 |
|
| 1317 |
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); |
| 1318 |
$this->assertSame( $value, $ret ); |
| 1319 |
$this->assertSame( 2, $calls, 'Callback used (stale, mutex acquired, regenerated)' ); |
| 1320 |
$this->assertSame( $value, $lastCallOldValue, 'Old value for callback' ); |
| 1321 |
|
| 1322 |
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5, 'lowTTL' => -1 ] ); |
| 1323 |
$this->assertSame( $value, $ret ); |
| 1324 |
$this->assertSame( 2, $calls, 'Callback not used (extremely new value reused)' ); |
| 1325 |
|
| 1326 |
|
| 1327 |
$mockWallClock += 32; |
| 1328 |
|
| 1329 |
$this->setMutexKey( $bag, $key ); |
| 1330 |
|
| 1331 |
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); |
| 1332 |
$this->assertSame( $value, $ret ); |
| 1333 |
$this->assertSame( 2, $calls, 'Callback not used (mutex not acquired, stale value used)' ); |
| 1334 |
|
| 1335 |
$mockWallClock += 301; |
| 1336 |
|
| 1337 |
$this->setMutexKey( $bag, $key ); |
| 1338 |
|
| 1339 |
$ret = $cache->getWithSetCallback( $key, 300, $func, [ 'lockTSE' => 5 ] ); |
| 1340 |
$this->assertSame( $value, $ret ); |
| 1341 |
$this->assertSame( 3, $calls, 'Callback was used (mutex not acquired, not in cache)' ); |
| 1342 |
|
| 1343 |
$calls = 0; |
| 1344 |
$func2 = static function ( $oldValue, &$ttl, &$setOpts ) use ( &$calls, $value ) { |
| 1345 |
++$calls; |
| 1346 |
$setOpts['lag'] = 15; |
| 1347 |
return $value; |
| 1348 |
}; |
| 1349 |
|
| 1350 |
|
| 1351 |
$curTTL = null; |
| 1352 |
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] ); |
| 1353 |
$this->assertSame( $value, $ret ); |
| 1354 |
$this->assertSame( $value, $cache->get( $key2, $curTTL ), 'Value was populated' ); |
| 1355 |
$this->assertSame( 30.0, $curTTL, 'Value has reduced logical TTL', 0.01 ); |
| 1356 |
$this->assertSame( 1, $calls, 'Value was generated' ); |
| 1357 |
|
| 1358 |
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] ); |
| 1359 |
$this->assertSame( $value, $ret ); |
| 1360 |
$this->assertSame( 1, $calls, 'Callback was used (not expired)' ); |
| 1361 |
|
| 1362 |
$mockWallClock += 31; |
| 1363 |
|
| 1364 |
$ret = $cache->getWithSetCallback( $key2, 300, $func2, [ 'lockTSE' => 5 ] ); |
| 1365 |
$this->assertSame( $value, $ret ); |
| 1366 |
$this->assertSame( 2, $calls, 'Callback was used (mutex acquired)' ); |
| 1367 |
} |
| 1368 |
|
| 1369 |
|
| 1370 |
|
| 1371 |
|
| 1372 |
public function testBusyValueBasic( array $params ) { |
| 1373 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 1374 |
$key = wfRandomString(); |
| 1375 |
$value = wfRandomString(); |
| 1376 |
$busyValue = wfRandomString(); |
| 1377 |
|
| 1378 |
$mockWallClock = 1549343530.0; |
| 1379 |
$cache->setMockTime( $mockWallClock ); |
| 1380 |
|
| 1381 |
$calls = 0; |
| 1382 |
$func = static function () use ( &$calls, $value ) { |
| 1383 |
++$calls; |
| 1384 |
return $value; |
| 1385 |
}; |
| 1386 |
|
| 1387 |
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] ); |
| 1388 |
$this->assertSame( $value, $ret ); |
| 1389 |
$this->assertSame( 1, $calls, 'Value was populated' ); |
| 1390 |
|
| 1391 |
$mockWallClock += 0.2; |
| 1392 |
|
| 1393 |
|
| 1394 |
$this->setMutexKey( $bag, $key ); |
| 1395 |
|
| 1396 |
$checkKeys = [ wfRandomString() ]; |
| 1397 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1398 |
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] ); |
| 1399 |
$this->assertSame( $value, $ret, 'Callback used' ); |
| 1400 |
$this->assertSame( 2, $calls, 'Callback used' ); |
| 1401 |
|
| 1402 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1403 |
[ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] ); |
| 1404 |
$this->assertSame( $value, $ret, 'Old value used' ); |
| 1405 |
$this->assertSame( 2, $calls, 'Callback was not used' ); |
| 1406 |
|
| 1407 |
$cache->delete( $key ); |
| 1408 |
|
| 1409 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1410 |
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] ); |
| 1411 |
$this->assertSame( $busyValue, $ret, 'Callback was not used; used busy value' ); |
| 1412 |
$this->assertSame( 2, $calls, 'Callback was not used; used busy value' ); |
| 1413 |
|
| 1414 |
$this->clearMutexKey( $bag, $key ); |
| 1415 |
$mockWallClock += 0.001; |
| 1416 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1417 |
[ 'lockTSE' => 30, 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] ); |
| 1418 |
$this->assertSame( $value, $ret, 'Callback was used; saved interim' ); |
| 1419 |
$this->assertSame( 3, $calls, 'Callback was used; saved interim' ); |
| 1420 |
|
| 1421 |
$this->setMutexKey( $bag, $key ); |
| 1422 |
$ret = $cache->getWithSetCallback( $key, 30, $func, |
| 1423 |
[ 'busyValue' => $busyValue, 'checkKeys' => $checkKeys ] ); |
| 1424 |
$this->assertSame( $value, $ret, 'Callback was not used; used interim' ); |
| 1425 |
$this->assertSame( 3, $calls, 'Callback was not used; used interim' ); |
| 1426 |
} |
| 1427 |
|
| 1428 |
public static function getBusyValues_Provider() { |
| 1429 |
$hash = new HashBagOStuff( [] ); |
| 1430 |
|
| 1431 |
return [ |
| 1432 |
[ |
| 1433 |
static function () { |
| 1434 |
return "Saint Oliver Plunckett"; |
| 1435 |
}, |
| 1436 |
'Saint Oliver Plunckett' |
| 1437 |
], |
| 1438 |
[ 'strlen', 'strlen' ], |
| 1439 |
[ 'WANObjectCache::newEmpty', 'WANObjectCache::newEmpty' ], |
| 1440 |
[ [ 'WANObjectCache', 'newEmpty' ], [ 'WANObjectCache', 'newEmpty' ] ], |
| 1441 |
[ [ $hash, 'getLastError' ], [ $hash, 'getLastError' ] ], |
| 1442 |
[ [ 1, 2, 3 ], [ 1, 2, 3 ] ] |
| 1443 |
]; |
| 1444 |
} |
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
|
| 1449 |
public function testBusyValueTypes( $busyValue, $expected ) { |
| 1450 |
[ $cache, $bag ] = $this->newWanCache(); |
| 1451 |
$key = wfRandomString(); |
| 1452 |
|
| 1453 |
$mockWallClock = 1549343530.0; |
| 1454 |
$cache->setMockTime( $mockWallClock ); |
| 1455 |
|
| 1456 |
$calls = 0; |
| 1457 |
$func = static function () use ( &$calls ) { |
| 1458 |
++$calls; |
| 1459 |
return 418; |
| 1460 |
}; |
| 1461 |
|
| 1462 |
|
| 1463 |
$this->setMutexKey( $bag, $key ); |
| 1464 |
|
| 1465 |
$ret = $cache->getWithSetCallback( $key, 30, $func, [ 'busyValue' => $busyValue ] ); |
| 1466 |
$this->assertSame( $expected, $ret, 'busyValue used as expected' ); |
| 1467 |
$this->assertSame( 0, $calls, 'busyValue was used' ); |
| 1468 |
} |
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
public function testGetMulti() { |
| 1473 |
[ $cache ] = $this->newWanCache(); |
| 1474 |
|
| 1475 |
$value1 = [ 'this' => 'is', 'a' => 'test' ]; |
| 1476 |
$value2 = [ 'this' => 'is', 'another' => 'test' ]; |
| 1477 |
|
| 1478 |
$key1 = wfRandomString(); |
| 1479 |
$key2 = wfRandomString(); |
| 1480 |
$key3 = wfRandomString(); |
| 1481 |
|
| 1482 |
$mockWallClock = 1549343530.0; |
| 1483 |
$priorTime = $mockWallClock; |
| 1484 |
$cache->setMockTime( $mockWallClock ); |
| 1485 |
|
| 1486 |
$cache->set( $key1, $value1, 5 ); |
| 1487 |
$cache->set( $key2, $value2, 10 ); |
| 1488 |
|
| 1489 |
$curTTLs = []; |
| 1490 |
$this->assertSame( |
| 1491 |
[ $key1 => $value1, $key2 => $value2 ], |
| 1492 |
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs ), |
| 1493 |
'Result array populated' |
| 1494 |
); |
| 1495 |
|
| 1496 |
$this->assertCount( 2, $curTTLs, "Two current TTLs in array" ); |
| 1497 |
$this->assertGreaterThan( 0, $curTTLs[$key1], "Key 1 has current TTL > 0" ); |
| 1498 |
$this->assertGreaterThan( 0, $curTTLs[$key2], "Key 2 has current TTL > 0" ); |
| 1499 |
|
| 1500 |
$cKey1 = wfRandomString(); |
| 1501 |
$cKey2 = wfRandomString(); |
| 1502 |
|
| 1503 |
$mockWallClock += 1; |
| 1504 |
|
| 1505 |
$cache->touchCheckKey( $cKey1 ); |
| 1506 |
$cache->touchCheckKey( $cKey2 ); |
| 1507 |
$t1 = $cache->getCheckKeyTime( $cKey1 ); |
| 1508 |
$this->assertSame( $mockWallClock, $t1, 'Check key 1 generated' ); |
| 1509 |
$t2 = $cache->getCheckKeyTime( $cKey2 ); |
| 1510 |
$this->assertSame( $mockWallClock, $t2, 'Check key 2 generated' ); |
| 1511 |
|
| 1512 |
$curTTLs = []; |
| 1513 |
$this->assertSame( |
| 1514 |
[ $key1 => $value1, $key2 => $value2 ], |
| 1515 |
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ), |
| 1516 |
"Result array populated even with new check keys" |
| 1517 |
); |
| 1518 |
$this->assertCount( 2, $curTTLs, "Current TTLs array set" ); |
| 1519 |
$this->assertLessThanOrEqual( 0, $curTTLs[$key1], 'Key 1 has current TTL <= 0' ); |
| 1520 |
$this->assertLessThanOrEqual( 0, $curTTLs[$key2], 'Key 2 has current TTL <= 0' ); |
| 1521 |
|
| 1522 |
$mockWallClock += 1; |
| 1523 |
|
| 1524 |
$curTTLs = []; |
| 1525 |
$this->assertSame( |
| 1526 |
[ $key1 => $value1, $key2 => $value2 ], |
| 1527 |
$cache->getMulti( [ $key1, $key2, $key3 ], $curTTLs, [ $cKey1, $cKey2 ] ), |
| 1528 |
"Result array still populated even with new check keys" |
| 1529 |
); |
| 1530 |
$this->assertCount( 2, $curTTLs, "Current TTLs still array set" ); |
| 1531 |
$this->assertLessThan( 0, $curTTLs[$key1], 'Key 1 has negative current TTL' ); |
| 1532 |
$this->assertLessThan( 0, $curTTLs[$key2], 'Key 2 has negative current TTL' ); |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
public function testGetMultiCheckKeys( array $params ) { |
| 1540 |
[ $cache ] = $this->newWanCache( $params ); |
| 1541 |
|
| 1542 |
$checkAll = wfRandomString(); |
| 1543 |
$check1 = wfRandomString(); |
| 1544 |
$check2 = wfRandomString(); |
| 1545 |
$check3 = wfRandomString(); |
| 1546 |
$value1 = wfRandomString(); |
| 1547 |
$value2 = wfRandomString(); |
| 1548 |
|
| 1549 |
$mockWallClock = 1549343530.0; |
| 1550 |
$cache->setMockTime( $mockWallClock ); |
| 1551 |
|
| 1552 |
|
| 1553 |
|
| 1554 |
foreach ( [ $checkAll, $check1, $check2 ] as $checkKey ) { |
| 1555 |
$cache->touchCheckKey( $checkKey, WANObjectCache::HOLDOFF_TTL_NONE ); |
| 1556 |
} |
| 1557 |
|
| 1558 |
$mockWallClock += 0.100; |
| 1559 |
|
| 1560 |
$cache->set( 'key1', $value1, 10 ); |
| 1561 |
$cache->set( 'key2', $value2, 10 ); |
| 1562 |
|
| 1563 |
$curTTLs = []; |
| 1564 |
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [ |
| 1565 |
'key1' => $check1, |
| 1566 |
$checkAll, |
| 1567 |
'key2' => $check2, |
| 1568 |
'key3' => $check3, |
| 1569 |
] ); |
| 1570 |
$this->assertSame( |
| 1571 |
[ 'key1' => $value1, 'key2' => $value2 ], |
| 1572 |
$result, |
| 1573 |
'Initial values' |
| 1574 |
); |
| 1575 |
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key1'], 'Initial ttls' ); |
| 1576 |
$this->assertLessThanOrEqual( 10.5, $curTTLs['key1'], 'Initial ttls' ); |
| 1577 |
$this->assertGreaterThanOrEqual( 9.5, $curTTLs['key2'], 'Initial ttls' ); |
| 1578 |
$this->assertLessThanOrEqual( 10.5, $curTTLs['key2'], 'Initial ttls' ); |
| 1579 |
|
| 1580 |
$mockWallClock += 0.100; |
| 1581 |
$cache->touchCheckKey( $check1 ); |
| 1582 |
|
| 1583 |
$curTTLs = []; |
| 1584 |
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [ |
| 1585 |
'key1' => $check1, |
| 1586 |
$checkAll, |
| 1587 |
'key2' => $check2, |
| 1588 |
'key3' => $check3, |
| 1589 |
] ); |
| 1590 |
$this->assertSame( |
| 1591 |
[ 'key1' => $value1, 'key2' => $value2 ], |
| 1592 |
$result, |
| 1593 |
'key1 expired by check1, but value still provided' |
| 1594 |
); |
| 1595 |
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 TTL expired' ); |
| 1596 |
$this->assertGreaterThan( 0, $curTTLs['key2'], 'key2 still valid' ); |
| 1597 |
|
| 1598 |
$cache->touchCheckKey( $checkAll ); |
| 1599 |
|
| 1600 |
$curTTLs = []; |
| 1601 |
$result = $cache->getMulti( [ 'key1', 'key2', 'key3' ], $curTTLs, [ |
| 1602 |
'key1' => $check1, |
| 1603 |
$checkAll, |
| 1604 |
'key2' => $check2, |
| 1605 |
'key3' => $check3, |
| 1606 |
] ); |
| 1607 |
$this->assertSame( |
| 1608 |
[ 'key1' => $value1, 'key2' => $value2 ], |
| 1609 |
$result, |
| 1610 |
'All keys expired by checkAll, but value still provided' |
| 1611 |
); |
| 1612 |
$this->assertLessThan( 0, $curTTLs['key1'], 'key1 expired by checkAll' ); |
| 1613 |
$this->assertLessThan( 0, $curTTLs['key2'], 'key2 expired by checkAll' ); |
| 1614 |
} |
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
public function testCheckKeyHoldoff() { |
| 1619 |
[ $cache ] = $this->newWanCache(); |
| 1620 |
$key = wfRandomString(); |
| 1621 |
$checkKey = wfRandomString(); |
| 1622 |
|
| 1623 |
$mockWallClock = 1549343530.0; |
| 1624 |
$cache->setMockTime( $mockWallClock ); |
| 1625 |
$cache->touchCheckKey( $checkKey, 8 ); |
| 1626 |
|
| 1627 |
$mockWallClock += 1; |
| 1628 |
$cache->set( $key, 1, 60 ); |
| 1629 |
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); |
| 1630 |
$this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" ); |
| 1631 |
|
| 1632 |
$mockWallClock += 3; |
| 1633 |
$cache->set( $key, 1, 60 ); |
| 1634 |
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); |
| 1635 |
$this->assertLessThan( 0, $curTTL, "Key in hold-off due to check key" ); |
| 1636 |
|
| 1637 |
$mockWallClock += 10; |
| 1638 |
$cache->set( $key, 1, 60 ); |
| 1639 |
$this->assertSame( 1, $cache->get( $key, $curTTL, [ $checkKey ] ) ); |
| 1640 |
$this->assertGreaterThan( 0, $curTTL, "Key not in hold-off due to check key" ); |
| 1641 |
} |
| 1642 |
|
| 1643 |
public function testDelete() { |
| 1644 |
[ $cache ] = $this->newWanCache(); |
| 1645 |
$key = wfRandomString(); |
| 1646 |
$value = wfRandomString(); |
| 1647 |
$cache->set( $key, $value ); |
| 1648 |
|
| 1649 |
$curTTL = null; |
| 1650 |
$v = $cache->get( $key, $curTTL ); |
| 1651 |
$this->assertSame( $value, $v, "Key was created with value" ); |
| 1652 |
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" ); |
| 1653 |
|
| 1654 |
$cache->delete( $key ); |
| 1655 |
|
| 1656 |
$curTTL = null; |
| 1657 |
$v = $cache->get( $key, $curTTL ); |
| 1658 |
$this->assertSame( false, $v, "Deleted key has false value" ); |
| 1659 |
$this->assertLessThan( 0, $curTTL, "Deleted key has current TTL < 0" ); |
| 1660 |
|
| 1661 |
$cache->set( $key, $value . 'more' ); |
| 1662 |
$v = $cache->get( $key, $curTTL ); |
| 1663 |
$this->assertSame( false, $v, "Deleted key is tombstoned and has false value" ); |
| 1664 |
$this->assertLessThan( 0, $curTTL, "Deleted key is tombstoned and has current TTL < 0" ); |
| 1665 |
|
| 1666 |
$cache->set( $key, $value ); |
| 1667 |
$cache->delete( $key, WANObjectCache::HOLDOFF_TTL_NONE ); |
| 1668 |
|
| 1669 |
$curTTL = null; |
| 1670 |
$v = $cache->get( $key, $curTTL ); |
| 1671 |
$this->assertSame( false, $v, "Deleted key has false value" ); |
| 1672 |
$this->assertSame( null, $curTTL, "Deleted key has null current TTL" ); |
| 1673 |
|
| 1674 |
$cache->set( $key, $value ); |
| 1675 |
$v = $cache->get( $key, $curTTL ); |
| 1676 |
$this->assertSame( $value, $v, "Key was created with value" ); |
| 1677 |
$this->assertGreaterThan( 0, $curTTL, "Existing key has current TTL > 0" ); |
| 1678 |
} |
| 1679 |
|
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
|
| 1685 |
public function testGetWithSetCallback_versions( array $extOpts, $versioned ) { |
| 1686 |
[ $cache ] = $this->newWanCache(); |
| 1687 |
|
| 1688 |
$key = wfRandomString(); |
| 1689 |
$valueV1 = wfRandomString(); |
| 1690 |
$valueV2 = [ wfRandomString() ]; |
| 1691 |
|
| 1692 |
$wasSet = 0; |
| 1693 |
$funcV1 = static function () use ( &$wasSet, $valueV1 ) { |
| 1694 |
++$wasSet; |
| 1695 |
|
| 1696 |
return $valueV1; |
| 1697 |
}; |
| 1698 |
|
| 1699 |
$priorValue = false; |
| 1700 |
$priorAsOf = null; |
| 1701 |
$funcV2 = static function ( $oldValue, &$ttl, $setOpts, $oldAsOf ) |
| 1702 |
use ( &$wasSet, $valueV2, &$priorValue, &$priorAsOf ) { |
| 1703 |
$priorValue = $oldValue; |
| 1704 |
$priorAsOf = $oldAsOf; |
| 1705 |
++$wasSet; |
| 1706 |
|
| 1707 |
return $valueV2; |
| 1708 |
}; |
| 1709 |
|
| 1710 |
|
| 1711 |
$wasSet = 0; |
| 1712 |
$v = $cache->getWithSetCallback( $key, 30, $funcV1, $extOpts ); |
| 1713 |
$this->assertSame( $valueV1, $v, "Value returned" ); |
| 1714 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 1715 |
$cache->getWithSetCallback( $key, 30, $funcV1, $extOpts ); |
| 1716 |
$this->assertSame( 1, $wasSet, "Value not regenerated" ); |
| 1717 |
$this->assertSame( $valueV1, $v, "Value not regenerated" ); |
| 1718 |
|
| 1719 |
if ( $versioned ) { |
| 1720 |
|
| 1721 |
$verOpts = [ 'version' => $extOpts['version'] + 1 ]; |
| 1722 |
} else { |
| 1723 |
|
| 1724 |
$verOpts = [ 'version' => 1 ]; |
| 1725 |
} |
| 1726 |
|
| 1727 |
|
| 1728 |
$wasSet = 0; |
| 1729 |
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts ); |
| 1730 |
$this->assertSame( $valueV2, $v, "Value returned" ); |
| 1731 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 1732 |
$this->assertSame( false, $priorValue, "Old value not given due to old format" ); |
| 1733 |
$this->assertSame( null, $priorAsOf, "Old value not given due to old format" ); |
| 1734 |
|
| 1735 |
$wasSet = 0; |
| 1736 |
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts ); |
| 1737 |
$this->assertSame( $valueV2, $v, "Value not regenerated (secondary key)" ); |
| 1738 |
$this->assertSame( 0, $wasSet, "Value not regenerated (secondary key)" ); |
| 1739 |
|
| 1740 |
|
| 1741 |
$cache->delete( $key, 0 ); |
| 1742 |
|
| 1743 |
|
| 1744 |
$wasSet = 0; |
| 1745 |
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts ); |
| 1746 |
$this->assertSame( $valueV2, $v, "Value returned" ); |
| 1747 |
$this->assertSame( 1, $wasSet, "Value regenerated" ); |
| 1748 |
|
| 1749 |
$v = $cache->getWithSetCallback( $key, 30, $funcV2, $verOpts + $extOpts ); |
| 1750 |
$this->assertSame( $valueV2, $v, "Value not regenerated (main key)" ); |
| 1751 |
$this->assertSame( 1, $wasSet, "Value not regenerated (main key)" ); |
| 1752 |
} |
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
public function testInterimHoldOffCaching( array $params ) { |
| 1758 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 1759 |
|
| 1760 |
$mockWallClock = 1549343530.0; |
| 1761 |
$cache->setMockTime( $mockWallClock ); |
| 1762 |
|
| 1763 |
$value = 'CRL-40-940'; |
| 1764 |
$wasCalled = 0; |
| 1765 |
$func = static function () use ( &$wasCalled, $value ) { |
| 1766 |
$wasCalled++; |
| 1767 |
|
| 1768 |
return $value; |
| 1769 |
}; |
| 1770 |
|
| 1771 |
$cache->useInterimHoldOffCaching( true ); |
| 1772 |
|
| 1773 |
$key = wfRandomString( 32 ); |
| 1774 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1775 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1776 |
$this->assertSame( 1, $wasCalled, 'Value cached' ); |
| 1777 |
|
| 1778 |
$cache->delete( $key ); |
| 1779 |
|
| 1780 |
$mockWallClock += 1; |
| 1781 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1782 |
$this->assertSame( 2, $wasCalled, 'Value regenerated (got mutex)' ); |
| 1783 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1784 |
$this->assertSame( 2, $wasCalled, 'Value interim cached' ); |
| 1785 |
|
| 1786 |
$mockWallClock += 1; |
| 1787 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1788 |
$this->assertSame( 3, $wasCalled, 'Value regenerated (got mutex)' ); |
| 1789 |
|
| 1790 |
$this->setMutexKey( $bag, $key ); |
| 1791 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1792 |
$this->assertSame( 3, $wasCalled, 'Value interim cached (failed mutex)' ); |
| 1793 |
$this->clearMutexKey( $bag, $key ); |
| 1794 |
|
| 1795 |
$cache->useInterimHoldOffCaching( false ); |
| 1796 |
|
| 1797 |
$wasCalled = 0; |
| 1798 |
$key = wfRandomString( 32 ); |
| 1799 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1800 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1801 |
$this->assertSame( 1, $wasCalled, 'Value cached' ); |
| 1802 |
|
| 1803 |
$cache->delete( $key ); |
| 1804 |
|
| 1805 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1806 |
$this->assertSame( 2, $wasCalled, 'Value regenerated (got mutex)' ); |
| 1807 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1808 |
$this->assertSame( 3, $wasCalled, 'Value still regenerated (got mutex)' ); |
| 1809 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1810 |
$this->assertSame( 4, $wasCalled, 'Value still regenerated (got mutex)' ); |
| 1811 |
|
| 1812 |
$this->setMutexKey( $bag, $key ); |
| 1813 |
$cache->getWithSetCallback( $key, 60, $func ); |
| 1814 |
$this->assertSame( 5, $wasCalled, 'Value still regenerated (failed mutex)' ); |
| 1815 |
} |
| 1816 |
|
| 1817 |
public function testTouchKeys() { |
| 1818 |
[ $cache ] = $this->newWanCache(); |
| 1819 |
$key = wfRandomString(); |
| 1820 |
|
| 1821 |
$mockWallClock = 1549343530.0; |
| 1822 |
$priorTime = floor( $mockWallClock ); |
| 1823 |
$cache->setMockTime( $mockWallClock ); |
| 1824 |
|
| 1825 |
$t0 = $cache->getCheckKeyTime( $key ); |
| 1826 |
$this->assertGreaterThanOrEqual( $priorTime, $t0, 'Check key auto-created' ); |
| 1827 |
|
| 1828 |
$mockWallClock += 1.100; |
| 1829 |
$priorTime = floor( $mockWallClock ); |
| 1830 |
$cache->touchCheckKey( $key ); |
| 1831 |
$t1 = $cache->getCheckKeyTime( $key ); |
| 1832 |
$this->assertGreaterThanOrEqual( $priorTime, $t1, 'Check key created' ); |
| 1833 |
|
| 1834 |
$mockWallClock += 1.100; |
| 1835 |
$t2 = $cache->getCheckKeyTime( $key ); |
| 1836 |
$this->assertSame( $t1, $t2, 'Check key time did not change' ); |
| 1837 |
|
| 1838 |
$mockWallClock += 1.100; |
| 1839 |
$cache->touchCheckKey( $key ); |
| 1840 |
$t3 = $cache->getCheckKeyTime( $key ); |
| 1841 |
$this->assertGreaterThan( $t2, $t3, 'Check key time increased' ); |
| 1842 |
|
| 1843 |
$mockWallClock += 1.100; |
| 1844 |
$t4 = $cache->getCheckKeyTime( $key ); |
| 1845 |
$this->assertSame( $t3, $t4, 'Check key time did not change' ); |
| 1846 |
|
| 1847 |
$mockWallClock += 1.100; |
| 1848 |
$cache->resetCheckKey( $key ); |
| 1849 |
$t5 = $cache->getCheckKeyTime( $key ); |
| 1850 |
$this->assertGreaterThan( $t4, $t5, 'Check key time increased' ); |
| 1851 |
|
| 1852 |
$mockWallClock += 1.100; |
| 1853 |
$t6 = $cache->getCheckKeyTime( $key ); |
| 1854 |
$this->assertSame( $t5, $t6, 'Check key time did not change' ); |
| 1855 |
} |
| 1856 |
|
| 1857 |
|
| 1858 |
|
| 1859 |
|
| 1860 |
|
| 1861 |
public function testGetWithSeveralCheckKeys( array $params ) { |
| 1862 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 1863 |
$key = wfRandomString(); |
| 1864 |
$tKey1 = wfRandomString(); |
| 1865 |
$tKey2 = wfRandomString(); |
| 1866 |
$value = 'meow'; |
| 1867 |
|
| 1868 |
$mockWallClock = 1549343530.0; |
| 1869 |
$priorTime = $mockWallClock; |
| 1870 |
$cache->setMockTime( $mockWallClock ); |
| 1871 |
|
| 1872 |
|
| 1873 |
$this->setCheckKey( $bag, $tKey2, $priorTime - 3 ); |
| 1874 |
$this->setCheckKey( $bag, $tKey2, $priorTime - 5 ); |
| 1875 |
$this->setCheckKey( $bag, $tKey1, $priorTime - 30 ); |
| 1876 |
$cache->set( $key, $value, 30 ); |
| 1877 |
|
| 1878 |
$curTTL = null; |
| 1879 |
$v = $cache->get( $key, $curTTL, [ $tKey1, $tKey2 ] ); |
| 1880 |
$this->assertSame( $value, $v, "Value matches" ); |
| 1881 |
$this->assertLessThan( -4.9, $curTTL, "Correct CTL" ); |
| 1882 |
$this->assertGreaterThan( -5.1, $curTTL, "Correct CTL" ); |
| 1883 |
} |
| 1884 |
|
| 1885 |
|
| 1886 |
|
| 1887 |
public function testSetWithLag() { |
| 1888 |
[ $cache ] = $this->newWanCache(); |
| 1889 |
|
| 1890 |
$mockWallClock = 1549343530.0; |
| 1891 |
$cache->setMockTime( $mockWallClock ); |
| 1892 |
|
| 1893 |
$v = 1; |
| 1894 |
|
| 1895 |
$key = wfRandomString(); |
| 1896 |
$opts = [ 'lag' => 300, 'since' => $mockWallClock, 'walltime' => 0.1 ]; |
| 1897 |
$cache->set( $key, $v, 30, $opts ); |
| 1898 |
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written." ); |
| 1899 |
|
| 1900 |
$key = wfRandomString(); |
| 1901 |
$opts = [ 'lag' => 300, 'since' => $mockWallClock ]; |
| 1902 |
$cache->set( $key, $v, 30, $opts ); |
| 1903 |
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written (no walltime)." ); |
| 1904 |
|
| 1905 |
$key = wfRandomString(); |
| 1906 |
$cache->get( $key ); |
| 1907 |
$mockWallClock += 15; |
| 1908 |
$opts = [ 'lag' => 300, 'since' => $mockWallClock ]; |
| 1909 |
$cache->set( $key, $v, 30, $opts ); |
| 1910 |
$this->assertSame( $v, $cache->get( $key ), "Repl-lagged value written (auto-walltime)." ); |
| 1911 |
|
| 1912 |
$key = wfRandomString(); |
| 1913 |
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300, 'walltime' => 0.1 ]; |
| 1914 |
$cache->set( $key, $v, 30, $opts ); |
| 1915 |
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value written." ); |
| 1916 |
|
| 1917 |
$key = wfRandomString(); |
| 1918 |
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300 ]; |
| 1919 |
$cache->set( $key, $v, 30, $opts ); |
| 1920 |
$this->assertSame( $v, $cache->get( $key ), "Trx-lagged value written (no walltime)." ); |
| 1921 |
|
| 1922 |
$key = wfRandomString(); |
| 1923 |
$cache->get( $key ); |
| 1924 |
$mockWallClock += 15; |
| 1925 |
$opts = [ 'lag' => 0, 'since' => $mockWallClock - 300 ]; |
| 1926 |
$cache->set( $key, $v, 30, $opts ); |
| 1927 |
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value not written (auto-walltime)." ); |
| 1928 |
|
| 1929 |
$key = wfRandomString(); |
| 1930 |
$opts = [ 'lag' => 5, 'since' => $mockWallClock - 5, 'walltime' => 0.1 ]; |
| 1931 |
$cache->set( $key, $v, 30, $opts ); |
| 1932 |
$this->assertSame( false, $cache->get( $key ), "Trx-lagged value written." ); |
| 1933 |
|
| 1934 |
$key = wfRandomString(); |
| 1935 |
$opts = [ 'lag' => 3, 'since' => $mockWallClock - 3 ]; |
| 1936 |
$cache->set( $key, $v, 30, $opts ); |
| 1937 |
$this->assertSame( $v, $cache->get( $key ), "Lagged value written (no walltime)." ); |
| 1938 |
} |
| 1939 |
|
| 1940 |
|
| 1941 |
|
| 1942 |
public function testWritePending() { |
| 1943 |
[ $cache ] = $this->newWanCache(); |
| 1944 |
$value = 1; |
| 1945 |
|
| 1946 |
$key = wfRandomString(); |
| 1947 |
$opts = [ 'pending' => true ]; |
| 1948 |
$cache->set( $key, $value, 30, $opts ); |
| 1949 |
$this->assertSame( false, $cache->get( $key ), "Pending value not written." ); |
| 1950 |
} |
| 1951 |
|
| 1952 |
public function testMcRouterSupport() { |
| 1953 |
$localBag = $this->getMockBuilder( EmptyBagOStuff::class ) |
| 1954 |
->onlyMethods( [ 'set', 'delete' ] )->getMock(); |
| 1955 |
$localBag->expects( $this->never() )->method( 'set' ); |
| 1956 |
$localBag->expects( $this->never() )->method( 'delete' ); |
| 1957 |
$wanCache = new WANObjectCache( [ |
| 1958 |
'cache' => $localBag, |
| 1959 |
'broadcastRoutingPrefix' => '/*/mw-wan/', |
| 1960 |
] ); |
| 1961 |
$valFunc = static function () { |
| 1962 |
return 1; |
| 1963 |
}; |
| 1964 |
|
| 1965 |
|
| 1966 |
$wanCache->get( 'x' ); |
| 1967 |
$wanCache->get( 'x', $ctl, [ 'check1' ] ); |
| 1968 |
$wanCache->getMulti( [ 'x', 'y' ] ); |
| 1969 |
$wanCache->getMulti( [ 'x', 'y' ], $ctls, [ 'check2' ] ); |
| 1970 |
$wanCache->getWithSetCallback( 'p', 30, $valFunc ); |
| 1971 |
$wanCache->getCheckKeyTime( 'zzz' ); |
| 1972 |
} |
| 1973 |
|
| 1974 |
public function testMcRouterSupportBroadcastDelete() { |
| 1975 |
$localBag = $this->getMockBuilder( EmptyBagOStuff::class ) |
| 1976 |
->onlyMethods( [ 'set' ] )->getMock(); |
| 1977 |
$wanCache = new WANObjectCache( [ |
| 1978 |
'cache' => $localBag, |
| 1979 |
'broadcastRoutingPrefix' => '/*/mw-wan/', |
| 1980 |
] ); |
| 1981 |
|
| 1982 |
$localBag->expects( $this->once() )->method( 'set' ) |
| 1983 |
->with( "/*/mw-wan/WANCache:test|#|v" ); |
| 1984 |
|
| 1985 |
$wanCache->delete( 'test' ); |
| 1986 |
} |
| 1987 |
|
| 1988 |
public function testMcRouterSupportBroadcastTouchCK() { |
| 1989 |
$localBag = $this->getMockBuilder( EmptyBagOStuff::class ) |
| 1990 |
->onlyMethods( [ 'set' ] )->getMock(); |
| 1991 |
$wanCache = new WANObjectCache( [ |
| 1992 |
'cache' => $localBag, |
| 1993 |
'broadcastRoutingPrefix' => '/*/mw-wan/', |
| 1994 |
] ); |
| 1995 |
|
| 1996 |
$localBag->expects( $this->once() )->method( 'set' ) |
| 1997 |
->with( "/*/mw-wan/WANCache:test|#|t" ); |
| 1998 |
|
| 1999 |
$wanCache->touchCheckKey( 'test' ); |
| 2000 |
} |
| 2001 |
|
| 2002 |
public function testMcRouterSupportBroadcastResetCK() { |
| 2003 |
$localBag = $this->getMockBuilder( EmptyBagOStuff::class ) |
| 2004 |
->onlyMethods( [ 'delete' ] )->getMock(); |
| 2005 |
$wanCache = new WANObjectCache( [ |
| 2006 |
'cache' => $localBag, |
| 2007 |
'broadcastRoutingPrefix' => '/*/mw-wan/', |
| 2008 |
] ); |
| 2009 |
|
| 2010 |
$localBag->expects( $this->once() )->method( 'delete' ) |
| 2011 |
->with( "/*/mw-wan/WANCache:test|#|t" ); |
| 2012 |
|
| 2013 |
$wanCache->resetCheckKey( 'test' ); |
| 2014 |
} |
| 2015 |
|
| 2016 |
public function testEpoch() { |
| 2017 |
$bag = new HashBagOStuff(); |
| 2018 |
$cache = new WANObjectCache( [ 'cache' => $bag ] ); |
| 2019 |
$key = $cache->makeGlobalKey( 'The whole of the Law' ); |
| 2020 |
|
| 2021 |
$mockWallClock = 1549343530.0; |
| 2022 |
$cache->setMockTime( $mockWallClock ); |
| 2023 |
|
| 2024 |
$cache->set( $key, 'Do what thou Wilt' ); |
| 2025 |
$cache->touchCheckKey( $key ); |
| 2026 |
|
| 2027 |
$then = $mockWallClock; |
| 2028 |
$mockWallClock += 30; |
| 2029 |
$this->assertSame( 'Do what thou Wilt', $cache->get( $key ) ); |
| 2030 |
$this->assertEqualsWithDelta( |
| 2031 |
$then, |
| 2032 |
$cache->getCheckKeyTime( $key ), |
| 2033 |
0.01, |
| 2034 |
'Check key init' |
| 2035 |
); |
| 2036 |
|
| 2037 |
$cache = new WANObjectCache( [ |
| 2038 |
'cache' => $bag, |
| 2039 |
'epoch' => $mockWallClock - 3600 |
| 2040 |
] ); |
| 2041 |
$cache->setMockTime( $mockWallClock ); |
| 2042 |
|
| 2043 |
$this->assertSame( 'Do what thou Wilt', $cache->get( $key ) ); |
| 2044 |
$this->assertEqualsWithDelta( |
| 2045 |
$then, |
| 2046 |
$cache->getCheckKeyTime( $key ), |
| 2047 |
0.01, |
| 2048 |
'Check key kept' |
| 2049 |
); |
| 2050 |
|
| 2051 |
$mockWallClock += 30; |
| 2052 |
$cache = new WANObjectCache( [ |
| 2053 |
'cache' => $bag, |
| 2054 |
'epoch' => $mockWallClock + 3600 |
| 2055 |
] ); |
| 2056 |
$cache->setMockTime( $mockWallClock ); |
| 2057 |
|
| 2058 |
$this->assertSame( false, $cache->get( $key ), 'Key rejected due to epoch' ); |
| 2059 |
$this->assertEqualsWithDelta( |
| 2060 |
$mockWallClock, |
| 2061 |
$cache->getCheckKeyTime( $key ), |
| 2062 |
0.01, |
| 2063 |
'Check key reset' |
| 2064 |
); |
| 2065 |
} |
| 2066 |
|
| 2067 |
|
| 2068 |
|
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
public function testAdaptiveTTL( $ago, $maxTTL, $minTTL, $factor, $adaptiveTTL ) { |
| 2076 |
[ $cache ] = $this->newWanCache(); |
| 2077 |
$mtime = $ago ? time() - $ago : $ago; |
| 2078 |
$margin = 5; |
| 2079 |
$ttl = $cache->adaptiveTTL( $mtime, $maxTTL, $minTTL, $factor ); |
| 2080 |
|
| 2081 |
$this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl ); |
| 2082 |
$this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl ); |
| 2083 |
|
| 2084 |
$ttl = $cache->adaptiveTTL( (string)$mtime, $maxTTL, $minTTL, $factor ); |
| 2085 |
|
| 2086 |
$this->assertGreaterThanOrEqual( $adaptiveTTL - $margin, $ttl ); |
| 2087 |
$this->assertLessThanOrEqual( $adaptiveTTL + $margin, $ttl ); |
| 2088 |
} |
| 2089 |
|
| 2090 |
public static function provideAdaptiveTTL() { |
| 2091 |
return [ |
| 2092 |
[ 3600, 900, 30, 0.2, 720 ], |
| 2093 |
[ 3600, 500, 30, 0.2, 500 ], |
| 2094 |
[ 3600, 86400, 800, 0.2, 800 ], |
| 2095 |
[ false, 86400, 800, 0.2, 800 ], |
| 2096 |
[ null, 86400, 800, 0.2, 800 ] |
| 2097 |
]; |
| 2098 |
} |
| 2099 |
|
| 2100 |
|
| 2101 |
|
| 2102 |
public function testNewEmpty() { |
| 2103 |
$this->assertInstanceOf( |
| 2104 |
WANObjectCache::class, |
| 2105 |
WANObjectCache::newEmpty() |
| 2106 |
); |
| 2107 |
} |
| 2108 |
|
| 2109 |
|
| 2110 |
|
| 2111 |
public function testSetLogger() { |
| 2112 |
[ $cache ] = $this->newWanCache(); |
| 2113 |
$this->assertSame( null, $cache->setLogger( new NullLogger ) ); |
| 2114 |
} |
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
public function testGetQoS() { |
| 2119 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 2120 |
->onlyMethods( [ 'getQoS' ] )->getMock(); |
| 2121 |
$backend->expects( $this->once() )->method( 'getQoS' ) |
| 2122 |
->willReturn( BagOStuff::QOS_UNKNOWN ); |
| 2123 |
$wanCache = new WANObjectCache( [ 'cache' => $backend ] ); |
| 2124 |
|
| 2125 |
$this->assertSame( |
| 2126 |
$wanCache::QOS_UNKNOWN, |
| 2127 |
$wanCache->getQoS( $wanCache::ATTR_DURABILITY ) |
| 2128 |
); |
| 2129 |
} |
| 2130 |
|
| 2131 |
|
| 2132 |
|
| 2133 |
public function testMakeKey() { |
| 2134 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 2135 |
->onlyMethods( [ 'makeKey' ] )->getMock(); |
| 2136 |
$backend->expects( $this->once() )->method( 'makeKey' ) |
| 2137 |
->willReturn( 'special' ); |
| 2138 |
|
| 2139 |
$wanCache = new WANObjectCache( [ |
| 2140 |
'cache' => $backend |
| 2141 |
] ); |
| 2142 |
|
| 2143 |
$this->assertSame( 'special', $wanCache->makeKey( 'a', 'b' ) ); |
| 2144 |
} |
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
public function testMakeGlobalKey() { |
| 2149 |
$backend = $this->getMockBuilder( HashBagOStuff::class ) |
| 2150 |
->onlyMethods( [ 'makeGlobalKey' ] )->getMock(); |
| 2151 |
$backend->expects( $this->once() )->method( 'makeGlobalKey' ) |
| 2152 |
->willReturn( 'special' ); |
| 2153 |
|
| 2154 |
$wanCache = new WANObjectCache( [ |
| 2155 |
'cache' => $backend |
| 2156 |
] ); |
| 2157 |
|
| 2158 |
$this->assertSame( 'special', $wanCache->makeGlobalKey( 'a', 'b' ) ); |
| 2159 |
} |
| 2160 |
|
| 2161 |
public static function statsKeyProvider() { |
| 2162 |
return [ |
| 2163 |
[ 'domain:page:5', 'page' ], |
| 2164 |
[ 'domain:main-key', 'main-key' ], |
| 2165 |
[ 'domain:page:history', 'page' ], |
| 2166 |
|
| 2167 |
[ 'domain:foo-bar-1.2:abc:v2', 'foo-bar-1_2' ], |
| 2168 |
[ 'missingdomainkey', 'missingdomainkey' ] |
| 2169 |
]; |
| 2170 |
} |
| 2171 |
|
| 2172 |
|
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
|
| 2177 |
public function testStatsKeyClass( $key, $class ) { |
| 2178 |
|
| 2179 |
$wanCache = TestingAccessWrapper::newFromObject( new WANObjectCache( [ |
| 2180 |
'cache' => new HashBagOStuff |
| 2181 |
] ) ); |
| 2182 |
|
| 2183 |
$this->assertSame( $class, $wanCache->determineKeyGroupForStats( $key ) ); |
| 2184 |
} |
| 2185 |
|
| 2186 |
|
| 2187 |
|
| 2188 |
public function testMakeMultiKeys() { |
| 2189 |
[ $cache ] = $this->newWanCache(); |
| 2190 |
|
| 2191 |
$ids = [ 1, 2, 3, 4, 4, 5, 6, 6, 7, 7 ]; |
| 2192 |
$keyCallback = static function ( $id, WANObjectCache $cache ) { |
| 2193 |
return $cache->makeKey( 'key', $id ); |
| 2194 |
}; |
| 2195 |
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback ); |
| 2196 |
|
| 2197 |
$expected = [ |
| 2198 |
"local:key:1" => 1, |
| 2199 |
"local:key:2" => 2, |
| 2200 |
"local:key:3" => 3, |
| 2201 |
"local:key:4" => 4, |
| 2202 |
"local:key:5" => 5, |
| 2203 |
"local:key:6" => 6, |
| 2204 |
"local:key:7" => 7 |
| 2205 |
]; |
| 2206 |
$this->assertSame( $expected, iterator_to_array( $keyedIds ) ); |
| 2207 |
|
| 2208 |
$ids = [ '1', '2', '3', '4', '4', '5', '6', '6', '7', '7' ]; |
| 2209 |
$keyCallback = static function ( $id, WANObjectCache $cache ) { |
| 2210 |
return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' ); |
| 2211 |
}; |
| 2212 |
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback ); |
| 2213 |
|
| 2214 |
$expected = [ |
| 2215 |
"global:key:1:a:1:b" => '1', |
| 2216 |
"global:key:2:a:2:b" => '2', |
| 2217 |
"global:key:3:a:3:b" => '3', |
| 2218 |
"global:key:4:a:4:b" => '4', |
| 2219 |
"global:key:5:a:5:b" => '5', |
| 2220 |
"global:key:6:a:6:b" => '6', |
| 2221 |
"global:key:7:a:7:b" => '7' |
| 2222 |
]; |
| 2223 |
$this->assertSame( $expected, iterator_to_array( $keyedIds ) ); |
| 2224 |
} |
| 2225 |
|
| 2226 |
|
| 2227 |
|
| 2228 |
public function testMakeMultiKeysIntString() { |
| 2229 |
[ $cache ] = $this->newWanCache(); |
| 2230 |
$ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7, '7' ]; |
| 2231 |
$keyCallback = static function ( $id, WANObjectCache $cache ) { |
| 2232 |
return $cache->makeGlobalKey( 'key', $id, 'a', $id, 'b' ); |
| 2233 |
}; |
| 2234 |
|
| 2235 |
$keyedIds = $cache->makeMultiKeys( $ids, $keyCallback ); |
| 2236 |
|
| 2237 |
$expected = [ |
| 2238 |
"global:key:1:a:1:b" => 1, |
| 2239 |
"global:key:2:a:2:b" => 2, |
| 2240 |
"global:key:3:a:3:b" => 3, |
| 2241 |
"global:key:4:a:4:b" => 4, |
| 2242 |
"global:key:5:a:5:b" => 5, |
| 2243 |
"global:key:6:a:6:b" => 6, |
| 2244 |
"global:key:7:a:7:b" => 7 |
| 2245 |
]; |
| 2246 |
$this->assertSame( $expected, iterator_to_array( $keyedIds ) ); |
| 2247 |
} |
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
public function testMakeMultiKeysCollision() { |
| 2252 |
[ $cache ] = $this->newWanCache(); |
| 2253 |
$ids = [ 1, 2, 3, 4, '4', 5, 6, 6, 7 ]; |
| 2254 |
|
| 2255 |
$this->expectException( UnexpectedValueException::class ); |
| 2256 |
$cache->makeMultiKeys( |
| 2257 |
$ids, |
| 2258 |
static function ( $id ) { |
| 2259 |
return "keymod:" . $id % 3; |
| 2260 |
} |
| 2261 |
); |
| 2262 |
} |
| 2263 |
|
| 2264 |
|
| 2265 |
|
| 2266 |
public function testMultiRemap() { |
| 2267 |
[ $cache ] = $this->newWanCache(); |
| 2268 |
|
| 2269 |
$ids = [ 'a', 'b', 'c' ]; |
| 2270 |
$res = [ 'keyA' => 1, 'keyB' => 2, 'keyC' => 3 ]; |
| 2271 |
$this->assertSame( |
| 2272 |
[ 'a' => 1, 'b' => 2, 'c' => 3 ], |
| 2273 |
$cache->multiRemap( $ids, $res ) |
| 2274 |
); |
| 2275 |
|
| 2276 |
$ids = [ 'd', 'c' ]; |
| 2277 |
$res = [ 'keyD' => 40, 'keyC' => 30 ]; |
| 2278 |
$this->assertSame( |
| 2279 |
[ 'd' => 40, 'c' => 30 ], |
| 2280 |
$cache->multiRemap( $ids, $res ) |
| 2281 |
); |
| 2282 |
} |
| 2283 |
|
| 2284 |
|
| 2285 |
|
| 2286 |
public function testHash256() { |
| 2287 |
[ $cache ] = $this->newWanCache( [ 'epoch' => 5 ] ); |
| 2288 |
$this->assertEquals( |
| 2289 |
'f402bce76bfa1136adc705d8d5719911ce1fe61f0ad82ddf79a15f3c4de6ec4c', |
| 2290 |
$cache->hash256( 'x' ) |
| 2291 |
); |
| 2292 |
|
| 2293 |
[ $cache ] = $this->newWanCache( [ 'epoch' => 50 ] ); |
| 2294 |
$this->assertSame( |
| 2295 |
'f79a126722f0a682c4c500509f1b61e836e56c4803f92edc89fc281da5caa54e', |
| 2296 |
$cache->hash256( 'x' ) |
| 2297 |
); |
| 2298 |
|
| 2299 |
[ $cache ] = $this->newWanCache( [ 'secret' => 'garden' ] ); |
| 2300 |
$this->assertSame( |
| 2301 |
'48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093', |
| 2302 |
$cache->hash256( 'x' ) |
| 2303 |
); |
| 2304 |
|
| 2305 |
[ $cache ] = $this->newWanCache( [ 'secret' => 'garden', 'epoch' => 3 ] ); |
| 2306 |
$this->assertSame( |
| 2307 |
'48cd57016ffe29981a1114c45e5daef327d30fc6206cb73edc3cb94b4d8fe093', |
| 2308 |
$cache->hash256( 'x' ) |
| 2309 |
); |
| 2310 |
} |
| 2311 |
|
| 2312 |
|
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
public function testCoalesceKeys( array $params, $keyNeedle ) { |
| 2319 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 2320 |
$key = wfRandomString(); |
| 2321 |
$callback = static function () { |
| 2322 |
return 2020; |
| 2323 |
}; |
| 2324 |
|
| 2325 |
$cache->getWithSetCallback( $key, 60, $callback ); |
| 2326 |
$wrapper = TestingAccessWrapper::newFromObject( $bag ); |
| 2327 |
foreach ( $wrapper->bag as $bagKey => $_ ) { |
| 2328 |
if ( $keyNeedle === null ) { |
| 2329 |
$this->assertDoesNotMatchRegularExpression( '/[#{}]/', $bagKey, 'Respects "coalesceKeys"' ); |
| 2330 |
} else { |
| 2331 |
$this->assertStringContainsString( |
| 2332 |
$keyNeedle, |
| 2333 |
$bagKey, |
| 2334 |
'Respects "coalesceKeys"' |
| 2335 |
); |
| 2336 |
} |
| 2337 |
} |
| 2338 |
} |
| 2339 |
|
| 2340 |
|
| 2341 |
|
| 2342 |
|
| 2343 |
|
| 2344 |
|
| 2345 |
public function testSegmentableValues( array $params, $keyNeedle ) { |
| 2346 |
[ $cache, $bag ] = $this->newWanCache( $params ); |
| 2347 |
$mockWallClock = 1549343530.0; |
| 2348 |
$cache->setMockTime( $mockWallClock ); |
| 2349 |
$key = $cache->makeGlobalKey( 'z', wfRandomString() ); |
| 2350 |
|
| 2351 |
$tiny = 418; |
| 2352 |
$small = wfRandomString( 32 ); |
| 2353 |
|
| 2354 |
|
| 2355 |
$big = str_repeat( wfRandomString( 32 ) . '-' . wfRandomString( 32 ), 32768 ); |
| 2356 |
|
| 2357 |
$cases = [ 'tiny' => $tiny, 'small' => $small, 'big' => $big ]; |
| 2358 |
foreach ( $cases as $case => $value ) { |
| 2359 |
$cache->set( $key, $value, 10, [ 'segmentable' => 1 ] ); |
| 2360 |
$this->assertEquals( $value, $cache->get( $key ), "get $case" ); |
| 2361 |
$this->assertEquals( [ $key => $value ], $cache->getMulti( [ $key ] ), "get $case" ); |
| 2362 |
|
| 2363 |
$this->assertTrue( $cache->delete( $key ), "delete $case" ); |
| 2364 |
$this->assertFalse( $cache->get( $key ), "deleted $case" ); |
| 2365 |
$this->assertEquals( [], $cache->getMulti( [ $key ] ), "deleted $case" ); |
| 2366 |
$mockWallClock += 40; |
| 2367 |
|
| 2368 |
$v = $cache->getWithSetCallback( |
| 2369 |
$key, |
| 2370 |
10, |
| 2371 |
static function ( $cache, $key, $oldValue ) use ( $value ) { |
| 2372 |
return "@$value"; |
| 2373 |
}, |
| 2374 |
[ 'segmentable' => 1 ] |
| 2375 |
); |
| 2376 |
$this->assertEquals( "@$value", $v, "get $case" ); |
| 2377 |
$this->assertEquals( "@$value", $cache->get( $key ), "get $case" ); |
| 2378 |
|
| 2379 |
$this->assertTrue( |
| 2380 |
$cache->delete( $key ), |
| 2381 |
"prune $case" |
| 2382 |
); |
| 2383 |
$this->assertFalse( $cache->get( $key ), "pruned $case" ); |
| 2384 |
$this->assertEquals( [], $cache->getMulti( [ $key ] ), "pruned $case" ); |
| 2385 |
$mockWallClock += 40; |
| 2386 |
} |
| 2387 |
} |
| 2388 |
} |
| 2389 |
|
| 2390 |
class McrouterHashBagOStuff extends HashBagOStuff { |
| 2391 |
public function set( $key, $value, $exptime = 0, $flags = 0 ) { |
| 2392 |
|
| 2393 |
|
| 2394 |
if ( preg_match( '#^/\*/[^/]+/(.*)$#', $key, $m ) ) { |
| 2395 |
$key = $m[1]; |
| 2396 |
} |
| 2397 |
|
| 2398 |
return parent::set( $key, $value, $exptime, $flags ); |
| 2399 |
} |
| 2400 |
|
| 2401 |
public function delete( $key, $flags = 0 ) { |
| 2402 |
|
| 2403 |
|
| 2404 |
if ( preg_match( '#^/\*/[^/]+/(.*)$#', $key, $m ) ) { |
| 2405 |
$key = $m[1]; |
| 2406 |
} |
| 2407 |
|
| 2408 |
return parent::delete( $key, $flags ); |
| 2409 |
} |
| 2410 |
} |
| 2411 |
|
| 2412 |
class NearExpiringWANObjectCache extends WANObjectCache { |
| 2413 |
private const CLOCK_SKEW = 1; |
| 2414 |
|
| 2415 |
protected function worthRefreshExpiring( $curTTL, $logicalTTL, $lowTTL ) { |
| 2416 |
return ( $curTTL > 0 && ( $curTTL + self::CLOCK_SKEW ) < $lowTTL ); |
| 2417 |
} |
| 2418 |
} |
| 2419 |
|
| 2420 |
class PopularityRefreshingWANObjectCache extends WANObjectCache { |
| 2421 |
protected function worthRefreshPopular( $asOf, $ageNew, $timeTillRefresh, $now ) { |
| 2422 |
return ( ( $now - $asOf ) > $timeTillRefresh ); |
| 2423 |
} |
| 2424 |
} |
| 2425 |
|
| 2426 |
class SerialHashBagOStuff extends HashBagOStuff { |
| 2427 |
protected function doGet( $key, $flags = 0, &$casToken = null ) { |
| 2428 |
$serialized = parent::doGet( $key, $flags, $casToken ); |
| 2429 |
|
| 2430 |
return ( $serialized !== false ) ? $this->unserialize( $serialized ) : false; |
| 2431 |
} |
| 2432 |
|
| 2433 |
protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) { |
| 2434 |
$serialized = $this->getSerialized( $value, $key ); |
| 2435 |
|
| 2436 |
return parent::doSet( $key, $serialized, $exptime, $flags ); |
| 2437 |
} |
| 2438 |
} |