objectcache: add WANObjectCache::getMultiCheckKeyTime method
authorAaron Schulz <aschulz@wikimedia.org>
Fri, 1 Dec 2017 04:38:07 +0000 (20:38 -0800)
committerKrinkle <krinklemail@gmail.com>
Fri, 1 Dec 2017 21:45:32 +0000 (21:45 +0000)
Change-Id: I5675fabc5aa70f72659ce02d68caae88be20e06d

includes/libs/objectcache/WANObjectCache.php
tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php

index b337e9e..68da16d 100644 (file)
@@ -601,25 +601,54 @@ class WANObjectCache implements IExpiringStore, LoggerAwareInterface {
         * Note that "check" keys won't collide with other regular keys.
         *
         * @param string $key
-        * @return float UNIX timestamp of the check key
+        * @return float UNIX timestamp
         */
        final public function getCheckKeyTime( $key ) {
-               $key = self::TIME_KEY_PREFIX . $key;
+               return $this->getMultiCheckKeyTime( [ $key ] )[$key];
+       }
 
-               $purge = self::parsePurgeValue( $this->cache->get( $key ) );
-               if ( $purge !== false ) {
-                       $time = $purge[self::FLD_TIME];
-               } else {
-                       // Casting assures identical floats for the next getCheckKeyTime() calls
-                       $now = (string)$this->getCurrentTime();
-                       $this->cache->add( $key,
-                               $this->makePurgeValue( $now, self::HOLDOFF_TTL ),
-                               self::CHECK_KEY_TTL
-                       );
-                       $time = (float)$now;
+       /**
+        * Fetch the values of each timestamp "check" key
+        *
+        * This works like getCheckKeyTime() except it takes a list of keys
+        * and returns a list of timestamps instead of just that of one key
+        *
+        * @see WANObjectCache::getCheckKeyTime()
+        *
+        * @param array $keys
+        * @return float[] Map of (key => UNIX timestamp)
+        * @since 1.31
+        */
+       final public function getMultiCheckKeyTime( array $keys ) {
+               $rawKeys = [];
+               foreach ( $keys as $key ) {
+                       $rawKeys[$key] = self::TIME_KEY_PREFIX . $key;
+               }
+
+               $rawValues = $this->cache->getMulti( $rawKeys );
+               $rawValues += array_fill_keys( $rawKeys, false );
+
+               $index = 0;
+               $times = [];
+               foreach ( $rawKeys as $key => $rawKey ) {
+                       $purge = self::parsePurgeValue( $rawValues[$rawKey] );
+                       if ( $purge !== false ) {
+                               $time = $purge[self::FLD_TIME];
+                       } else {
+                               // Casting assures identical floats for the next getCheckKeyTime() calls
+                               $now = (string)$this->getCurrentTime();
+                               $this->cache->add(
+                                       $rawKey,
+                                       $this->makePurgeValue( $now, self::HOLDOFF_TTL ),
+                                       self::CHECK_KEY_TTL
+                               );
+                               $time = (float)$now;
+                       }
+
+                       $times[$key] = $time;
                }
 
-               return $time;
+               return $times;
        }
 
        /**
index f586d03..e534f92 100644 (file)
@@ -1280,6 +1280,7 @@ class WANObjectCacheTest extends PHPUnit_Framework_TestCase {
         * @covers WANObjectCache::touchCheckKey
         * @covers WANObjectCache::resetCheckKey
         * @covers WANObjectCache::getCheckKeyTime
+        * @covers WANObjectCache::getMultiCheckKeyTime
         * @covers WANObjectCache::makePurgeValue
         * @covers WANObjectCache::parsePurgeValue
         */