From 5a7ea46da86467ef5c0e4aed768264e7ecdb113b Mon Sep 17 00:00:00 2001 From: huangdijia Date: Thu, 28 Jan 2016 13:19:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96memcache=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E5=A2=9E=E5=8A=A0=E8=BF=9E=E6=8E=A5?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/cache/driver/Memcached.php | 23 +++++++++++--------- library/think/session/driver/Memcached.php | 25 ++++++++++++++-------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/library/think/cache/driver/Memcached.php b/library/think/cache/driver/Memcached.php index 4fbfe735..26d4daff 100644 --- a/library/think/cache/driver/Memcached.php +++ b/library/think/cache/driver/Memcached.php @@ -14,10 +14,6 @@ namespace think\cache\driver; use think\Cache; use think\Exception; -/** - * Memcache缓存驱动 - * @author liu21st - */ class Memcached { protected $handler = null; @@ -25,8 +21,7 @@ class Memcached 'host' => '127.0.0.1', 'port' => 11211, 'expire' => 0, - 'timeout' => 1, - //'persistent' => false, + 'timeout' => 0, // 超时时间(单位:毫秒) 'length' => 0, ]; @@ -44,14 +39,22 @@ class Memcached $this->options = array_merge($this->options, $options); } $this->handler = new \Memcached; + // 设置连接超时时间(单位:毫秒) + if ($this->options['timeout'] > 0) { + $this->handler->setOption(Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']); + } // 支持集群 $hosts = explode(',', $this->options['host']); $ports = explode(',', $this->options['port']); - - foreach ((array) $hosts as $i => $host) { - $port = isset($ports[$i]) ? $ports[$i] : $ports[0]; - $this->handler->addServer($host, $port, 1); + if (empty($ports[0])) { + $ports[0] = 11211; } + // 建立连接 + $servers = []; + foreach ((array) $hosts as $i => $host) { + $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1]; + } + $this->handler->addServers($servers); } /** diff --git a/library/think/session/driver/Memcached.php b/library/think/session/driver/Memcached.php index 0ba4fd80..680e3d10 100644 --- a/library/think/session/driver/Memcached.php +++ b/library/think/session/driver/Memcached.php @@ -18,11 +18,10 @@ class Memcached extends SessionHandler { protected $handler = null; protected $config = [ - 'host' => '127.0.0.1', // 主机 - 'port' => 1121, // 端口 - 'expire' => 3600, // 有效期 - 'timeout' => 1, // 超时时间 - //'persistent' => 0, // 是否长连接 + 'host' => '127.0.0.1', // memcache主机 + 'port' => 1121, // memcache端口 + 'expire' => 3600, // session有效期 + 'timeout' => 0, // 连接超时时间(单位:毫秒) 'session_name' => '', // memcache key前缀 ]; @@ -44,14 +43,22 @@ class Memcached extends SessionHandler throw new Exception('_NOT_SUPPERT_:memcached'); } $this->handler = new \Memcached; + // 设置连接超时时间(单位:毫秒) + if ($this->config['timeout'] > 0) { + $this->handler->setOption(Memcached::OPT_CONNECT_TIMEOUT, $this->config['timeout']); + } // 支持集群 $hosts = explode(',', $this->config['host']); $ports = explode(',', $this->config['port']); - // 建立连接 - foreach ((array) $hosts as $i => $host) { - $port = isset($ports[$i]) ? $ports[$i] : $ports[0]; - $this->handler->addServer($host, $port, 1); + if (empty($ports[0])) { + $ports[0] = 11211; } + // 建立连接 + $servers = []; + foreach ((array) $hosts as $i => $host) { + $servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1]; + } + $this->handler->addServers($servers); return true; }