From cc69a87991c357cb35a9707fb8f2400d67105e95 Mon Sep 17 00:00:00 2001 From: huangdijia Date: Mon, 25 Jan 2016 14:47:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96host=E3=80=81port=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=EF=BC=8C=E4=B8=8Ecache=E9=A9=B1=E5=8A=A8=E7=BB=9F?= =?UTF-8?q?=E4=B8=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think/session/driver/Memcache.class.php | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/library/think/session/driver/Memcache.class.php b/library/think/session/driver/Memcache.class.php index b9b44b1d..36fd2f13 100644 --- a/library/think/session/driver/Memcache.class.php +++ b/library/think/session/driver/Memcache.class.php @@ -6,13 +6,14 @@ use think\Config; class Memcache extends Driver { - protected $handle = null; + protected $handler = null; protected $config = [ - 'expire' => 3600, // 有效期 - 'timeout' => 1, // 超时时间 - 'persistent' => 0, // 是否长连接 - 'connections' => '127.0.0.1:11211', // 服务器连接配置 OR ['127.0.0.1:11211', '127.0.0.1:11212'] - 'session_name' => '', // memcache key前缀 + 'host' => '127.0.0.1', // 主机 + 'port' => 1121, // 端口 + 'expire' => 3600, // 有效期 + 'timeout' => 1, // 超时时间 + 'persistent' => 0, // 是否长连接 + 'session_name' => '', // memcache key前缀 ]; /** @@ -22,11 +23,20 @@ class Memcache extends Driver * @param mixed $sessName */ public function open($savePath, $sessName) { - $this->handle = new \Memcache; - $connections = is_array($connections) ? $connections : explode(',', $connections); - foreach ($connections as $connection) { - list($host, $port) = explode(':', $connection); - $this->handle->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']); + // 检测php环境 + if (!extension_loaded('memcache')) { + throw new Exception('_NOT_SUPPERT_:memcache'); + } + $this->handler = new \Memcache; + // 支持集群 + $hosts = explode(',', $this->config['host']); + $ports = explode(',', $this->config['port']); + // 建立连接 + foreach ((array) $hosts as $i => $host) { + $port = isset($ports[$i]) ? $ports[$i] : $ports[0]; + false === $config['timeout'] ? + $this->handler->addServer($host, $port, $this->config['persistent'], 1) : + $this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']); } return true; } @@ -37,8 +47,8 @@ class Memcache extends Driver */ public function close() { $this->gc(ini_get('session.gc_maxlifetime')); - $this->handle->close(); - $this->handle = null; + $this->handler->close(); + $this->handler = null; return true; } @@ -48,7 +58,7 @@ class Memcache extends Driver * @param string $sessID */ public function read($sessID) { - return $this->handle->get($this->config['session_name'].$sessID); + return $this->handler->get($this->config['session_name'].$sessID); } /** @@ -58,7 +68,7 @@ class Memcache extends Driver * @param String $sessData */ public function write($sessID, $sessData) { - return $this->handle->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']); + return $this->handler->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']); } /** @@ -67,7 +77,7 @@ class Memcache extends Driver * @param string $sessID */ public function destroy($sessID) { - return $this->handle->delete($this->config['session_name'].$sessID); + return $this->handler->delete($this->config['session_name'].$sessID); } /** @@ -78,4 +88,4 @@ class Memcache extends Driver public function gc($sessMaxLifeTime) { return true; } -} +} \ No newline at end of file