Redis.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace app\common\library\token\driver;
  3. use Throwable;
  4. use BadFunctionCallException;
  5. use app\common\library\token\Driver;
  6. /**
  7. * @see Driver
  8. */
  9. class Redis extends Driver
  10. {
  11. /**
  12. * 默认配置
  13. * @var array
  14. */
  15. protected array $options = [];
  16. /**
  17. * Token 过期后缓存继续保留的时间(s)
  18. */
  19. protected int $expiredHold = 60 * 60 * 24 * 2;
  20. /**
  21. * 构造函数
  22. * @access public
  23. * @param array $options 参数
  24. * @throws Throwable
  25. */
  26. public function __construct(array $options = [])
  27. {
  28. if (!extension_loaded('redis')) {
  29. throw new BadFunctionCallException('未安装redis扩展');
  30. }
  31. if (!empty($options)) {
  32. $this->options = array_merge($this->options, $options);
  33. }
  34. $this->handler = new \Redis();
  35. if ($this->options['persistent']) {
  36. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  37. } else {
  38. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  39. }
  40. if ('' != $this->options['password']) {
  41. $this->handler->auth($this->options['password']);
  42. }
  43. if (false !== $this->options['select']) {
  44. $this->handler->select($this->options['select']);
  45. }
  46. }
  47. /**
  48. * @throws Throwable
  49. */
  50. public function set(string $token, string $type, int $userId, int $expire = null): bool
  51. {
  52. if (is_null($expire)) {
  53. $expire = $this->options['expire'];
  54. }
  55. $expireTime = $expire !== 0 ? time() + $expire : 0;
  56. $token = $this->getEncryptedToken($token);
  57. $tokenInfo = [
  58. 'token' => $token,
  59. 'type' => $type,
  60. 'user_id' => $userId,
  61. 'create_time' => time(),
  62. 'expire_time' => $expireTime,
  63. ];
  64. $tokenInfo = json_encode($tokenInfo, JSON_UNESCAPED_UNICODE);
  65. if ($expire) {
  66. $expire += $this->expiredHold;
  67. $result = $this->handler->setex($token, $expire, $tokenInfo);
  68. } else {
  69. $result = $this->handler->set($token, $tokenInfo);
  70. }
  71. $this->handler->sAdd($this->getUserKey($type, $userId), $token);
  72. return $result;
  73. }
  74. /**
  75. * @throws Throwable
  76. */
  77. public function get(string $token): array
  78. {
  79. $key = $this->getEncryptedToken($token);
  80. $data = $this->handler->get($key);
  81. if (is_null($data) || false === $data) {
  82. return [];
  83. }
  84. $data = json_decode($data, true);
  85. $data['token'] = $token; // 返回未加密的token给客户端使用
  86. $data['expires_in'] = $this->getExpiredIn($data['expire_time'] ?? 0); // 过期时间
  87. return $data;
  88. }
  89. /**
  90. * @throws Throwable
  91. */
  92. public function check(string $token, string $type, int $userId): bool
  93. {
  94. $data = $this->get($token);
  95. if (!$data || ($data['expire_time'] && $data['expire_time'] <= time())) return false;
  96. return $data['type'] == $type && $data['user_id'] == $userId;
  97. }
  98. /**
  99. * @throws Throwable
  100. */
  101. public function delete(string $token): bool
  102. {
  103. $data = $this->get($token);
  104. if ($data) {
  105. $key = $this->getEncryptedToken($token);
  106. $this->handler->del($key);
  107. $this->handler->sRem($this->getUserKey($data['type'], $data['user_id']), $key);
  108. }
  109. return true;
  110. }
  111. /**
  112. * @throws Throwable
  113. */
  114. public function clear(string $type, int $userId): bool
  115. {
  116. $userKey = $this->getUserKey($type, $userId);
  117. $keys = $this->handler->sMembers($userKey);
  118. $this->handler->del($userKey);
  119. $this->handler->del($keys);
  120. return true;
  121. }
  122. /**
  123. * 获取会员的key
  124. * @param $type
  125. * @param $userId
  126. * @return string
  127. */
  128. protected function getUserKey($type, $userId): string
  129. {
  130. return $this->options['prefix'] . $type . '-' . $userId;
  131. }
  132. }