You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
120 lines
3.4 KiB
PHTML
120 lines
3.4 KiB
PHTML
4 years ago
|
<?php
|
||
|
|
||
|
namespace Think\Session\Driver;
|
||
|
|
||
|
use think\Exception;
|
||
|
|
||
|
class Redis implements \SessionHandlerInterface
|
||
|
{
|
||
|
/** @var \Redis */
|
||
|
protected $handler = null;
|
||
|
protected $config = [
|
||
|
'host' => '127.0.0.1',
|
||
|
'port' => 6379,
|
||
|
'password' => null,
|
||
|
'select' => 1,
|
||
|
'expire' => 3600,
|
||
|
'timeout' => 0,
|
||
|
'persistent' => true,
|
||
|
'session_name' => 'session_',
|
||
|
];
|
||
|
|
||
|
public function __construct($config = [])
|
||
|
{
|
||
|
$this->config['host'] = C("SESSION_REDIS_HOST") ? C("SESSION_REDIS_HOST") : $this->config['host'];
|
||
|
$this->config['port'] = C("SESSION_REDIS_POST") ? C("SESSION_REDIS_POST") : $this->config['port'];
|
||
|
$this->config['password'] = C("SESSION_REDIS_AUTH") ? C("SESSION_REDIS_AUTH") : $this->config['password'];
|
||
|
$this->config['select'] = C("SESSION_REDIS_SELECT") ? C("SESSION_REDIS_SELECT") : $this->config['select'];
|
||
|
$this->config['expire'] = C("SESSION_REDIS_EXPIRE") ? C("SESSION_REDIS_EXPIRE") : $this->config['expire'];
|
||
|
$this->config['session_name'] = C('SESSION_PREFIX') ? C('SESSION_PREFIX') : $this->config['session_name'];
|
||
|
$this->config['timeout'] = C('SESSION_CACHE_TIMEOUT') ? C('SESSION_CACHE_TIMEOUT') : $this->config['timeout'];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 打开Session
|
||
|
* @access public
|
||
|
* @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;
|
||
|
// 建立连接
|
||
|
$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 (0 != $this->config['select']) {
|
||
|
$this->handler->select($this->config['select']);
|
||
|
}
|
||
|
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
|
||
|
* @return string
|
||
|
*/
|
||
|
public function read($sessID)
|
||
|
{
|
||
|
return (string)$this->handler->get($this->config['session_name'] . $sessID);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 写入Session
|
||
|
* @access public
|
||
|
* @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);
|
||
|
} else {
|
||
|
return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 删除Session
|
||
|
* @access public
|
||
|
* @param string $sessID
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function destroy($sessID)
|
||
|
{
|
||
|
return $this->handler->delete($this->config['session_name'] . $sessID) > 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Session 垃圾回收
|
||
|
* @access public
|
||
|
* @param string $sessMaxLifeTime
|
||
|
* @return bool
|
||
|
*/
|
||
|
public function gc($sessMaxLifeTime)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
}
|