Thinkphp3.2 Redis缓存session
Thinkphpsession缓存没有redis类库
Redis.class.php放在Library/Think/Session/Driver/下:
<?php /**
* +----------------------------------------------------------------------
* | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
* +----------------------------------------------------------------------
* | Copyright (c) 2006-2013 http://thinkphp.cn All rights reserved.
* +----------------------------------------------------------------------
* | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
* +----------------------------------------------------------------------
* | @author guizhiming <sd2536888@163.com>
* +----------------------------------------------------------------------
* | @datetime 2014-4-1 14:11:24
* +----------------------------------------------------------------------
*/ namespace Think\Session\Driver; /**
* Redis Session驱动
* 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
* @category Think
* @package Session
* @subpackage Driver
* @author guizhiming <sd2536888@163.com>
* @version TP3.2~TP3.2.1
*/
class Redis { /**
* Redis句柄
*/
private $handler;
private $get_result; public function __construct(){
if ( !extension_loaded('redis') ) {
E(L('_NOT_SUPPERT_').':redis');
}
if(empty($options)) {
$options = array (
'host' => C('SESSION_REDIS_HOST') ? C('SESSION_REDIS_HOST') : '127.0.0.1',
'port' => C('SESSION_REDIS_PORT') ? C('SESSION_REDIS_PORT') : 6379,
'timeout' => C('SESSION_CACHE_TIME') ? C('SESSION_CACHE_TIME') : false,
'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : false,
'auth' => C('SESSION_REDIS_AUTH') ? C('SESSION_REDIS_AUTH') : false,
);
}
$options['host'] = explode(',', $options['host']);
$options['port'] = explode(',', $options['port']);
$options['auth'] = explode(',', $options['auth']);
foreach ($options['host'] as $key=>$value) {
if (!isset($options['port'][$key])) {
$options['port'][$key] = $options['port'][0];
}
if (!isset($options['auth'][$key])) {
$options['auth'][$key] = $options['auth'][0];
}
}
$this->options = $options;
$expire = C('SESSION_EXPIRE');
$this->options['expire'] = isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');;
$this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('SESSION_PREFIX');
$this->handler = new \Redis;
} /**
* 连接Redis服务端
* @access public
* @param bool $is_master : 是否连接主服务器
*/
public function connect($is_master = true) {
if ($is_master) {
$i = 0;
} else {
$count = count($this->options['host']);
if ($count == 1) {
$i = 0;
} else {
$i = rand(1, $count - 1); //多个从服务器随机选择
}
}
$func = $this->options['persistent'] ? 'pconnect' : 'connect';
try {
if ($this->options['timeout'] === false) {
$result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i]);
if (!$result)
throw new \Think\Exception('Redis Error', 100);
} else {
$result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i], $this->options['timeout']);
if (!$result)
throw new \Think\Exception('Redis Error', 101);
}
if ($this->options['auth'][$i]) {
$result = $this->handler->auth($this->options['auth'][$i]);
if (!$result) {
throw new \Think\Exception('Redis Error', 102);
}
}
} catch ( \Exception $e ) {
exit('Error Message:'.$e->getMessage().'<br>Error Code:'.$e->getCode().'');
}
} /**
* 打开Session
* @access public
* @param string $savePath
* @param mixed $sessName
*/
public function open($savePath, $sessName) {
return true;
} /**
* 关闭Session
* @access public
*/
public function close() {
if ($this->options['persistent'] == 'pconnect') {
$this->handler->close();
}
return true;
} /**
* 读取Session
* @access public
* @param string $sessID
*/
public function read($sessID) {
$this->connect(0);
$this->get_result = $this->handler->get($this->options['prefix'].$sessID);
return $this->get_result;
} /**
* 写入Session
* @access public
* @param string $sessID
* @param String $sessData
*/
public function write($sessID, $sessData) {
if (!$sessData || $sessData == $this->get_result) {
return true;
}
$this->connect(1);
$expire = $this->options['expire'];
$sessID = $this->options['prefix'].$sessID;
if(is_int($expire) && $expire > 0) {
$result = $this->handler->setex($sessID, $expire, $sessData);
$re = $result ? 'true' : 'false';
}else{
$result = $this->handler->set($sessID, $sessData);
$re = $result ? 'true' : 'false';
}
return $result;
} /**
* 删除Session
* @access public
* @param string $sessID
*/
public function destroy($sessID) {
$this->connect(1);
return $this->handler->delete($this->options['prefix'].$sessID);
} /**
* Session 垃圾回收
* @access public
* @param string $sessMaxLifeTime
*/
public function gc($sessMaxLifeTime) {
return true;
} /**
* 打开Session
* @access public
* @param string $savePath
* @param mixed $sessName
*/
public function execute() {
session_set_save_handler(
array(&$this, "open"),
array(&$this, "close"),
array(&$this, "read"),
array(&$this, "write"),
array(&$this, "destroy"),
array(&$this, "gc")
);
} public function __destruct() {
if ($this->options['persistent'] == 'pconnect') {
$this->handler->close();
}
session_write_close();
} }
同样加载配置文件:
//redis操作session
'SESSION_AUTO_START' => true, // 是否自动开启Session
'SESSION_TYPE' => 'Redis', //session类型
'SESSION_PERSISTENT' => 1, //是否长连接(对于php来说0和1都一样)
'SESSION_CACHE_TIME' => 1, //连接超时时间(秒)
'SESSION_EXPIRE' => 300, //session有效期(单位:秒) 0表示永久缓存
'SESSION_PREFIX' => 'sess_', //session前缀
'SESSION_REDIS_HOST' => '服务器地址', //分布式Redis,默认第一个为主服务器
'SESSION_REDIS_AUTH' => 'shgc@123456',
'SESSION_REDIS_PORT' => '', //端口,如果相同只填一个,用英文逗号分隔
引入类库:
use Think\Session\Driver\Redis;
代码:
$user = "哈哈哈";
$redis = new Redis();
$redis->write('user',$user);
echo $redis->read('user');
Thinkphp3.2 Redis缓存session的更多相关文章
- Django之使用redis缓存session,历史浏览记录,首页数据实现性能优化
Redis缓存session 配置Django缓存数据到redis中 # diango的缓存配置 CACHES = { "default": { "BACKEND&quo ...
- redis缓存+session 实现单点登录
一.单点登录介绍 单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系 ...
- thinkphp3.2.3 版本使用redis缓存的时候无法使用认证
我在使用thinkphp3.2.3的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的 $redis->auth这一步没有, ...
- 基于Redis缓存的Session共享(附源码)
基于Redis缓存的Session共享(附源码) 在上一篇文章中我们研究了Redis的安装及一些基本的缓存操作,今天我们就利用Redis缓存实现一个Session共享,基于.NET平台的Seesion ...
- thinkphp3.2.3 版本使用redis缓存添加认证
我在使用thinkphp3.2.3的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的 $redis->auth这一步没有, ...
- [转]在nodejs使用Redis缓存和查询数据及Session持久化(Express)
本文转自:https://blog.csdn.net/wellway/article/details/76176760 在之前的这篇文章 在ExpressJS(NodeJS)中设置二级域名跨域共享Co ...
- redis缓存web session
redis缓存web session 首先说下架构图.使用Redis作为会话服务器,统一管理Session.如图,集群里的WEB服务器共享存放在REDIS里面全部的客户端SESSION. 当然,反向代 ...
- 在nodejs使用Redis缓存和查询数据及Session持久化(Express)
在nodejs使用Redis缓存和查询数据及Session持久化(Express) https://segmentfault.com/a/1190000002488971
- django memcached/redis缓存 =====缓存session
全站使用 例如 博客等缓存,通过中间件实现全站缓存. 加缓存中间件,那么多中间件加在什么位置? 请求时:缓存加在中间件里的最后一个,比如一次经过1.2.3.4中间件,加在4 返回事:缓存加在中间件里的 ...
随机推荐
- Python 字典(Dictionary)Ⅱ
删除字典元素 能删单一的元素也能清空字典,清空只需一项操作. 显示删除一个字典用del命令,如下实例: 但这会引发一个异常,因http://www.xuanhe.net/为用del后字典不再存在: 注 ...
- 安装caffe碰到的坑(各种.so未找到)
./include/caffe/common.hpp:4:32: fatal error: boost/shared_ptr.hpp: 没有那个文件或目录 所有类似于上面的错误,都可以用如下格式来解决 ...
- “_MSC_VER”的不匹配项
近些年来vs更新步伐加快,深刻的感受到了技术成长学习的重要性. 另一方面,版本的更换,也带来了许多的问题.今天用2019打开以前2010的工程时就碰到了一个: 检测到“_MSC_VER”的不匹配项: ...
- awk-第一篇
awk [单独的编程语言解释器] 1.awk介绍 全称:Aho Weinberger Kernaighan三个人的首字母缩写: 1970年第一次出现在Unix机器上,后来在开源领域使用它: 所以,我们 ...
- JUnit——assertThat(acture,matcher)
使用hamcrest之前需要引入相关的jar包,包括hamcrest-core.1.3.jar和hamcrest-library-1.3.jar. 具体引入的方法为:右击JUnit工程——build ...
- 2019hdu多校 K-th Closest Distance
题目链接:Click here 大致题意:q次询问,每次询问你区间[L,R]中|p-ai|的值第k小的是多少 Solution: 直接找是很困难的,我们考虑二分答案,那么本题就十分简单了 我们对权值维 ...
- xwiki安装部署
环境介绍 http://aiushtha-mybook.stor.sinaapp.com/xwiki/xwiki%E4%BB%8E%E5%85%A5%E9%97%A8%E5%88%B0%E8%BF%9 ...
- php语法标识符
php语法标识符 一.总结 一句话总结: 常用<?php //这里写代码 ?>:其它要么不常用,要么需要开配置 二.PHP四大标识符(语法环境) 参考或转自:PHP四大标识符(语法环境) ...
- 搜索引擎算法研究专题七:Hilltop算法
搜索引擎算法研究专题七:Hilltop算法 2017年12月19日 ⁄ 搜索技术 ⁄ 共 1256字 ⁄ 字号 小 中 大 ⁄ 评论关闭 HillTop也是搜索引擎结果排序的专利,是Google工 ...
- rtmp协议分析
最近需要做一个rtmp服务器,着手分析一下rtmp协议,开干. rtmp握手 这个推荐一篇文章讲解得比较透彻http://blog.sina.com.cn/s/blog_676e11660102v8b ...