优化host、port配置,与cache驱动统一

This commit is contained in:
huangdijia
2016-01-25 14:47:13 +08:00
parent 586a94fa8e
commit cc69a87991

View File

@@ -6,13 +6,14 @@ use think\Config;
class Memcache extends Driver class Memcache extends Driver
{ {
protected $handle = null; protected $handler = null;
protected $config = [ protected $config = [
'expire' => 3600, // 有效期 'host' => '127.0.0.1', // 主机
'timeout' => 1, // 超时时间 'port' => 1121, // 端口
'persistent' => 0, // 是否长连接 'expire' => 3600, // 有效期
'connections' => '127.0.0.1:11211', // 服务器连接配置 OR ['127.0.0.1:11211', '127.0.0.1:11212'] 'timeout' => 1, // 超时时间
'session_name' => '', // memcache key前缀 'persistent' => 0, // 是否长连接
'session_name' => '', // memcache key前缀
]; ];
/** /**
@@ -22,11 +23,20 @@ class Memcache extends Driver
* @param mixed $sessName * @param mixed $sessName
*/ */
public function open($savePath, $sessName) { public function open($savePath, $sessName) {
$this->handle = new \Memcache; // 检测php环境
$connections = is_array($connections) ? $connections : explode(',', $connections); if (!extension_loaded('memcache')) {
foreach ($connections as $connection) { throw new Exception('_NOT_SUPPERT_:memcache');
list($host, $port) = explode(':', $connection); }
$this->handle->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']); $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; return true;
} }
@@ -37,8 +47,8 @@ class Memcache extends Driver
*/ */
public function close() { public function close() {
$this->gc(ini_get('session.gc_maxlifetime')); $this->gc(ini_get('session.gc_maxlifetime'));
$this->handle->close(); $this->handler->close();
$this->handle = null; $this->handler = null;
return true; return true;
} }
@@ -48,7 +58,7 @@ class Memcache extends Driver
* @param string $sessID * @param string $sessID
*/ */
public function read($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 * @param String $sessData
*/ */
public function write($sessID, $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 * @param string $sessID
*/ */
public function destroy($sessID) { public function destroy($sessID) {
return $this->handle->delete($this->config['session_name'].$sessID); return $this->handler->delete($this->config['session_name'].$sessID);
} }
/** /**