Merge "objectcache: fully respect "pcTTL" in WANObjectCache instead of using INF...
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 27 Sep 2019 00:13:37 +0000 (00:13 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 27 Sep 2019 00:13:37 +0000 (00:13 +0000)
includes/libs/objectcache/wancache/WANObjectCache.php
tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php

index 70f3553..2f44a55 100644 (file)
@@ -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 );
+               }
        }
 }
index 7c4c9bf..72ac567 100644 (file)
@@ -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
         */