Redis.php 3.2 KB

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