From c19d9e4224d4977d90c07d2b6e5234ae15f266e9 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 19 Sep 2016 21:21:23 +0200 Subject: [PATCH 1/2] fixed #107: fixed redis server version test in Redis::internalGetMetadata() --- src/Storage/Adapter/Redis.php | 8 +++--- src/Storage/Adapter/RedisResourceManager.php | 23 ++++++++++++++--- .../Adapter/RedisResourceManagerTest.php | 25 ++++++++++++++++--- 3 files changed, 45 insertions(+), 11 deletions(-) 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') { From 722af9ca24be1d08912856c2f6d2edbb17af0770 Mon Sep 17 00:00:00 2001 From: Marc Bennewitz Date: Mon, 19 Sep 2016 22:03:44 +0200 Subject: [PATCH 2/2] Added #107 to CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3b3eab4c..5bf68ddde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ All notable changes to this project will be documented in this file, in reverse ### Fixed +- [#107](https://github.com/zendframework/zend-cache/issues/107) + fixed redis server version test in Redis::internalGetMetadata() - [#111](https://github.com/zendframework/zend-cache/pull/111) Fixed typo in storage adapter doc - [#102](https://github.com/zendframework/zend-cache/pull/102)