SiteCache.php 7.1 KB

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