diff --git a/src/Storage/Adapter/Redis.php b/src/Storage/Adapter/Redis.php index 93770af62..95f592098 100644 --- a/src/Storage/Adapter/Redis.php +++ b/src/Storage/Adapter/Redis.php @@ -553,7 +553,7 @@ protected function internalGetMetadata(& $normalizedKey) $metadata = []; try { - $redisVersion = $this->resourceManager->getMajorVersion($this->resourceId); + $redisVersion = $this->resourceManager->getVersion($this->resourceId); // redis >= 2.8 // The command 'pttl' returns -2 if the item does not exist @@ -565,8 +565,8 @@ protected function internalGetMetadata(& $normalizedKey) } $metadata['ttl'] = ($pttl == -1) ? null : $pttl / 1000; - // redis >= 2.6 - // The command 'pttl' returns -1 if the item does not exist or the item as no associated expire + // redis >= 2.6, < 2.8 + // The command 'pttl' returns -1 if the item does not exist or the item has no associated expire } elseif (version_compare($redisVersion, '2.6', '>=')) { $pttl = $redis->pttl($this->namespacePrefix . $normalizedKey); if ($pttl <= -1) { @@ -578,7 +578,7 @@ protected function internalGetMetadata(& $normalizedKey) $metadata['ttl'] = $pttl / 1000; } - // redis >= 2 + // redis >= 2, < 2.6 // The command 'pttl' is not supported but 'ttl' // The command 'ttl' returns 0 if the item does not exist same as if the item is going to be expired // NOTE: In case of ttl=0 we return false because the item is going to be expired in a very near future diff --git a/src/Storage/Adapter/RedisResourceManager.php b/src/Storage/Adapter/RedisResourceManager.php index 6573850ab..fa2ddd616 100644 --- a/src/Storage/Adapter/RedisResourceManager.php +++ b/src/Storage/Adapter/RedisResourceManager.php @@ -41,16 +41,31 @@ public function hasResource($id) /** * Get redis server version * - * @param string $id + * @param string $resourceId + * @return string + * @throws Exception\RuntimeException + */ + public function getVersion($resourceId) + { + // check resource id and initialize the resource + $this->getResource($resourceId); + + return $this->resources[$resourceId]['version']; + } + + /** + * Get redis major server version + * + * @param string $resourceId * @return int * @throws Exception\RuntimeException */ - public function getMajorVersion($id) + public function getMajorVersion($resourceId) { // check resource id and initialize the resource - $this->getResource($id); + $this->getResource($resourceId); - return (int) $this->resources[$id]['version']; + return (int) $this->resources[$resourceId]['version']; } /** diff --git a/test/Storage/Adapter/RedisResourceManagerTest.php b/test/Storage/Adapter/RedisResourceManagerTest.php index b62984962..8ce024131 100644 --- a/test/Storage/Adapter/RedisResourceManagerTest.php +++ b/test/Storage/Adapter/RedisResourceManagerTest.php @@ -151,9 +151,28 @@ public function testNotValidPersistentIdOptionName() $this->assertInstanceOf('Redis', $this->resourceManager->getResource($resourceId)); } - /** - * Test with 'persistend_id' instead of 'persistent_id' - */ + public function testGetVersion() + { + if (getenv('TESTS_ZEND_CACHE_REDIS_ENABLED') != 'true') { + $this->markTestSkipped('Enable TESTS_ZEND_CACHE_REDIS_ENABLED to run this test'); + } + + if (!extension_loaded('redis')) { + $this->markTestSkipped("Redis extension is not loaded"); + } + + $resourceId = __FUNCTION__; + $resource = [ + 'server' => [ + 'host' => getenv('TESTS_ZEND_CACHE_REDIS_HOST') ?: 'localhost', + 'port' => getenv('TESTS_ZEND_CACHE_REDIS_PORT') ?: 6379, + ], + ]; + $this->resourceManager->setResource($resourceId, $resource); + + $this->assertRegExp('/^\d+\.\d+\.\d+/', $this->resourceManager->getVersion($resourceId)); + } + public function testGetMajorVersion() { if (getenv('TESTS_ZEND_CACHE_REDIS_ENABLED') != 'true') {