SiteCache.php 6.2 KB

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