CacheService.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services;
  12. use think\facade\Cache;
  13. use think\facade\Config;
  14. use think\cache\TagSet;
  15. /**
  16. * CRMEB 缓存类
  17. * Class CacheService
  18. * @package crmeb\services
  19. */
  20. class CacheService
  21. {
  22. /**
  23. * 过期时间
  24. * @var int
  25. */
  26. protected static $expire;
  27. /**
  28. * 写入缓存
  29. * @param string $name 缓存名称
  30. * @param mixed $value 缓存值
  31. * @param int|null $expire 缓存时间,为0读取系统缓存时间
  32. */
  33. public static function set(string $name, $value, int $expire = 0, string $tag = 'crmeb')
  34. {
  35. try {
  36. return Cache::tag($tag)->set($name, $value, $expire);
  37. } catch (\Throwable $e) {
  38. return false;
  39. }
  40. }
  41. /**
  42. * 如果不存在则写入缓存
  43. * @param string $name
  44. * @param mixed $default
  45. * @param int|null $expire
  46. * @param string $tag
  47. * @return mixed|string|null
  48. */
  49. public static function remember(string $name, $default = '', int $expire = 0, string $tag = 'crmeb')
  50. {
  51. try {
  52. return Cache::tag($tag)->remember($name, $default, $expire);
  53. } catch (\Throwable $e) {
  54. try {
  55. if (is_callable($default)) {
  56. return $default();
  57. } else {
  58. return $default;
  59. }
  60. } catch (\Throwable $e) {
  61. return null;
  62. }
  63. }
  64. }
  65. /**
  66. * 读取缓存
  67. * @param string $name
  68. * @param mixed $default
  69. * @return mixed|string
  70. */
  71. public static function get(string $name, $default = '')
  72. {
  73. return Cache::get($name) ?? $default;
  74. }
  75. /**
  76. * 删除缓存
  77. * @param string $name
  78. * @return bool
  79. */
  80. public static function delete(string $name)
  81. {
  82. return Cache::delete($name);
  83. }
  84. /**
  85. * 清空缓存池
  86. * @return bool
  87. */
  88. public static function clear(string $tag = 'crmeb')
  89. {
  90. return Cache::tag($tag)->clear();
  91. }
  92. /**
  93. * 清空全部缓存
  94. * @return bool
  95. * @author 吴汐
  96. * @email 442384644@qq.com
  97. * @date 2023/12/19
  98. */
  99. public static function clearAll()
  100. {
  101. return Cache::clear();
  102. }
  103. /**
  104. * 检查缓存是否存在
  105. * @param string $key
  106. * @return bool
  107. */
  108. public static function has(string $key)
  109. {
  110. try {
  111. return Cache::has($key);
  112. } catch (\Throwable $e) {
  113. return false;
  114. }
  115. }
  116. /**
  117. * 指定缓存类型
  118. * @param string $type
  119. * @param string $tag
  120. * @return TagSet
  121. */
  122. public static function store(string $type = 'file', string $tag = 'crmeb')
  123. {
  124. return Cache::store($type)->tag($tag);
  125. }
  126. /**
  127. * 检查锁
  128. * @param string $key
  129. * @param int $timeout
  130. * @return bool
  131. */
  132. public static function setMutex(string $key, int $timeout = 10): bool
  133. {
  134. $curTime = time();
  135. $readMutexKey = "redis:mutex:{$key}";
  136. $mutexRes = Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
  137. if ($mutexRes) {
  138. return true;
  139. }
  140. //就算意外退出,下次进来也会检查key,防止死锁
  141. $time = Cache::store('redis')->handler()->get($readMutexKey);
  142. if ($curTime > $time) {
  143. Cache::store('redis')->handler()->del($readMutexKey);
  144. return Cache::store('redis')->handler()->setnx($readMutexKey, $curTime + $timeout);
  145. }
  146. return false;
  147. }
  148. /**
  149. * 删除锁
  150. * @param string $key
  151. * @author 等风来
  152. * @email 136327134@qq.com
  153. * @date 2022/11/22
  154. */
  155. public static function delMutex(string $key)
  156. {
  157. $readMutexKey = "redis:mutex:{$key}";
  158. Cache::store('redis')->handler()->del($readMutexKey);
  159. }
  160. /**
  161. * 数据库锁
  162. * @param $key
  163. * @param $fn
  164. * @param int $ex
  165. * @return mixed
  166. * @author 吴汐
  167. * @email 442384644@qq.com
  168. * @date 2023/03/01
  169. */
  170. public static function lock($key, $fn, int $ex = 6)
  171. {
  172. if (Config::get('cache.default') == 'file') {
  173. return $fn();
  174. }
  175. return app()->make(LockService::class)->exec($key, $fn, $ex);
  176. }
  177. }