feat: 本次提交更新内容如下
php有问题,覆盖下
This commit is contained in:
@@ -11,10 +11,10 @@
|
||||
|
||||
namespace think\session\driver;
|
||||
|
||||
use SessionHandler;
|
||||
use SessionHandlerInterface;
|
||||
use think\Exception;
|
||||
|
||||
class Memcache extends SessionHandler
|
||||
class Memcache implements SessionHandlerInterface
|
||||
{
|
||||
protected $handler = null;
|
||||
protected $config = [
|
||||
@@ -34,8 +34,8 @@ class Memcache extends SessionHandler
|
||||
/**
|
||||
* 打开Session
|
||||
* @access public
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
*/
|
||||
public function open($savePath, $sessName)
|
||||
{
|
||||
@@ -43,13 +43,17 @@ class Memcache extends SessionHandler
|
||||
if (!extension_loaded('memcache')) {
|
||||
throw new Exception('not support:memcache');
|
||||
}
|
||||
|
||||
$this->handler = new \Memcache;
|
||||
|
||||
// 支持集群
|
||||
$hosts = explode(',', $this->config['host']);
|
||||
$ports = explode(',', $this->config['port']);
|
||||
|
||||
if (empty($ports[0])) {
|
||||
$ports[0] = 11211;
|
||||
}
|
||||
|
||||
// 建立连接
|
||||
foreach ((array) $hosts as $i => $host) {
|
||||
$port = isset($ports[$i]) ? $ports[$i] : $ports[0];
|
||||
@@ -57,6 +61,7 @@ class Memcache extends SessionHandler
|
||||
$this->handler->addServer($host, $port, $this->config['persistent'], 1, $this->config['timeout']) :
|
||||
$this->handler->addServer($host, $port, $this->config['persistent'], 1);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -69,13 +74,14 @@ class Memcache extends SessionHandler
|
||||
$this->gc(ini_get('session.gc_maxlifetime'));
|
||||
$this->handler->close();
|
||||
$this->handler = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
*/
|
||||
public function read($sessID)
|
||||
{
|
||||
@@ -85,8 +91,8 @@ class Memcache extends SessionHandler
|
||||
/**
|
||||
* 写入Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param String $sessData
|
||||
* @param string $sessID
|
||||
* @param string $sessData
|
||||
* @return bool
|
||||
*/
|
||||
public function write($sessID, $sessData)
|
||||
@@ -97,7 +103,7 @@ class Memcache extends SessionHandler
|
||||
/**
|
||||
* 删除Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($sessID)
|
||||
@@ -108,7 +114,7 @@ class Memcache extends SessionHandler
|
||||
/**
|
||||
* Session 垃圾回收
|
||||
* @access public
|
||||
* @param string $sessMaxLifeTime
|
||||
* @param string $sessMaxLifeTime
|
||||
* @return true
|
||||
*/
|
||||
public function gc($sessMaxLifeTime)
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
|
||||
namespace think\session\driver;
|
||||
|
||||
use SessionHandler;
|
||||
use SessionHandlerInterface;
|
||||
use think\Exception;
|
||||
|
||||
class Memcached extends SessionHandler
|
||||
class Memcached implements SessionHandlerInterface
|
||||
{
|
||||
protected $handler = null;
|
||||
protected $config = [
|
||||
@@ -35,8 +35,8 @@ class Memcached extends SessionHandler
|
||||
/**
|
||||
* 打开Session
|
||||
* @access public
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
*/
|
||||
public function open($savePath, $sessName)
|
||||
{
|
||||
@@ -44,27 +44,35 @@ class Memcached extends SessionHandler
|
||||
if (!extension_loaded('memcached')) {
|
||||
throw new Exception('not support: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']);
|
||||
|
||||
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);
|
||||
|
||||
if ('' != $this->config['username']) {
|
||||
$this->handler->setOption(\Memcached::OPT_BINARY_PROTOCOL, true);
|
||||
$this->handler->setSaslAuthData($this->config['username'], $this->config['password']);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -77,13 +85,14 @@ class Memcached extends SessionHandler
|
||||
$this->gc(ini_get('session.gc_maxlifetime'));
|
||||
$this->handler->quit();
|
||||
$this->handler = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
*/
|
||||
public function read($sessID)
|
||||
{
|
||||
@@ -93,8 +102,8 @@ class Memcached extends SessionHandler
|
||||
/**
|
||||
* 写入Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param String $sessData
|
||||
* @param string $sessID
|
||||
* @param string $sessData
|
||||
* @return bool
|
||||
*/
|
||||
public function write($sessID, $sessData)
|
||||
@@ -105,7 +114,7 @@ class Memcached extends SessionHandler
|
||||
/**
|
||||
* 删除Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($sessID)
|
||||
@@ -116,7 +125,7 @@ class Memcached extends SessionHandler
|
||||
/**
|
||||
* Session 垃圾回收
|
||||
* @access public
|
||||
* @param string $sessMaxLifeTime
|
||||
* @param string $sessMaxLifeTime
|
||||
* @return true
|
||||
*/
|
||||
public function gc($sessMaxLifeTime)
|
||||
|
||||
@@ -11,10 +11,10 @@
|
||||
|
||||
namespace think\session\driver;
|
||||
|
||||
use SessionHandler;
|
||||
use SessionHandlerInterface;
|
||||
use think\Exception;
|
||||
|
||||
class Redis extends SessionHandler
|
||||
class Redis implements SessionHandlerInterface
|
||||
{
|
||||
/** @var \Redis */
|
||||
protected $handler = null;
|
||||
@@ -37,29 +37,38 @@ class Redis extends SessionHandler
|
||||
/**
|
||||
* 打开Session
|
||||
* @access public
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
* @param string $savePath
|
||||
* @param mixed $sessName
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function open($savePath, $sessName)
|
||||
{
|
||||
// 检测php环境
|
||||
if (!extension_loaded('redis')) {
|
||||
throw new Exception('not support:redis');
|
||||
}
|
||||
$this->handler = new \Redis;
|
||||
if (extension_loaded('redis')) {
|
||||
$this->handler = new \Redis;
|
||||
|
||||
// 建立连接
|
||||
$func = $this->config['persistent'] ? 'pconnect' : 'connect';
|
||||
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
|
||||
// 建立连接
|
||||
$func = $this->config['persistent'] ? 'pconnect' : 'connect';
|
||||
$this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
|
||||
|
||||
if ('' != $this->config['password']) {
|
||||
$this->handler->auth($this->config['password']);
|
||||
}
|
||||
if ('' != $this->config['password']) {
|
||||
$this->handler->auth($this->config['password']);
|
||||
}
|
||||
|
||||
if (0 != $this->config['select']) {
|
||||
$this->handler->select($this->config['select']);
|
||||
if (0 != $this->config['select']) {
|
||||
$this->handler->select($this->config['select']);
|
||||
}
|
||||
} elseif (class_exists('\Predis\Client')) {
|
||||
$params = [];
|
||||
foreach ($this->config as $key => $val) {
|
||||
if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication'])) {
|
||||
$params[$key] = $val;
|
||||
unset($this->config[$key]);
|
||||
}
|
||||
}
|
||||
$this->handler = new \Predis\Client($this->config, $params);
|
||||
} else {
|
||||
throw new \BadFunctionCallException('not support: redis');
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -74,13 +83,14 @@ class Redis extends SessionHandler
|
||||
$this->gc(ini_get('session.gc_maxlifetime'));
|
||||
$this->handler->close();
|
||||
$this->handler = null;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
* @return string
|
||||
*/
|
||||
public function read($sessID)
|
||||
@@ -91,38 +101,79 @@ class Redis extends SessionHandler
|
||||
/**
|
||||
* 写入Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param String $sessData
|
||||
* @param string $sessID
|
||||
* @param string $sessData
|
||||
* @return bool
|
||||
*/
|
||||
public function write($sessID, $sessData)
|
||||
{
|
||||
if ($this->config['expire'] > 0) {
|
||||
return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
|
||||
$result = $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
|
||||
} else {
|
||||
return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
|
||||
$result = $this->handler->set($this->config['session_name'] . $sessID, $sessData);
|
||||
}
|
||||
|
||||
return $result ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除Session
|
||||
* @access public
|
||||
* @param string $sessID
|
||||
* @param string $sessID
|
||||
* @return bool
|
||||
*/
|
||||
public function destroy($sessID)
|
||||
{
|
||||
return $this->handler->delete($this->config['session_name'] . $sessID) > 0;
|
||||
return $this->handler->del($this->config['session_name'] . $sessID) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Session 垃圾回收
|
||||
* @access public
|
||||
* @param string $sessMaxLifeTime
|
||||
* @param string $sessMaxLifeTime
|
||||
* @return bool
|
||||
*/
|
||||
public function gc($sessMaxLifeTime)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redis Session 驱动的加锁机制
|
||||
* @access public
|
||||
* @param string $sessID 用于加锁的sessID
|
||||
* @param integer $timeout 默认过期时间
|
||||
* @return bool
|
||||
*/
|
||||
public function lock($sessID, $timeout = 10)
|
||||
{
|
||||
if (null == $this->handler) {
|
||||
$this->open('', '');
|
||||
}
|
||||
|
||||
$lockKey = 'LOCK_PREFIX_' . $sessID;
|
||||
// 使用setnx操作加锁
|
||||
$isLock = $this->handler->setnx($lockKey, 1);
|
||||
if ($isLock) {
|
||||
// 设置过期时间,防止死任务的出现
|
||||
$this->handler->expire($lockKey, $timeout);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Redis Session 驱动的解锁机制
|
||||
* @access public
|
||||
* @param string $sessID 用于解锁的sessID
|
||||
*/
|
||||
public function unlock($sessID)
|
||||
{
|
||||
if (null == $this->handler) {
|
||||
$this->open('', '');
|
||||
}
|
||||
|
||||
$this->handler->del('LOCK_PREFIX_' . $sessID);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user