From 9530ac7cab80d55cf359e5cca240da1a5cd5ed73 Mon Sep 17 00:00:00 2001 From: "J.H" Date: Sat, 28 Jan 2017 20:25:23 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96redis=E9=A9=B1=E5=8A=A8?= =?UTF-8?q?=EF=BC=8C=E7=94=B1=E5=8E=9F=E5=90=8C=E4=B8=80=E6=9C=8D=E5=8A=A1?= =?UTF-8?q?=E5=99=A8=E5=A4=9A=E8=BF=9E=E6=8E=A5=E6=94=B9=E4=B8=BA=E5=8D=95?= =?UTF-8?q?=E4=B8=80=E8=BF=9E=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cache/Driver/RedisDriver.php | 56 +++++++++++++++++++------------- 1 file changed, 34 insertions(+), 22 deletions(-) diff --git a/src/Cache/Driver/RedisDriver.php b/src/Cache/Driver/RedisDriver.php index 8a52653..46bdbdc 100644 --- a/src/Cache/Driver/RedisDriver.php +++ b/src/Cache/Driver/RedisDriver.php @@ -1,7 +1,6 @@ * * @param $option + * * @throws \cross\exception\CoreException */ function __construct(array $option) { - if (!extension_loaded('redis')) { - throw new CoreException('Not support redis extension !'); - } - - $redis = new Redis(); - if (strcasecmp(PHP_OS, 'linux') == 0 && !empty($option['unix_socket'])) { - $redis->connect($option['unix_socket']); + $server = $option['host'] . $option['port']; + if (isset(self::$connects[$server]) && self::$connects[$server]['link']) { + $this->link = self::$connects[$server]['link']; + if (self::$connects[$server]['current_db'] != $option['db']) { + self::$connects[$server]['current_db'] = $option['db']; + $this->link->select($option['db']); + } } else { - $redis->connect($option['host'], $option['port'], $option['timeout']); - } - - $authStatus = true; - if (!empty($option['pass'])) { - $authStatus = $redis->auth($option['pass']); - } + if (!extension_loaded('redis')) { + throw new CoreException('Not support redis extension !'); + } - try { - if ($authStatus) { - $redis->select($option['db']); - $this->link = $redis; + $redis = new Redis(); + if (strcasecmp(PHP_OS, 'linux') == 0 && !empty($option['unix_socket'])) { + self::$connects[$server]['link'] = $redis->connect($option['unix_socket']); } else { - throw new CoreException('Redis auth failed !'); + self::$connects[$server]['link'] = $redis->connect($option['host'], $option['port'], $option['timeout']); + } + + $authStatus = true; + if (!empty($option['pass'])) { + $authStatus = $redis->auth($option['pass']); + } + + try { + if ($authStatus) { + self::$connects[$server]['current_db'] = $option['db']; + $redis->select($option['db']); + self::$connects[$server]['link'] = $this->link = $redis; + } else { + throw new CoreException('Redis auth failed !'); + } + } catch (Exception $e) { + throw new CoreException($e->getMessage()); } - } catch (Exception $e) { - throw new CoreException($e->getMessage()); } } @@ -69,6 +80,7 @@ class RedisDriver * * @param $method * @param $argv + * * @return mixed|null */ public function __call($method, $argv) -- Gitee