diff --git a/src/Cache/Driver/RedisDriver.php b/src/Cache/Driver/RedisDriver.php index 8a5265386d1624f404ed88787b0d55798004e44d..46bdbdc34591b77842a2a74e87dfbf71db5199b7 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)