1. Thinkphp\Library\Think\Session\Driver中新建redis缓存文件:Redis.class.php
  2. Thinkphp\Common\function.php function session($name='',$value='') //session说明文件
  3.  
  4. 一:配置文件中新加:
  5. //redis操作session
  6. 'SESSION_AUTO_START' => true, // 是否自动开启Session
  7. 'SESSION_TYPE' => 'Redis', //session类型
  8. 'SESSION_PERSISTENT' => , //是否长连接(对于php来说0和1都一样)
  9. 'SESSION_CACHE_TIME' => , //连接超时时间(秒)
  10. 'SESSION_EXPIRE' => , //session有效期(单位:秒) 0表示永久缓存
  11. 'SESSION_PREFIX' => 'sses_', //session前缀
  12.  
  13. 二:Redis.class.php中内容:
  14. <?php
  15.  
  16. namespace Think\Session\Driver;
  17.  
  18. /**
  19. * Redis Session驱动
  20. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  21. * @category Think
  22. * @package Session
  23. * @subpackage Driver
  24. * @version TP3.2~TP3.2.1
  25. */
  26. class Redis implements \SessionHandlerInterface{
  27.  
  28. /**
  29. * Redis句柄
  30. */
  31. private $handler;
  32. private $get_result;
  33.  
  34. public function __construct(){
  35. if ( !extension_loaded('redis') ) {
  36. E(L('_NOT_SUPPERT_').':redis');
  37. }
  38. if(empty($options)) {
  39. $options = array (
  40. 'host' => C('REDIS_HOST') ? C('REDIS_HOST') : '127.0.0.1',
  41. 'port' => C('REDIS_PORT') ? C('REDIS_PORT') : ,
  42. 'timeout' => C('SESSION_CACHE_TIME') ? C('SESSION_CACHE_TIME') : false,
  43. 'persistent' => C('SESSION_PERSISTENT') ? C('SESSION_PERSISTENT') : false,
  44. 'auth' => C('REDIS_AUTH') ? C('REDIS_AUTH') : false,
  45. 'dbindex' => C('REDIS_DBINDEX') ? C('REDIS_DBINDEX') : , //选择存库
  46. );
  47. }
  48. $options['host'] = explode(',', $options['host']);
  49. $options['port'] = explode(',', $options['port']);
  50. $options['auth'] = explode(',', $options['auth']);
  51.  
  52. foreach ($options['host'] as $key=>$value) {
  53. if (!isset($options['port'][$key])) {
  54. $options['port'][$key] = $options['port'][];
  55. }
  56. if (!isset($options['auth'][$key])) {
  57. $options['auth'][$key] = $options['auth'][];
  58. }
  59. }
  60. $this->options = $options;
  61. $expire = C('SESSION_EXPIRE');
  62. $this->options['expire'] = isset($expire) ? (int)$expire : (int)ini_get('session.gc_maxlifetime');;
  63. $this->options['prefix'] = isset($options['prefix']) ? $options['prefix'] : C('SESSION_PREFIX');
  64. $this->options['dbindex'] = isset($options['dbindex'])? $options['dbindex'] : ; //选择存库
  65.  
  66. $this->handler = new \Redis;
  67.  
  68. }
  69.  
  70. /**
  71. * 连接Redis服务端
  72. * @access public
  73. * @param bool $is_master : 是否连接主服务器
  74. */
  75. public function connect($is_master = true) {
  76. if ($is_master) {
  77. $i = ;
  78. } else {
  79. $count = count($this->options['host']);
  80. if ($count == ) {
  81. $i = ;
  82. } else {
  83. $i = rand(, $count - ); //多个从服务器随机选择
  84. }
  85. }
  86. $func = $this->options['persistent'] ? 'pconnect' : 'connect';
  87. try {
  88. if ($this->options['timeout'] === false) {
  89. $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i]);
  90. if (!$result)
  91. throw new \Think\Exception('Redis Error', );
  92. } else {
  93. $result = $this->handler->$func($this->options['host'][$i], $this->options['port'][$i], $this->options['timeout']);
  94. if (!$result)
  95. throw new \Think\Exception('Redis Error', );
  96. }
  97. if ($this->options['auth'][$i]) {
  98. $result = $this->handler->auth($this->options['auth'][$i]);
  99. if (!$result) {
  100. throw new \Think\Exception('Redis Error', );
  101. }
  102. }
  103. //选择db存库
  104. if ( != $this->options['dbindex']) {
  105. $this->handler->select($this->options['dbindex']);
  106. }
  107.  
  108. } catch ( \Exception $e ) {
  109. exit('Error Message:'.$e->getMessage().'<br>Error Code:'.$e->getCode().'');
  110. }
  111. }
  112.  
  113. /**
  114. * 打开Session
  115. * @access public
  116. * @param string $savePath
  117. * @param mixed $sessName
  118. */
  119. public function open($savePath, $sessName) {
  120. return true;
  121. }
  122.  
  123. /**
  124. * 关闭Session
  125. * @access public
  126. */
  127. public function close() {
  128. if ($this->options['persistent'] == 'pconnect') {
  129. $this->handler->close();
  130. }
  131. return true;
  132. }
  133.  
  134. /**
  135. * 读取Session
  136. * @access public
  137. * @param string $sessID
  138. */
  139. public function read($sessID) {
  140. $this->connect();
  141. $this->get_result = $this->handler->get($this->options['prefix'].$sessID);
  142. return $this->get_result;
  143. }
  144.  
  145. /**
  146. * 写入Session
  147. * @access public
  148. * @param string $sessID
  149. * @param String $sessData
  150. */
  151. public function write($sessID, $sessData) {
  152. if (!$sessData || $sessData == $this->get_result) {
  153. return true;
  154. }
  155. $this->connect();
  156. $expire = $this->options['expire'];
  157. $sessID = $this->options['prefix'].$sessID;
  158. if(is_int($expire) && $expire > ) {
  159. $result = $this->handler->setex($sessID, $expire, $sessData);
  160. $re = $result ? 'true' : 'false';
  161. }else{
  162. $result = $this->handler->set($sessID, $sessData);
  163. $re = $result ? 'true' : 'false';
  164. }
  165. return $result;
  166. }
  167.  
  168. /**
  169. * 删除Session
  170. * @access public
  171. * @param string $sessID
  172. */
  173. public function destroy($sessID) {
  174. $this->connect();
  175. return $this->handler->delete($this->options['prefix'].$sessID);
  176. }
  177.  
  178. /**
  179. * Session 垃圾回收
  180. * @access public
  181. * @param string $sessMaxLifeTime
  182. */
  183. public function gc($sessMaxLifeTime) {
  184. return true;
  185. }
  186.  
  187. /**
  188. * 打开Session
  189. * @access public
  190. * @param string $savePath
  191. * @param mixed $sessName
  192. */
  193. public function execute() {
  194. session_set_save_handler(
  195. array(&$this, "open"),
  196. array(&$this, "close"),
  197. array(&$this, "read"),
  198. array(&$this, "write"),
  199. array(&$this, "destroy"),
  200. array(&$this, "gc")
  201. );
  202. }
  203.  
  204. public function __destruct() {
  205. if ($this->options['persistent'] == 'pconnect') {
  206. $this->handler->close();
  207. }
  208. session_write_close();
  209. }
  210.  
  211. }

【原】thinkphp实现存储session至redis的更多相关文章

  1. PHP中使用Redis接管文件存储Session详解

    前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用redis替换文件来存储session. 最近就遇到了这个问题,之前找了网上的一套直播 ...

  2. PHP中如何使用Redis接管文件存储Session详解

    https://www.jb51.net/article/151580.htm 前言 php默认使用文件存储session,如果并发量大,效率会非常低.而redis对高并发的支持非常好,可以利用red ...

  3. (转)Tomcat7+Redis存储Session

    原创http://blog.csdn.net/caiwenfeng_for_23/article/details/45666831 PS:截止到2015-05-12前是不支持Tomcat8的,详情见官 ...

  4. redis/分布式文件存储系统/数据库 存储session,解决负载均衡集群中session不一致问题

    先来说下session和cookie的异同 session和cookie不仅仅是一个存放在服务器端,一个存放在客户端那么笼统 session虽然存放在服务器端,但是也需要和客户端相互匹配,试想一个浏览 ...

  5. redis 存储session实现session共享

    nginx 作为代理 tomcat集群 redis存储共享session nginx采用轮询方式将动态请求反向代理给tomcat,tomcat通过加载相应jar包方式实现获得redis中共享的sess ...

  6. Tomcat 使用Redis存储Session

    Tomcat Redis Session Github 地址. 下载 commons-pool2-2.2.jar,jedis-2.5.2.jar,tomcat-redis-session-manage ...

  7. Asp.net Core 使用Redis存储Session

    前言 Asp.net Core 改变了之前的封闭,现在开源且开放,下面我们来用Redis存储Session来做一个简单的测试,或者叫做中间件(middleware). 对于Session来说褒贬不一, ...

  8. PHP中Redis替代文件存储Session语句

    php默认使用文件存储session,如果并发量大,效率非常低.而Redis对高并发的支持非常好,所以,可以使用redis替代文件存储session. 这里,介绍下php的 session_set_s ...

  9. Tomcat7+Redis存储Session(转)

    PS:截止到2015-05-12前是不支持Tomcat8的,详情见官网:https://github.com/jcoleman/tomcat-redis-session-manager 前提:你已经部 ...

随机推荐

  1. Python爬虫开发【第1篇】【beautifulSoup4解析器】

    CSS 选择器:BeautifulSoup4 Beautiful Soup 也是一个HTML/XML的解析器,主要的功能也是如何解析和提取 HTML/XML 数据. pip 安装:pip instal ...

  2. 使用-Wl直接向ld传递参数

    gcc -Wl, key1, value1, key2, value2, key3, value3 包括-Wl在内全部都是以逗号分隔. 上面等价于: ld key1=value1 key2=value ...

  3. H264--5--H264解码[8]

    原文:http://blog.csdn.net/yangzhongxuan/article/details/8003547 解码器在解码时,首先逐个字节读取NAL的数据,统计NAL的长度,然后再开始解 ...

  4. 【POJ 1364】 King

    [题目链接] 点击打开链接 [算法] 差分约束系统 [代码] #include <algorithm> #include <bitset> #include <cctyp ...

  5. POJ3709 K-Anonymous Sequence 斜率优化DP

    POJ3709 题意很简单 给n个递增整数(n<=500000)和一种操作(选择任意个数 使他们减少整数值) 使得对于所有的整数 在数列中 有k个相等的数 O(n^2)的DP方程很容易得出 如下 ...

  6. KeepAlived的实现示例

    KeepAlived的实现示例 KeepAlived的实现 HA Cluster配置准备: 各节点时间必须同步 ntp(6), chrony(7) 1>在centos6上 ntpdate 172 ...

  7. 51nod2006 飞行员配对(二分图最大匹配)

    2006 飞行员配对(二分图最大匹配) 题目来源: 网络流24题 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 第二次世界大战时期,英国皇家空军从沦陷国 ...

  8. 洛谷 P2881 [USACO07MAR]排名的牛Ranking the Cows

    题应该是假的...先不做了 https://www.cnblogs.com/Blue233333/p/7249057.html 比如输入5 0,答案是10,但可以比较8次就出来.就是在一个已知有序数列 ...

  9. Hadoop Hive概念学习系列之hive的正则表达式初步(六)

    说在前面的话 hive的正则表达式,是非常重要!作为大数据开发人员,用好hive,正则表达式,是必须品! Hive中的正则表达式还是很强大的.数据工作者平时也离不开正则表达式.对此,特意做了个hive ...

  10. cocos creator 场景如何透明,多个canvas层级显示

    转载地址:https://forum.cocos.com/t/creator-canvas/55373/14 Creator 版本:1.7 目标平台:WEB MOBILE 项目需要,页面做了多个Can ...