Redis.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\session\driver;
  12. use SessionHandler;
  13. use think\Exception;
  14. class Redis extends SessionHandler
  15. {
  16. /** @var \Redis */
  17. protected $handler = null;
  18. protected $config = [
  19. 'host' => '127.0.0.1', // redis主机
  20. 'port' => 6379, // redis端口
  21. 'password' => '', // 密码
  22. 'select' => 0, // 操作库
  23. 'expire' => 3600, // 有效期(秒)
  24. 'timeout' => 0, // 超时时间(秒)
  25. 'persistent' => true, // 是否长连接
  26. 'session_name' => '', // sessionkey前缀
  27. ];
  28. public function __construct($config = [])
  29. {
  30. $this->config = array_merge($this->config, $config);
  31. }
  32. /**
  33. * 打开Session
  34. * @access public
  35. * @param string $savePath
  36. * @param mixed $sessName
  37. * @return bool
  38. * @throws Exception
  39. */
  40. public function open($savePath, $sessName)
  41. {
  42. // 检测php环境
  43. if (!extension_loaded('redis')) {
  44. throw new Exception('not support:redis');
  45. }
  46. $this->handler = new \Redis;
  47. // 建立连接
  48. $func = $this->config['persistent'] ? 'pconnect' : 'connect';
  49. $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
  50. if ('' != $this->config['password']) {
  51. $this->handler->auth($this->config['password']);
  52. }
  53. if (0 != $this->config['select']) {
  54. $this->handler->select($this->config['select']);
  55. }
  56. return true;
  57. }
  58. /**
  59. * 关闭Session
  60. * @access public
  61. */
  62. public function close()
  63. {
  64. $this->gc(ini_get('session.gc_maxlifetime'));
  65. $this->handler->close();
  66. $this->handler = null;
  67. return true;
  68. }
  69. /**
  70. * 读取Session
  71. * @access public
  72. * @param string $sessID
  73. * @return string
  74. */
  75. public function read($sessID)
  76. {
  77. return (string) $this->handler->get($this->config['session_name'] . $sessID);
  78. }
  79. /**
  80. * 写入Session
  81. * @access public
  82. * @param string $sessID
  83. * @param String $sessData
  84. * @return bool
  85. */
  86. public function write($sessID, $sessData)
  87. {
  88. if ($this->config['expire'] > 0) {
  89. return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
  90. } else {
  91. return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
  92. }
  93. }
  94. /**
  95. * 删除Session
  96. * @access public
  97. * @param string $sessID
  98. * @return bool
  99. */
  100. public function destroy($sessID)
  101. {
  102. return $this->handler->del($this->config['session_name'] . $sessID) > 0;
  103. }
  104. /**
  105. * Session 垃圾回收
  106. * @access public
  107. * @param string $sessMaxLifeTime
  108. * @return bool
  109. */
  110. public function gc($sessMaxLifeTime)
  111. {
  112. return true;
  113. }
  114. }