Redis.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #[\ReturnTypeWillChange]
  41. public function open($savePath, $sessName)
  42. {
  43. // 检测php环境
  44. if (!extension_loaded('redis')) {
  45. throw new Exception('not support:redis');
  46. }
  47. $this->handler = new \Redis;
  48. // 建立连接
  49. $func = $this->config['persistent'] ? 'pconnect' : 'connect';
  50. $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
  51. if ('' != $this->config['password']) {
  52. $this->handler->auth($this->config['password']);
  53. }
  54. if (0 != $this->config['select']) {
  55. $this->handler->select($this->config['select']);
  56. }
  57. return true;
  58. }
  59. /**
  60. * 关闭Session
  61. * @access public
  62. */
  63. #[\ReturnTypeWillChange]
  64. public function close()
  65. {
  66. $this->gc(ini_get('session.gc_maxlifetime'));
  67. $this->handler->close();
  68. $this->handler = null;
  69. return true;
  70. }
  71. /**
  72. * 读取Session
  73. * @access public
  74. * @param string $sessID
  75. * @return string
  76. */
  77. #[\ReturnTypeWillChange]
  78. public function read($sessID)
  79. {
  80. return (string) $this->handler->get($this->config['session_name'] . $sessID);
  81. }
  82. /**
  83. * 写入Session
  84. * @access public
  85. * @param string $sessID
  86. * @param string $sessData
  87. * @return bool
  88. */
  89. #[\ReturnTypeWillChange]
  90. public function write($sessID, $sessData)
  91. {
  92. if ($this->config['expire'] > 0) {
  93. return $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
  94. } else {
  95. return $this->handler->set($this->config['session_name'] . $sessID, $sessData);
  96. }
  97. }
  98. /**
  99. * 删除Session
  100. * @access public
  101. * @param string $sessID
  102. * @return bool
  103. */
  104. #[\ReturnTypeWillChange]
  105. public function destroy($sessID)
  106. {
  107. return $this->handler->del($this->config['session_name'] . $sessID) > 0;
  108. }
  109. /**
  110. * Session 垃圾回收
  111. * @access public
  112. * @param string $sessMaxLifeTime
  113. * @return bool
  114. */
  115. #[\ReturnTypeWillChange]
  116. public function gc($sessMaxLifeTime)
  117. {
  118. return true;
  119. }
  120. }