SiteCache.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace App\Helpers;
  3. use Illuminate\Support\Facades\Cache;
  4. use Illuminate\Support\Facades\Request; // 引入 Request 类
  5. use App\Models\DistAdminDistributor;
  6. use App\Models\DistAppearance;
  7. use App\Models\DistAppearancePublishList;
  8. use App\Services\MenuService;
  9. class SiteCache
  10. {
  11. /**
  12. * 获取或缓存商店信息
  13. *
  14. * @param string|null $domain
  15. * @param int $seconds 缓存时间(秒)
  16. * @return Dist|null
  17. */
  18. public static function getDist(?string $domain = null, int $seconds = 300): ?string
  19. {
  20. if (is_null($domain)) {
  21. return null; // 如果未传入域名,返回 null
  22. }
  23. self::checkAndClearCache($domain, 'dist'); // 检查是否需要清缓存
  24. return Cache::tags([$domain, 'dist'])->remember("dist_{$domain}", $seconds, function () use ($domain) {
  25. $dist= DistAdminDistributor::findByDomain($domain);
  26. // 检查 $dist 是否为 null
  27. if (!$dist)
  28. {
  29. // 如果找不到 $dist,返回 null 或其他默认值
  30. return null;
  31. }
  32. if (!empty($dist['appearance_id']))
  33. {
  34. $dist['appearance'] = DistAppearance::getTemplateById($dist['appearance_id']);
  35. $dist['publishList'] = DistAppearancePublishList::findByDistAndAppearance($dist['id'], $dist['appearance_id']);
  36. $dist['template_name'] = $dist['appearance']['title'];
  37. }
  38. // 序列化存储
  39. return serialize($dist);
  40. });
  41. }
  42. /**
  43. * 清除商店信息缓存
  44. *
  45. * @param string|null $domain
  46. * @return void
  47. */
  48. public static function clearDistCache(?string $domain = null): void
  49. {
  50. if (is_null($domain)) {
  51. return; // 如果未传入域名,不执行清除操作
  52. }
  53. Cache::tags([$domain, 'dist'])->forget("dist_{$domain}");
  54. }
  55. /**
  56. * 获取或缓存商品信息
  57. *
  58. * @param string|null $domain
  59. * @param int|null $productId
  60. * @param int $seconds 缓存时间(秒)
  61. * @return Product|null
  62. */
  63. public static function getProduct(?string $domain = null, ?int $productId = null, int $seconds = 300): ?Product
  64. {
  65. if (is_null($domain) || is_null($productId)) {
  66. return null; // 如果未传入 domain 或 productId,返回 null
  67. }
  68. return Cache::tags([$domain, 'product'])->remember("product_{$productId}", $seconds, function () use ($productId) {
  69. return Product::find($productId);
  70. });
  71. }
  72. /**
  73. * 清除商品信息缓存
  74. *
  75. * @param string|null $domain
  76. * @param int|null $productId
  77. * @return void
  78. */
  79. public static function clearProductCache(?string $domain = null, ?int $productId = null): void
  80. {
  81. if (is_null($domain) || is_null($productId)) {
  82. return; // 如果未传入 domain 或 productId,不执行清除操作
  83. }
  84. Cache::tags([$domain, 'product'])->forget("product_{$productId}");
  85. }
  86. /**
  87. * 获取或缓存文章信息
  88. *
  89. * @param string|null $domain
  90. * @param int|null $articleId
  91. * @param int $seconds 缓存时间(秒)
  92. * @return Article|null
  93. */
  94. public static function getArticle(?string $domain = null, ?int $articleId = null, int $seconds = 300): ?Article
  95. {
  96. if (is_null($domain) || is_null($articleId)) {
  97. return null; // 如果未传入 domain 或 articleId,返回 null
  98. }
  99. return Cache::tags([$domain, 'article'])->remember("article_{$articleId}", $seconds, function () use ($articleId) {
  100. return Article::find($articleId);
  101. });
  102. }
  103. /**
  104. * 清除文章信息缓存
  105. *
  106. * @param string|null $domain
  107. * @param int|null $articleId
  108. * @return void
  109. */
  110. public static function clearArticleCache(?string $domain = null, ?int $articleId = null): void
  111. {
  112. if (is_null($domain) || is_null($articleId)) {
  113. return; // 如果未传入 domain 或 articleId,不执行清除操作
  114. }
  115. Cache::tags([$domain, 'article'])->forget("article_{$articleId}");
  116. }
  117. /**
  118. * 清除某一域名下的所有缓存
  119. *
  120. * @param string $domain
  121. * @return void
  122. */
  123. public static function clearAllDomainCache(string $domain): void
  124. {
  125. Cache::tags([$domain])->flush(); // 清除该域名标签下的所有缓存
  126. }
  127. /**
  128. * 清除某一域名下特定类型的缓存
  129. *
  130. * @param string $domain
  131. * @param string $type
  132. * @return void
  133. */
  134. public static function clearSpecificTypeCache(string $domain, string $type): void
  135. {
  136. //找出域名下的所有域名后再清除缓存
  137. if(!empty($domain)) {
  138. $dist= DistAdminDistributor::findByDomain($domain);
  139. if($dist?->secondary_domain) {
  140. Cache::tags([$dist?->secondary_domain, $type])->flush(); // 清除该域名下特定类型的缓存
  141. }
  142. if($dist?->custom_domain) {
  143. Cache::tags([$dist?->custom_domain, $type])->flush(); // 清除该域名下特定类型的缓存
  144. }
  145. }
  146. }
  147. /**
  148. * 获取多级菜单(支持缓存)
  149. *
  150. * @param int $seconds 缓存时间(秒)
  151. * @return array
  152. */
  153. public static function getMenu(string $domain,int $menu_location=0, int $dist_id=0,int $seconds = 300): array
  154. {
  155. // 检查 URL 是否需要清除菜单缓存
  156. self::checkAndClearCache($domain, 'menu'); // 检查是否需要清缓存
  157. // 使用缓存存储和获取多级菜单
  158. return Cache::tags([$domain, "menu_{$menu_location}"])->remember('menu', $seconds, function () use ($menu_location,$dist_id) {
  159. $menuService = new MenuService();
  160. return $menuService->getMultiLevelMenu($menu_location,$dist_id);
  161. });
  162. }
  163. /**
  164. * 清除多级菜单缓存
  165. *
  166. * @return void
  167. */
  168. public static function clearMenuCache(?string $domain = null): void
  169. {
  170. Cache::tags([$domain,'menu'])->forget(); // 清除该域名下特定类型的缓存
  171. }
  172. /**
  173. * 检查 URL 是否包含清缓存参数并执行清除操作
  174. *
  175. * @param string|null $domain
  176. * @param string $type 缓存类型(如 'dist', 'product', 'article' 等)
  177. * @param int|null $id 相关 ID(可选)
  178. * @return void
  179. */
  180. public static function checkAndClearCache(?string $domain, string $type, ?int $id = null): void
  181. {
  182. if (Request::has('__clear_cache') && Request::input('__clear_cache') == 1) {
  183. if ($id) {
  184. self::clearSpecificTypeCache($domain, "{$type}_{$id}");
  185. } else {
  186. self::clearSpecificTypeCache($domain, $type);
  187. }
  188. }
  189. }
  190. }