From b29ee7a960fb21d28506cf9b07a88dc47c8c8e52 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Thu, 30 Nov 2017 20:38:07 -0800 Subject: [PATCH] objectcache: add WANObjectCache::getMultiCheckKeyTime method Change-Id: I5675fabc5aa70f72659ce02d68caae88be20e06d --- includes/libs/objectcache/WANObjectCache.php | 57 ++++++++++++++----- .../libs/objectcache/WANObjectCacheTest.php | 1 + 2 files changed, 44 insertions(+), 14 deletions(-) diff --git a/includes/libs/objectcache/WANObjectCache.php b/includes/libs/objectcache/WANObjectCache.php index b337e9eb6d..68da16ddbb 100644 --- a/includes/libs/objectcache/WANObjectCache.php +++ b/includes/libs/objectcache/WANObjectCache.php @@ -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; } /** diff --git a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php index f586d03353..e534f92513 100644 --- a/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php +++ b/tests/phpunit/includes/libs/objectcache/WANObjectCacheTest.php @@ -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 */ -- 2.20.1