修改 Memcache 扩展为 Memcached 扩展

This commit is contained in:
7IN0SAN9
2016-01-27 21:01:19 +08:00
parent 3c9c93c6b8
commit faaf99c036
2 changed files with 17 additions and 21 deletions

View File

@@ -18,16 +18,16 @@ use think\Exception;
* Memcache缓存驱动 * Memcache缓存驱动
* @author liu21st <liu21st@gmail.com> * @author liu21st <liu21st@gmail.com>
*/ */
class Memcache class Memcached
{ {
protected $handler = null; protected $handler = null;
protected $options = [ protected $options = [
'host' => '127.0.0.1', 'host' => '127.0.0.1',
'port' => 11211, 'port' => 11211,
'expire' => 0, 'expire' => 0,
'timeout' => 1, 'timeout' => 1,
'persistent' => false, //'persistent' => false,
'length' => 0, 'length' => 0,
]; ];
/** /**
@@ -37,22 +37,20 @@ class Memcache
*/ */
public function __construct($options = []) public function __construct($options = [])
{ {
if (!extension_loaded('memcache')) { if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcache'); throw new Exception('_NOT_SUPPERT_:memcached');
} }
if (!empty($options)) { if (!empty($options)) {
$this->options = array_merge($this->options, $options); $this->options = array_merge($this->options, $options);
} }
$this->handler = new \Memcache; $this->handler = new \Memcached;
// 支持集群 // 支持集群
$hosts = explode(',', $this->options['host']); $hosts = explode(',', $this->options['host']);
$ports = explode(',', $this->options['port']); $ports = explode(',', $this->options['port']);
foreach ((array) $hosts as $i => $host) { foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0]; $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
false === $options['timeout'] ? $this->handler->addServer($host, $port, 1);
$this->handler->addServer($host, $port, $this->options['persistent'], 1) :
$this->handler->addServer($host, $port, $this->options['persistent'], 1, $this->options['timeout']);
} }
} }

View File

@@ -14,7 +14,7 @@ namespace think\session\driver;
use SessionHandler; use SessionHandler;
use think\Exception; use think\Exception;
class Memcache extends SessionHandler class Memcached extends SessionHandler
{ {
protected $handler = null; protected $handler = null;
protected $config = [ protected $config = [
@@ -22,7 +22,7 @@ class Memcache extends SessionHandler
'port' => 1121, // 端口 'port' => 1121, // 端口
'expire' => 3600, // 有效期 'expire' => 3600, // 有效期
'timeout' => 1, // 超时时间 'timeout' => 1, // 超时时间
'persistent' => 0, // 是否长连接 //'persistent' => 0, // 是否长连接
'session_name' => '', // memcache key前缀 'session_name' => '', // memcache key前缀
]; ];
@@ -40,19 +40,17 @@ class Memcache extends SessionHandler
public function open($savePath, $sessName) public function open($savePath, $sessName)
{ {
// 检测php环境 // 检测php环境
if (!extension_loaded('memcache')) { if (!extension_loaded('memcached')) {
throw new Exception('_NOT_SUPPERT_:memcache'); throw new Exception('_NOT_SUPPERT_:memcached');
} }
$this->handler = new \Memcache; $this->handler = new \Memcached;
// 支持集群 // 支持集群
$hosts = explode(',', $this->config['host']); $hosts = explode(',', $this->config['host']);
$ports = explode(',', $this->config['port']); $ports = explode(',', $this->config['port']);
// 建立连接 // 建立连接
foreach ((array) $hosts as $i => $host) { foreach ((array) $hosts as $i => $host) {
$port = isset($ports[$i]) ? $ports[$i] : $ports[0]; $port = isset($ports[$i]) ? $ports[$i] : $ports[0];
false === $this->config['timeout'] ? $this->handler->addServer($host, $port, 1);
$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;
} }