【原】thinkphp实现存储session至redis
- Thinkphp\Library\Think\Session\Driver中新建redis缓存文件:Redis.class.php
- Thinkphp\Common\function.php 中 function session($name='',$value='') //session说明文件
- 一:配置文件中新加:
- //redis操作session
- 'SESSION_AUTO_START' => true, // 是否自动开启Session
- 'SESSION_TYPE' => 'Redis', //session类型
- 'SESSION_PERSISTENT' => , //是否长连接(对于php来说0和1都一样)
- 'SESSION_CACHE_TIME' => , //连接超时时间(秒)
- 'SESSION_EXPIRE' => , //session有效期(单位:秒) 0表示永久缓存
- 'SESSION_PREFIX' => 'sses_', //session前缀
- 二:Redis.class.php中内容:
- <?php
- namespace Think\Session\Driver;
- /**
- * Redis Session驱动
- * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
- * @category Think
- * @package Session
- * @subpackage Driver
- * @version TP3.2~TP3.2.1
- */
- class Redis implements \SessionHandlerInterface{
- /**
- * 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('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
- 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : ,
- 'timeout' => C('SESSION_CACHE_TIME') ? C('SESSION_CACHE_TIME') : false,
- 'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : false,
- 'auth' => C('REDIS_AUTH') ? C('REDIS_AUTH') : false,
- 'dbindex' => C('REDIS_DBINDEX') ? C('REDIS_DBINDEX') : , //选择存库
- );
- }
- $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'][];
- }
- if (!isset($options['auth'][$key])) {
- $options['auth'][$key] = $options['auth'][];
- }
- }
- $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->options['dbindex'] = isset($options['dbindex'])? $options['dbindex'] : ; //选择存库
- $this->handler = new \Redis;
- }
- /**
- * 连接Redis服务端
- * @access public
- * @param bool $is_master : 是否连接主服务器
- */
- public function connect($is_master = true) {
- if ($is_master) {
- $i = ;
- } else {
- $count = count($this->options['host']);
- if ($count == ) {
- $i = ;
- } else {
- $i = rand(, $count - ); //多个从服务器随机选择
- }
- }
- $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', );
- } else {
- $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i], $this->options['timeout']);
- if (!$result)
- throw new \Think\Exception('Redis Error', );
- }
- if ($this->options['auth'][$i]) {
- $result = $this->handler->auth($this->options['auth'][$i]);
- if (!$result) {
- throw new \Think\Exception('Redis Error', );
- }
- }
- //选择db存库
- if ( != $this->options['dbindex']) {
- $this->handler->select($this->options['dbindex']);
- }
- } 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();
- $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();
- $expire = $this->options['expire'];
- $sessID = $this->options['prefix'].$sessID;
- if(is_int($expire) && $expire > ) {
- $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();
- 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();
- }
- }
【原】thinkphp实现存储session至redis的更多相关文章
- PHP中使用Redis接管文件存储Session详解
前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用redis替换文件来存储session. 最近就遇到了这个问题,之前找了网上的一套直播 ...
- PHP中如何使用Redis接管文件存储Session详解
https://www.jb51.net/article/151580.htm 前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用red ...
- (转)Tomcat7+Redis存储Session
原创http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831 PS:截止到2015-05-12前是不支持Tomcat8的,详情见官 ...
- redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题
先来说下session和cookie的异同 session和cookie不仅仅是一个存放在服务器端,一个存放在客户端那么笼统 session虽然存放在服务器端,但是也需要和客户端相互匹配,试想一个浏览 ...
- redis 存储session实现session共享
nginx 作为代理 tomcat集群 redis存储共享session nginx采用轮询方式将动态请求反向代理给tomcat,tomcat通过加载相应jar包方式实现获得redis中共享的sess ...
- Tomcat 使用Redis存储Session
Tomcat Redis Session Github 地址. 下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manage ...
- Asp.net Core 使用Redis存储Session
前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...
- PHP中Redis替代文件存储Session语句
php默认使用文件存储session,如果并发量大,效率非常低.而Redis对高并发的支持非常好,所以,可以使用redis替代文件存储session. 这里,介绍下php的 session_set_s ...
- Tomcat7+Redis存储Session(转)
PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager 前提:你已经部 ...
随机推荐
- Python爬虫开发【第1篇】【beautifulSoup4解析器】
CSS 选择器:BeautifulSoup4 Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. pip 安装:pip instal ...
- 使用-Wl直接向ld传递参数
gcc -Wl, key1, value1, key2, value2, key3, value3 包括-Wl在内全部都是以逗号分隔. 上面等价于: ld key1=value1 key2=value ...
- H264--5--H264解码[8]
原文:http://blog.csdn.net/yangzhongxuan/article/details/8003547 解码器在解码时,首先逐个字节读取NAL的数据,统计NAL的长度,然后再开始解 ...
- 【POJ 1364】 King
[题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ...
- POJ3709 K-Anonymous Sequence 斜率优化DP
POJ3709 题意很简单 给n个递增整数(n<=500000)和一种操作(选择任意个数 使他们减少整数值) 使得对于所有的整数 在数列中 有k个相等的数 O(n^2)的DP方程很容易得出 如下 ...
- KeepAlived的实现示例
KeepAlived的实现示例 KeepAlived的实现 HA Cluster配置准备: 各节点时间必须同步 ntp(6), chrony(7) 1>在centos6上 ntpdate 172 ...
- 51nod2006 飞行员配对(二分图最大匹配)
2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 第二次世界大战时期,英国皇家空军从沦陷国 ...
- 洛谷 P2881 [USACO07MAR]排名的牛Ranking the Cows
题应该是假的...先不做了 https://www.cnblogs.com/Blue233333/p/7249057.html 比如输入5 0,答案是10,但可以比较8次就出来.就是在一个已知有序数列 ...
- Hadoop Hive概念学习系列之hive的正则表达式初步(六)
说在前面的话 hive的正则表达式,是非常重要!作为大数据开发人员,用好hive,正则表达式,是必须品! Hive中的正则表达式还是很强大的.数据工作者平时也离不开正则表达式.对此,特意做了个hive ...
- cocos creator 场景如何透明,多个canvas层级显示
转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...