From 6d46bf327613c33555b8f3d9957302f64282a60f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=98=E7=BC=98?= Date: Mon, 9 May 2016 19:37:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E5=88=86=E9=87=8D=E6=9E=84Redisd?= =?UTF-8?q?=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/Redisd.php | 62 ++++++++++++--------------- 1 file changed, 27 insertions(+), 35 deletions(-) diff --git a/library/think/cache/driver/Redisd.php b/library/think/cache/driver/Redisd.php index a951888b..67058975 100644 --- a/library/think/cache/driver/Redisd.php +++ b/library/think/cache/driver/Redisd.php @@ -72,6 +72,7 @@ class Redisd 'persistent' => false, 'length' => 0, 'prefix' => '', + 'serialize' => \Redis::SERIALIZER_PHP, ]; /** @@ -106,9 +107,10 @@ class Redisd * 一致Hash适合超高可用,跨网络读取,且从节点较多的情况,本业务不考虑该需求 * * @access public - * @param bool $master true 默认主写 + * @param bool $master true 默认主写 + * @return Redisd */ - public function master($master = false) + public function master($master = true) { if (isset(self::$redis_rw_handler[$master])) { $this->handler = self::$redis_rw_handler[$master]; @@ -131,17 +133,23 @@ class Redisd $host = isset($parse['host']) ? $parse['host'] : $host; $port = isset($parse['host']) ? $parse['port'] : $this->options['port']; - $this->handler->$func($host, $port, $this->options['timeout']); - - if (null != $this->options['password']) { - $this->handler->auth($this->options['password']); - } - - APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), 'info'); - //发生错误则摘掉当前节点 try { - $error = $this->handler->getLastError(); + $result = $this->handler->$func($host, $port, $this->options['timeout']); + if($result === false) { + $this->handler->getLastError(); + } + + if (null != $this->options['password']) { + $this->handler->auth($this->options['password']); + } + + $this->handler->setOption(\Redis::OPT_SERIALIZER, $this->options['serialize']); + if(strlen($this->options['prefix'])) { + $this->handler->setOption(\Redis::OPT_PREFIX, $this->options['prefix']); + } + + APP_DEBUG && Log::record("[ CACHE ] INIT Redisd : {$host}:{$port} master->" . var_export($master, true), Log::ALERT); } catch (\RedisException $e) { //phpredis throws a RedisException object if it can't reach the Redis server. //That can happen in case of connectivity issues, if the Redis service is down, or if the redis host is overloaded. @@ -203,9 +211,8 @@ class Redisd { $this->master($master); - $value = null; try { - $value = $this->handler->get($this->options['prefix'] . $name); + $value = $this->handler->get($name); } catch (\RedisException $e) { unset(self::$redis_rw_handler[0]); @@ -215,13 +222,7 @@ class Redisd Log::record($e->getMessage(), Log::ERROR); } - $jsonData = null; - //如果是对象则进行反转 - if (!empty($value) && false !== strpos("[{", $value[0])) { - $jsonData = $value ? json_decode($value, true) : $value; - } - - return (null === $jsonData) ? $value : $jsonData; + return isset($value) ? $value : null; } /** @@ -240,21 +241,12 @@ class Redisd if (is_null($expire)) { $expire = $this->options['expire']; } - $name = $this->options['prefix'] . $name; - /** - * 兼容历史版本 - * Redis不支持存储对象,存入对象会转换成字符串 - * 但在这里,对所有数据做json_decode会有性能开销 - */ - $value = (is_object($value) || is_array($value)) ? json_encode($value) : $value; - - if (null === $value) { - return $this->handler->delete($this->options['prefix'] . $name); - } - - // $expire < 0 则等于ttl操作,列为todo吧 try { + if (null === $value) { + return $this->handler->delete($name); + } + if (is_int($expire) && $expire) { $result = $this->handler->setex($name, $expire, $value); } else { @@ -282,7 +274,7 @@ class Redisd public function rm($name) { $this->master(true); - return $this->handler->delete($this->options['prefix'] . $name); + return $this->handler->delete($name); } /** @@ -303,7 +295,7 @@ class Redisd * * @access public * @param bool $master 指定主从节点,可以从主节点获取结果 - * @return object + * @return \Redis */ public function handler($master = true) {