当前位置:Gxlcms > PHP教程 > PHP实现redis存储session

PHP实现redis存储session

时间:2021-07-01 10:21:17 帮助过:12人阅读

一、首先实现SessionHandlerInterface(此接口PHP>5.4.0),如下

/**
 * 以db的方式存储Session
 */
namespace OC\Session;
class redisSession implements \SessionHandlerInterface{
/**
     * 保存Session的数据库表的信息
     */
private $_options = array(
'handler' => null, //数据库连接句柄
'host' => null,
'port' => null,
'lifeTime' => null,
);
/**
     * 构造函数
     * @param $options 设置信息数组
     */
public function __construct($options=array()){
if(!class_exists("redis", false)){
die("必须安装redis扩展");
}
if(!isset($options['lifeTime']) || $options['lifeTime'] <= 0){
$options['lifeTime'] = ini_get('Session.gc_maxlifetime');
}
$this->_options = array_merge($this->_options, $options);
}
/**
     * 开始使用该驱动的Session
     */
public function begin(){
if($this->_options['host'] === null ||
$this->_options['port'] === null ||
$this->_options['lifeTime'] === null
){
return false;
}
//设置Session处理函数
Session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destory'),
array($this, 'gc')
        );
}
/**
     * 自动开始回话或者Session_start()开始回话后第一个调用的函数
     * 类似于构造函数的作用
     * @param $savePath 默认的保存路径
     * @param $SessionName 默认的参数名,PHPSESSID
     */
public function open($savePath, $SessionName){
if(is_resource($this->_options['handler'])) return true;
//连接redis
$redisHandle = new \Redis();
$redisHandle->connect($this->_options['host'], $this->_options['port']);
if(!$redisHandle){
return false;
}
$this->_options['handler'] = $redisHandle;
$this->gc(null);
return true;
}
/**
     * 类似于析构函数,在write之后调用或者Session_write_close()函数之后调用
     */
public function close(){
return $this->_options['handler']->close();
}
/**
     * 读取Session信息
     * @param $SessionId 通过该Id唯一确定对应的Session数据
     * @return Session信息/空串
     */
public function read($SessionId){
return $this->_options['handler']->get($SessionId);
}
/**
     * 写入或者修改Session数据
     * @param $SessionId 要写入数据的Session对应的id
     * @param $SessionData 要写入的数据,已经序列化过了
     */
public function write($SessionId, $SessionData){
return $this->_options['handler']->setex($SessionId, $this->_options['lifeTime'], $SessionData);
}
/**
     * 主动销毁Session会话
     * @param $SessionId 要销毁的会话的唯一id
     */
public function destroy($SessionId){
return $this->_options['handler']->delete($SessionId) >= 1 ? true : false;
}
/**
     * 清理绘画中的过期数据
     * @param 有效期
     */
public function gc($lifeTime){
//获取所有Sessionid,让过期的释放掉
$this->_options['handler']->keys("*");
return true;
}
}
以上就是一个完整的实现接口的类,在此封装成一个文件
二、调用时使用方法:
Session_set_save_handler(\OC::$server->getRedisSession(),true);
register_shutdown_function('Session_write_close');Session_start();
也就是在Session_start()方法之前调用,相当于告诉Session存储位置变成redis存储,至此,以后使用的所有$_Session方法存储的数据都会自动放到redis中去,而存储用的key值就是Session_id().
三、解释:
\OC::$server->getRedisSession()

以上的调用对象的方法时使用了pimple容器存储对象并调用的方式。具体如下使用:

namespace OC;
class Server extends SimpleContainer implements IServerContainer {

在此server类中注册加入对象

$this->registerService('RedisSession',function (Server $c) { return new \OC\Session\redisSession( $c->getSystemConfig()->getValue('redis') ); });获取对象方式: /** * 新增的获取用户redisSession的方法 */ public function getRedisSession() { return $this->query('RedisSession'); }其实也就是
Session_set_save_handler(\OC::$server->getRedisSession(),true);中的
\OC::$server->getRedisSession()

就是上面一、中的类的对象

参考:http://www.tuicool.com/articles/yeeyume

以上就介绍了PHP实现redis存储session,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

人气教程排行