控制台应用,yzncms本身基于tp5.1框架,里面的队列用不了,bug,坑
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Redis.php 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\member\library\token\driver;
  12. use app\member\library\token\Driver;
  13. /**
  14. * Redis缓存驱动,适合单机部署、有前端代理实现高可用的场景,性能最好
  15. * 有需要在业务层实现读写分离、或者使用RedisCluster的需求,请使用Redisd驱动
  16. *
  17. * 要求安装phpredis扩展:https://github.com/nicolasff/phpredis
  18. * @author 尘缘 <130775@qq.com>
  19. */
  20. class Redis extends Driver
  21. {
  22. protected $options = [
  23. 'host' => '127.0.0.1',
  24. 'port' => 6379,
  25. 'password' => '',
  26. 'select' => 0,
  27. 'timeout' => 0,
  28. 'expire' => 0,
  29. 'persistent' => false,
  30. 'userprefix' => 'up:',
  31. 'tokenprefix' => 'tp:',
  32. ];
  33. /**
  34. * 构造函数
  35. * @param array $options 缓存参数
  36. * @throws \BadFunctionCallException
  37. * @access public
  38. */
  39. public function __construct($options = [])
  40. {
  41. if (!extension_loaded('redis')) {
  42. throw new \BadFunctionCallException('not support: redis');
  43. }
  44. if (!empty($options)) {
  45. $this->options = array_merge($this->options, $options);
  46. }
  47. $this->handler = new \Redis;
  48. if ($this->options['persistent']) {
  49. $this->handler->pconnect($this->options['host'], $this->options['port'], $this->options['timeout'], 'persistent_id_' . $this->options['select']);
  50. } else {
  51. $this->handler->connect($this->options['host'], $this->options['port'], $this->options['timeout']);
  52. }
  53. if ('' != $this->options['password']) {
  54. $this->handler->auth($this->options['password']);
  55. }
  56. if (0 != $this->options['select']) {
  57. $this->handler->select($this->options['select']);
  58. }
  59. }
  60. /**
  61. * 获取加密后的Token
  62. * @param string $token Token标识
  63. * @return string
  64. */
  65. protected function getEncryptedToken($token)
  66. {
  67. $config = \think\facade\Config::get('token.');
  68. return $this->options['tokenprefix'] . hash_hmac($config['hashalgo'], $token, $config['key']);
  69. }
  70. /**
  71. * 获取会员的key
  72. * @param $user_id
  73. * @return string
  74. */
  75. protected function getUserKey($user_id)
  76. {
  77. return $this->options['userprefix'] . $user_id;
  78. }
  79. /**
  80. * 存储Token
  81. * @param string $token Token
  82. * @param int $user_id 会员ID
  83. * @param int $expire 过期时长,0表示无限,单位秒
  84. * @return bool
  85. */
  86. public function set($token, $user_id, $expire = 0)
  87. {
  88. if (is_null($expire)) {
  89. $expire = $this->options['expire'];
  90. }
  91. if ($expire instanceof \DateTime) {
  92. $expire = $expire->getTimestamp() - time();
  93. }
  94. $key = $this->getEncryptedToken($token);
  95. if ($expire) {
  96. $result = $this->handler->setex($key, $expire, $user_id);
  97. } else {
  98. $result = $this->handler->set($key, $user_id);
  99. }
  100. //写入会员关联的token
  101. $this->handler->sAdd($this->getUserKey($user_id), $key);
  102. return $result;
  103. }
  104. /**
  105. * 获取Token内的信息
  106. * @param string $token
  107. * @return array
  108. */
  109. public function get($token)
  110. {
  111. $key = $this->getEncryptedToken($token);
  112. $value = $this->handler->get($key);
  113. if (is_null($value) || false === $value) {
  114. return [];
  115. }
  116. //获取有效期
  117. $expire = $this->handler->ttl($key);
  118. $expire = $expire < 0 ? 365 * 86400 : $expire;
  119. $expiretime = time() + $expire;
  120. //解决使用redis方式储存token时api接口Token刷新与检测因expires_in拼写错误报错的BUG
  121. $result = ['token' => $token, 'user_id' => $value, 'expiretime' => $expiretime, 'expires_in' => $expire];
  122. return $result;
  123. }
  124. /**
  125. * 判断Token是否可用
  126. * @param string $token Token
  127. * @param int $user_id 会员ID
  128. * @return boolean
  129. */
  130. public function check($token, $user_id)
  131. {
  132. $data = self::get($token);
  133. return $data && $data['user_id'] == $user_id ? true : false;
  134. }
  135. /**
  136. * 删除Token
  137. * @param string $token
  138. * @return boolean
  139. */
  140. public function delete($token)
  141. {
  142. $data = $this->get($token);
  143. if ($data) {
  144. $key = $this->getEncryptedToken($token);
  145. $user_id = $data['user_id'];
  146. $this->handler->del($key);
  147. $this->handler->sRem($this->getUserKey($user_id), $key);
  148. }
  149. return true;
  150. }
  151. /**
  152. * 删除指定用户的所有Token
  153. * @param int $user_id
  154. * @return boolean
  155. */
  156. public function clear($user_id)
  157. {
  158. $keys = $this->handler->sMembers($this->getUserKey($user_id));
  159. $this->handler->del($this->getUserKey($user_id));
  160. $this->handler->del($keys);
  161. return true;
  162. }
  163. }