From 223b7a3717526682316a314f1cfa2a56ecac45b3 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Tue, 24 Sep 2019 02:40:34 -0700 Subject: [PATCH] objectcache: fully respect "pcTTL" in WANObjectCache instead of using INF when >= 0 This was broken since 611e2d55963b91 Change-Id: I612eaf211ff698d5ab1c911aa58195b7bc44f00c --- .../objectcache/wancache/WANObjectCache.php | 8 +++++- .../libs/objectcache/WANObjectCacheTest.php | 26 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/includes/libs/objectcache/wancache/WANObjectCache.php b/includes/libs/objectcache/wancache/WANObjectCache.php index 70f35532d2..2f44a55c6e 100644 --- a/includes/libs/objectcache/wancache/WANObjectCache.php +++ b/includes/libs/objectcache/wancache/WANObjectCache.php @@ -1269,7 +1269,7 @@ class WANObjectCache implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInt // Nested callback process cache use is not lag-safe with regard to HOLDOFF_TTL since // process cached values are more lagged than persistent ones as they are not purged. if ( $pCache && $this->callbackDepth == 0 ) { - $cached = $pCache->get( $this->getProcessCacheKey( $key, $version ), INF, false ); + $cached = $pCache->get( $this->getProcessCacheKey( $key, $version ), $pcTTL, false ); if ( $cached !== false ) { return $cached; } @@ -2545,6 +2545,9 @@ class WANObjectCache implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInt if ( !isset( $this->processCaches[$group] ) ) { list( , $size ) = explode( ':', $group ); $this->processCaches[$group] = new MapCacheLRU( (int)$size ); + if ( $this->wallClockOverride !== null ) { + $this->processCaches[$group]->setMockTime( $this->wallClockOverride ); + } } return $this->processCaches[$group]; @@ -2641,5 +2644,8 @@ class WANObjectCache implements IExpiringStore, IStoreKeyEncoder, LoggerAwareInt public function setMockTime( &$time ) { $this->wallClockOverride =& $time; $this->cache->setMockTime( $time ); + foreach ( $this->processCaches as $pCache ) { + $pCache->setMockTime( $time ); + } } } diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index 7c4c9bf944..72ac5675b8 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -128,6 +128,32 @@ class WANObjectCacheTest extends PHPUnit\Framework\TestCase { $this->assertFalse( $this->cache->get( $key ), "Stale set() value ignored" ); } + /** + * @covers WANObjectCache::getWithSetCallback + */ + public function testProcessCacheTTL() { + $cache = $this->cache; + $mockWallClock = 1549343530.2053; + $cache->setMockTime( $mockWallClock ); + + $key = "mykey-" . wfRandomString(); + + $hits = 0; + $callback = function ( $oldValue, &$ttl, &$setOpts ) use ( &$hits ) { + ++$hits; + return 42; + }; + + $cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + $cache->delete( $key, $cache::HOLDOFF_NONE ); // clear persistent cache + $cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + $this->assertEquals( 1, $hits, "Value process cached" ); + + $mockWallClock += 6; + $cache->getWithSetCallback( $key, 100, $callback, [ 'pcTTL' => 5 ] ); + $this->assertEquals( 2, $hits, "Value expired in process cache" ); + } + /** * @covers WANObjectCache::getWithSetCallback */ -- 2.20.1