优化memcache连接方法,增加连接超时支持

This commit is contained in:
huangdijia
2016-01-28 13:19:44 +08:00
parent 2006fc32eb
commit 5a7ea46da8
2 changed files with 29 additions and 19 deletions

View File

@@ -14,10 +14,6 @@ namespace think\cache\driver;
use think\Cache; use think\Cache;
use think\Exception; use think\Exception;
/**
* Memcache缓存驱动
* @author liu21st <liu21st@gmail.com>
*/
class Memcached class Memcached
{ {
protected $handler = null; protected $handler = null;
@@ -25,8 +21,7 @@ class Memcached
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'port' => 11211, 'port' => 11211,
'expire' => 0, 'expire' => 0,
'timeout' => 1, 'timeout' => 0, // 超时时间(单位:毫秒)
//'persistent' => false,
'length' => 0, 'length' => 0,
]; ];
@@ -44,14 +39,22 @@ class Memcached
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
$this->handler = new \Memcached; $this->handler = new \Memcached;
// 设置连接超时时间(单位:毫秒)
if ($this->options['timeout'] > 0) {
$this->handler->setOption(Memcached::OPT_CONNECT_TIMEOUT, $this->options['timeout']);
}
// 支持集群 // 支持集群
$hosts = explode(',', $this->options['host']); $hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']); $ports = explode(',', $this->options['port']);
if (empty($ports[0])) {
foreach ((array) $hosts as $i => $host) { $ports[0] = 11211;
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$this->handler->addServer($host, $port, 1);
} }
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
} }
/** /**

View File

@@ -18,11 +18,10 @@ class Memcached extends SessionHandler
{ {
protected $handler = null; protected $handler = null;
protected $config = [ protected $config = [
'host' => '127.0.0.1', // 主机 'host' => '127.0.0.1', // memcache主机
'port' => 1121, // 端口 'port' => 1121, // memcache端口
'expire' => 3600, // 有效期 'expire' => 3600, // session有效期
'timeout' => 1, // 超时时间 'timeout' => 0, // 连接超时时间(单位:毫秒)
//'persistent' => 0, // 是否长连接
'session_name' => '', // memcache key前缀 'session_name' => '', // memcache key前缀
]; ];
@@ -44,14 +43,22 @@ class Memcached extends SessionHandler
throw new Exception('_NOT_SUPPERT_:memcached'); throw new Exception('_NOT_SUPPERT_:memcached');
} }
$this->handler = new \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']); $hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']); $ports = explode(',', $this->config['port']);
// 建立连接 if (empty($ports[0])) {
foreach ((array) $hosts as $i => $host) { $ports[0] = 11211;
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
$this->handler->addServer($host, $port, 1);
} }
// 建立连接
$servers = [];
foreach ((array) $hosts as $i => $host) {
$servers[] = [$host, (isset($ports[$i]) ? $ports[$i] : $ports[0]), 1];
}
$this->handler->addServers($servers);
return true; return true;
} }