CacheService.php 4.1 KB

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