CacheService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace library\services;
  3. use think\facade\Cache as CacheStatic;
  4. /**
  5. * crmeb 缓存类
  6. * Class CacheService
  7. * @package library\services
  8. */
  9. class CacheService
  10. {
  11. /**
  12. * 标签名
  13. * @var string
  14. */
  15. protected static $globalCacheName = '_cached_xxlm';
  16. /**
  17. * 过期时间
  18. * @var int
  19. */
  20. protected static $expire;
  21. /**
  22. * 获取缓存过期时间
  23. * @param int|null $expire
  24. * @return int
  25. */
  26. protected static function getExpire(int $expire = null): int
  27. {
  28. if (self::$expire) {
  29. return (int)self::$expire;
  30. }
  31. $expire = !is_null($expire) ? $expire : 86400;
  32. if (!is_int($expire))
  33. $expire = (int)$expire;
  34. return self::$expire = $expire;
  35. }
  36. /**
  37. * 写入缓存
  38. * @param string $name 缓存名称
  39. * @param mixed $value 缓存值
  40. * @param int $expire 缓存时间,为0读取系统缓存时间
  41. * @return bool
  42. */
  43. public static function set(string $name, $value, int $expire = null): bool
  44. {
  45. return self::handler()->set($name, $value, self::getExpire($expire));
  46. }
  47. /**
  48. * 如果不存在则写入缓存
  49. * @param string $name
  50. * @param bool $default
  51. * @return mixed
  52. * @throws \throwable
  53. */
  54. public static function get(string $name, $default = false, int $expire = null)
  55. {
  56. return self::handler()->remember($name, $default, self::getExpire($expire));
  57. }
  58. /**
  59. * 删除缓存
  60. * @param string $name
  61. * @return bool
  62. * @throws \Psr\SimpleCache\InvalidArgumentException
  63. */
  64. public static function delete(string $name)
  65. {
  66. return CacheStatic::delete($name);
  67. }
  68. /**
  69. * 缓存句柄
  70. *
  71. * @return \think\cache\TagSet|CacheStatic
  72. */
  73. public static function handler()
  74. {
  75. return CacheStatic::tag(self::$globalCacheName);
  76. }
  77. /**
  78. * 清空缓存池
  79. * @return bool
  80. */
  81. public static function clear()
  82. {
  83. return self::handler()->clear();
  84. }
  85. /**
  86. * Redis缓存句柄
  87. *
  88. * @return \think\cache\TagSet|CacheStatic
  89. */
  90. public static function redisHandler(string $type = null)
  91. {
  92. if ($type) {
  93. return CacheStatic::store('redis')->tag($type);
  94. } else {
  95. return CacheStatic::store('redis');
  96. }
  97. }
  98. /**
  99. * 放入令牌桶
  100. * @param string $key
  101. * @param array $value
  102. * @param string $type
  103. * @return bool
  104. */
  105. public static function setTokenBucket(string $key, $value, $expire = null, string $type = 'admin')
  106. {
  107. try {
  108. $redisCahce = self::redisHandler($type);
  109. return $redisCahce->set($key, $value, $expire);
  110. } catch (\Throwable $e) {
  111. return false;
  112. }
  113. }
  114. /**
  115. * 清除所有令牌桶
  116. * @param string $type
  117. * @return bool
  118. */
  119. public static function clearTokenAll(string $type = 'admin')
  120. {
  121. try {
  122. return self::redisHandler($type)->clear();
  123. } catch (\Throwable $e) {
  124. return false;
  125. }
  126. }
  127. /**
  128. * 清除令牌桶
  129. * @param string $type
  130. * @return bool
  131. */
  132. public static function clearToken(string $key)
  133. {
  134. try {
  135. return self::redisHandler()->delete($key);
  136. } catch (\Throwable $e) {
  137. return false;
  138. }
  139. }
  140. /**
  141. * 查看令牌是否存在
  142. * @param string $key
  143. * @return bool
  144. */
  145. public static function hasToken(string $key)
  146. {
  147. try {
  148. return self::redisHandler()->has($key);
  149. } catch (\Throwable $e) {
  150. return false;
  151. }
  152. }
  153. /**
  154. * 获取token令牌桶
  155. * @param string $key
  156. * @return mixed|null
  157. * @throws \Psr\SimpleCache\InvalidArgumentException
  158. */
  159. public static function getTokenBucket(string $key)
  160. {
  161. try {
  162. return self::redisHandler()->get($key, null);
  163. } catch (\Throwable $e) {
  164. return null;
  165. }
  166. }
  167. }