From c281a66748a5d989ae408fd8c60ac6f0c717b600 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Thu, 7 Dec 2017 23:48:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bredis=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 | 30 +++++++++++----------------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/library/think/cache/driver/Redis.php b/library/think/cache/driver/Redis.php index a796fb09..9173ef46 100644 --- a/library/think/cache/driver/Redis.php +++ b/library/think/cache/driver/Redis.php @@ -85,7 +85,7 @@ class Redis extends Driver } try { - $result = unserialize($value); + $result = 0 === strpos($value, 'think_serialize:') ? unserialize(substr($value, 16)) : $value; } catch (\Exception $e) { $result = $default; } @@ -113,7 +113,7 @@ class Redis extends Driver $first = true; } $key = $this->getCacheKey($name); - $value = serialize($value); + $value = is_scalar($value) ? $value : 'think_serialize:' . serialize($value); if (is_int($expire) && $expire) { $result = $this->handler->setex($key, $expire, $value); } else { @@ -126,35 +126,29 @@ class Redis extends Driver /** * 自增缓存(针对数值缓存) * @access public - * @param string $name 缓存变量名 - * @param int $step 步长 + * @param string $name 缓存变量名 + * @param int $step 步长 * @return false|int */ public function inc($name, $step = 1) { - if ($this->has($name)) { - $value = $this->get($name) + $step; - } else { - $value = $step; - } - return $this->set($name, $value, 0) ? $value : false; + $key = $this->getCacheKey($name); + + return $this->handler->incrby($key, $step); } /** * 自减缓存(针对数值缓存) * @access public - * @param string $name 缓存变量名 - * @param int $step 步长 + * @param string $name 缓存变量名 + * @param int $step 步长 * @return false|int */ public function dec($name, $step = 1) { - if ($this->has($name)) { - $value = $this->get($name) - $step; - } else { - $value = -$step; - } - return $this->set($name, $value, 0) ? $value : false; + $key = $this->getCacheKey($name); + + return $this->handler->decrby($key, $step); } /**