From 23102de3525fa0fa6fb94639b9ba27eb87faaebf Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 21 Nov 2017 17:10:52 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3redis=E9=A9=B1=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/cache/driver/Redis.php | 16 ++++++++++++---- .../library/think/cache/driver/redisTest.php | 8 ++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index 90da6551..a796fb09 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -132,8 +132,12 @@ class Redis extends Driver */ public function inc($name, $step = 1) { - $key = $this->getCacheKey($name); - return $this->handler->incrby($key, $step); + if ($this->has($name)) { + $value = $this->get($name) + $step; + } else { + $value = $step; + } + return $this->set($name, $value, 0) ? $value : false; } /** @@ -145,8 +149,12 @@ class Redis extends Driver */ public function dec($name, $step = 1) { - $key = $this->getCacheKey($name); - return $this->handler->decrby($key, $step); + if ($this->has($name)) { + $value = $this->get($name) - $step; + } else { + $value = -$step; + } + return $this->set($name, $value, 0) ? $value : false; } /** diff --git a/tests/thinkphp/library/think/cache/driver/redisTest.php b/tests/thinkphp/library/think/cache/driver/redisTest.php index 839e5165..a5a36d40 100644 --- a/tests/thinkphp/library/think/cache/driver/redisTest.php +++ b/tests/thinkphp/library/think/cache/driver/redisTest.php @@ -41,7 +41,7 @@ class redisTest extends cacheTestCase $cache = $this->prepare(); $this->assertEquals('string_test', $cache->get('string_test')); $this->assertEquals(11, $cache->get('number_test')); - $result = $cache->get('array_test'); + $result = $cache->get('array_test'); $this->assertEquals('array_test', $result['array_test']); } @@ -53,11 +53,11 @@ class redisTest extends cacheTestCase $redis->handler()->setnx('key', 'value'); $value = $redis->handler()->get('key'); - $this->assertEquals('value', $value); - + $this->assertEquals('s:5:"value";', $value); + $redis->handler()->hset('hash', 'key', 'value'); $value = $redis->handler()->hget('hash', 'key'); - $this->assertEquals('value', $value); + $this->assertEquals('s:5:"value";', $value); } public function testExpire()