mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-01 12:42:48 +08:00
91 lines
2.5 KiB
PHP
91 lines
2.5 KiB
PHP
<?php
|
|
namespace think\session\driver;
|
|
|
|
use think\session\Driver;
|
|
use think\Config;
|
|
|
|
class Memcache extends Driver
|
|
{
|
|
protected $handler = null;
|
|
protected $config = [
|
|
'host' => '127.0.0.1', // 主机
|
|
'port' => 1121, // 端口
|
|
'expire' => 3600, // 有效期
|
|
'timeout' => 1, // 超时时间
|
|
'persistent' => 0, // 是否长连接
|
|
'session_name' => '', // memcache key前缀
|
|
];
|
|
|
|
/**
|
|
* 打开Session
|
|
* @access public
|
|
* @param string $savePath
|
|
* @param mixed $sessName
|
|
*/
|
|
public function open($savePath, $sessName) {
|
|
// 检测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;
|
|
}
|
|
|
|
/**
|
|
* 关闭Session
|
|
* @access public
|
|
*/
|
|
public function close() {
|
|
$this->gc(ini_get('session.gc_maxlifetime'));
|
|
$this->handler->close();
|
|
$this->handler = null;
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* 读取Session
|
|
* @access public
|
|
* @param string $sessID
|
|
*/
|
|
public function read($sessID) {
|
|
return $this->handler->get($this->config['session_name'].$sessID);
|
|
}
|
|
|
|
/**
|
|
* 写入Session
|
|
* @access public
|
|
* @param string $sessID
|
|
* @param String $sessData
|
|
*/
|
|
public function write($sessID, $sessData) {
|
|
return $this->handler->set($this->config['session_name'].$sessID, $sessData, 0, $this->config['expire']);
|
|
}
|
|
|
|
/**
|
|
* 删除Session
|
|
* @access public
|
|
* @param string $sessID
|
|
*/
|
|
public function destroy($sessID) {
|
|
return $this->handler->delete($this->config['session_name'].$sessID);
|
|
}
|
|
|
|
/**
|
|
* Session 垃圾回收
|
|
* @access public
|
|
* @param string $sessMaxLifeTime
|
|
*/
|
|
public function gc($sessMaxLifeTime) {
|
|
return true;
|
|
}
|
|
} |