Redis.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 SessionHandlerInterface;
  13. use think\Exception;
  14. class Redis implements SessionHandlerInterface
  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. if (extension_loaded('redis')) {
  43. $this->handler = new \Redis;
  44. // 建立连接
  45. $func = $this->config['persistent'] ? 'pconnect' : 'connect';
  46. $this->handler->$func($this->config['host'], $this->config['port'], $this->config['timeout']);
  47. if ('' != $this->config['password']) {
  48. $this->handler->auth($this->config['password']);
  49. }
  50. if (0 != $this->config['select']) {
  51. $this->handler->select($this->config['select']);
  52. }
  53. } elseif (class_exists('\Predis\Client')) {
  54. $params = [];
  55. foreach ($this->config as $key => $val) {
  56. if (in_array($key, ['aggregate', 'cluster', 'connections', 'exceptions', 'prefix', 'profile', 'replication'])) {
  57. $params[$key] = $val;
  58. unset($this->config[$key]);
  59. }
  60. }
  61. $this->handler = new \Predis\Client($this->config, $params);
  62. } else {
  63. throw new \BadFunctionCallException('not support: redis');
  64. }
  65. return true;
  66. }
  67. /**
  68. * 关闭Session
  69. * @access public
  70. */
  71. public function close()
  72. {
  73. $this->gc(ini_get('session.gc_maxlifetime'));
  74. $this->handler->close();
  75. $this->handler = null;
  76. return true;
  77. }
  78. /**
  79. * 读取Session
  80. * @access public
  81. * @param string $sessID
  82. * @return string
  83. */
  84. public function read($sessID)
  85. {
  86. return (string) $this->handler->get($this->config['session_name'] . $sessID);
  87. }
  88. /**
  89. * 写入Session
  90. * @access public
  91. * @param string $sessID
  92. * @param string $sessData
  93. * @return bool
  94. */
  95. public function write($sessID, $sessData)
  96. {
  97. if ($this->config['expire'] > 0) {
  98. $result = $this->handler->setex($this->config['session_name'] . $sessID, $this->config['expire'], $sessData);
  99. } else {
  100. $result = $this->handler->set($this->config['session_name'] . $sessID, $sessData);
  101. }
  102. return $result ? true : false;
  103. }
  104. /**
  105. * 删除Session
  106. * @access public
  107. * @param string $sessID
  108. * @return bool
  109. */
  110. public function destroy($sessID)
  111. {
  112. return $this->handler->del($this->config['session_name'] . $sessID) > 0;
  113. }
  114. /**
  115. * Session 垃圾回收
  116. * @access public
  117. * @param string $sessMaxLifeTime
  118. * @return bool
  119. */
  120. public function gc($sessMaxLifeTime)
  121. {
  122. return true;
  123. }
  124. /**
  125. * Redis Session 驱动的加锁机制
  126. * @access public
  127. * @param string $sessID 用于加锁的sessID
  128. * @param integer $timeout 默认过期时间
  129. * @return bool
  130. */
  131. public function lock($sessID, $timeout = 10)
  132. {
  133. if (null == $this->handler) {
  134. $this->open('', '');
  135. }
  136. $lockKey = 'LOCK_PREFIX_' . $sessID;
  137. // 使用setnx操作加锁
  138. $isLock = $this->handler->setnx($lockKey, 1);
  139. if ($isLock) {
  140. // 设置过期时间,防止死任务的出现
  141. $this->handler->expire($lockKey, $timeout);
  142. return true;
  143. }
  144. return false;
  145. }
  146. /**
  147. * Redis Session 驱动的解锁机制
  148. * @access public
  149. * @param string $sessID 用于解锁的sessID
  150. */
  151. public function unlock($sessID)
  152. {
  153. if (null == $this->handler) {
  154. $this->open('', '');
  155. }
  156. $this->handler->del('LOCK_PREFIX_' . $sessID);
  157. }
  158. }