Redis.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\common\token\driver;
  3. use think\facade\Config;
  4. use think\Response;
  5. use BadFunctionCallException;
  6. use app\common\token\Driver;
  7. use think\exception\HttpResponseException;
  8. /**
  9. * @see Driver
  10. */
  11. class Redis extends Driver
  12. {
  13. /**
  14. * 默认配置
  15. * @var array
  16. */
  17. protected $options = [];
  18. /**
  19. * 构造函数
  20. * @access public
  21. * @param array $options 参数
  22. */
  23. public function __construct(array $options = [])
  24. {
  25. if (!extension_loaded('redis')) {
  26. throw new BadFunctionCallException('未安装redis扩展');
  27. }
  28. if (!empty($options)) {
  29. $this->options = array_merge($this->options, $options);
  30. }
  31. $this->handler = new \Redis;
  32. if ($this->options['persistent']) {
  33. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  34. } else {
  35. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  36. }
  37. if ('' != $this->options['password']) {
  38. $this->handler->auth($this->options['password']);
  39. }
  40. if (false !== $this->options['select']) {
  41. $this->handler->select($this->options['select']);
  42. }
  43. }
  44. public function set(string $token, string $type, array $userinfo, int $expire = null): bool
  45. {
  46. if (is_null($expire)) {
  47. $expire = $this->options['expire'];
  48. }
  49. $data =json_encode($userinfo,JSON_UNESCAPED_UNICODE);
  50. $result = $this->handler->set($type.":info:".$token,$data,$expire);
  51. return $result;
  52. }
  53. public function get(string $token, bool $expirationException = true): array
  54. {
  55. $data = $this->handler->get($token);
  56. if (is_null($data) || false === $data) {
  57. return [];
  58. }
  59. $this->handler->set($token, $data, $this->options['expire']);
  60. // 返回未加密的token给客户端使用
  61. return json_decode($data,true);
  62. }
  63. public function check(string $token, string $type, array $userinfo, bool $expirationException = true): bool
  64. {
  65. $data = $this->get($token, $expirationException);
  66. if (!$data || (!$expirationException && $data['expiretime'] && $data['expiretime'] <= time())) return false;
  67. return $data['type'] == $type && $data['user_id'] == $userinfo['id'];
  68. }
  69. public function delete(string $token): bool
  70. {
  71. $data = $this->get($token, false);
  72. if ($data) {
  73. $key = $this->getEncryptedToken($token);
  74. $user_id = $data['user_id'];
  75. $this->handler->del($key);
  76. $this->handler->sRem($this->getUserKey($user_id), $key);
  77. }
  78. return true;
  79. }
  80. public function clear(string $type, int $user_id): bool
  81. {
  82. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  83. $this->handler->del($this->getUserKey($user_id));
  84. $this->handler->del($keys);
  85. return true;
  86. }
  87. /**
  88. * 获取会员的key
  89. * @param $user_id
  90. * @return string
  91. */
  92. protected function getUserKey($user_id): string
  93. {
  94. return $this->options['userprefix'] . $user_id;
  95. }
  96. }