CacheService.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\cache\driver\Redis;
  13. use think\facade\Cache;
  14. use think\facade\Config;
  15. /**
  16. * crmeb 缓存类
  17. * Class CacheService
  18. * @package crmeb\services
  19. * @mixin \Redis
  20. */
  21. class CacheService
  22. {
  23. const TAG_TOPIC = 'topic';
  24. const TAG_CONFIG = 'config';
  25. const TAG_COMMUNITY = 'community';
  26. const TAG_BRAND = 'brand';
  27. const TAG_CATEGORY = 'category';
  28. const TAG_GROUP_DATA = 'group_data';
  29. const TAG_MERCHANT = 'merchant';
  30. protected $handler;
  31. protected $tag;
  32. protected $type;
  33. /**
  34. * @param int $admin
  35. * @param string $tag
  36. */
  37. public function __construct($type, $tag)
  38. {
  39. $key = config('app.app_key');
  40. $tagLst = ['__cache_' . $key];
  41. if ($type) {
  42. $tagLst[] = '__cache_mer_' . $key . '_' . $type;
  43. $tagLst[] = '__cache_mer_' . $key;
  44. } else {
  45. $tagLst[] = '__cache_sys_' . $key;
  46. }
  47. if ($tag) {
  48. $tagLst[] = '__cache_tag_' . $key . '_' . $type . '_' . $tag;
  49. }
  50. $this->tag = $tag;
  51. $this->type = $type;
  52. $this->handler = Cache::store('file')->tag($tagLst);
  53. }
  54. public static function create($admin, $tag)
  55. {
  56. return new static($admin, $tag);
  57. }
  58. /**
  59. * 清除所以缓存
  60. */
  61. public static function clearAll()
  62. {
  63. Cache::store('file')->tag('__cache_' . config('app.app_key'))->clear();
  64. }
  65. /**
  66. * 清除商户缓存
  67. */
  68. public static function clearMerchantAll()
  69. {
  70. Cache::store('file')->tag('__cache_mer_' . config('app.app_key'))->clear();
  71. }
  72. /**
  73. * 清除平台缓存
  74. */
  75. public static function clearSystem()
  76. {
  77. Cache::store('file')->tag('__cache_sys_' . config('app.app_key'))->clear();
  78. }
  79. /**
  80. * @param int $merId
  81. * 清除指定商户缓存
  82. */
  83. public static function clearMerchant($merId)
  84. {
  85. Cache::store('file')->tag('__cache_mer_' . config('app.app_key') . '_' . $merId)->clear();
  86. }
  87. /**
  88. * 根据tag清除缓存
  89. * @param $merId
  90. * @param $tag
  91. */
  92. public static function clearByTag($merId, $tag)
  93. {
  94. Cache::store('file')->tag('__cache_tag_' . config('app.app_key') . '_' . $merId . '_' . $tag)->clear();
  95. }
  96. public static function delete($key)
  97. {
  98. Cache::store('file')->delete($key);
  99. }
  100. /**
  101. * @param $key
  102. * @return string
  103. * 生成 key
  104. */
  105. public function cacheKey($key)
  106. {
  107. if (is_array($key)) {
  108. $key = json_encode($key, JSON_UNESCAPED_UNICODE);
  109. }
  110. return '__sys_cache_' . config('app.app_key') . $this->type . $this->tag . $key;
  111. }
  112. /**
  113. * @param string|array $key
  114. * @param $cache
  115. * @param int $expire
  116. */
  117. public function set($key, $cache, $expire = 3600)
  118. {
  119. $this->handler->set($this->cacheKey($key), $cache, $expire);
  120. }
  121. /**
  122. * @param string|array $key
  123. * @param null $default
  124. * @return mixed
  125. */
  126. public function get($key, $default = null)
  127. {
  128. return $this->handler->get($this->cacheKey($key), $default);
  129. }
  130. /**
  131. * @param string|array $key
  132. * @return mixed
  133. */
  134. public function has($key)
  135. {
  136. return $this->handler->has($this->cacheKey($key));
  137. }
  138. /**
  139. * @param string|array $key
  140. * @param $value
  141. * @param int $expire
  142. * @return mixed
  143. */
  144. public function remember($key, $value, $expire = 3600)
  145. {
  146. return $this->handler->remember($this->cacheKey($key), $value, $expire);
  147. }
  148. }