修正redis驱动

This commit is contained in:
thinkphp
2017-11-21 17:10:52 +08:00
parent ea36fdfbe9
commit 23102de352
2 changed files with 16 additions and 8 deletions

View File

@@ -132,8 +132,12 @@ class Redis extends Driver
*/ */
public function inc($name, $step = 1) public function inc($name, $step = 1)
{ {
$key = $this->getCacheKey($name); if ($this->has($name)) {
return $this->handler->incrby($key, $step); $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) public function dec($name, $step = 1)
{ {
$key = $this->getCacheKey($name); if ($this->has($name)) {
return $this->handler->decrby($key, $step); $value = $this->get($name) - $step;
} else {
$value = -$step;
}
return $this->set($name, $value, 0) ? $value : false;
} }
/** /**

View File

@@ -53,11 +53,11 @@ class redisTest extends cacheTestCase
$redis->handler()->setnx('key', 'value'); $redis->handler()->setnx('key', 'value');
$value = $redis->handler()->get('key'); $value = $redis->handler()->get('key');
$this->assertEquals('value', $value); $this->assertEquals('s:5:"value";', $value);
$redis->handler()->hset('hash', 'key', 'value'); $redis->handler()->hset('hash', 'key', 'value');
$value = $redis->handler()->hget('hash', 'key'); $value = $redis->handler()->hget('hash', 'key');
$this->assertEquals('value', $value); $this->assertEquals('s:5:"value";', $value);
} }
public function testExpire() public function testExpire()