SiteCache.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Helpers;
  3. use Illuminate\Support\Facades\Cache;
  4. use App\Models\DistAdminDistributor;
  5. use App\Models\DistAppearance;
  6. use App\Models\DistAppearancePublishList;
  7. class SiteCache
  8. {
  9. /**
  10. * 获取或缓存商店信息
  11. *
  12. * @param string|null $domain
  13. * @param int $seconds 缓存时间(秒)
  14. * @return Dist|null
  15. */
  16. public static function getDist(?string $domain = null, int $seconds = 300): ?string
  17. {
  18. if (is_null($domain)) {
  19. return null; // 如果未传入域名,返回 null
  20. }
  21. return Cache::tags([$domain, 'dist'])->remember("dist_{$domain}", $seconds, function () use ($domain) {
  22. $dist= DistAdminDistributor::findByDomain($domain);
  23. if($dist['appearance_id'])
  24. {
  25. $dist['appearance'] = DistAppearance::getTemplateById($dist['appearance_id']);
  26. $dist['publishList'] = DistAppearancePublishList::findByDistAndAppearance($dist['id'], $dist['appearance_id']);
  27. }
  28. // 序列化存储
  29. return serialize($dist);
  30. });
  31. }
  32. /**
  33. * 清除商店信息缓存
  34. *
  35. * @param string|null $domain
  36. * @return void
  37. */
  38. public static function clearDistCache(?string $domain = null): void
  39. {
  40. if (is_null($domain)) {
  41. return; // 如果未传入域名,不执行清除操作
  42. }
  43. Cache::tags([$domain, 'dist'])->forget("dist_{$domain}");
  44. }
  45. /**
  46. * 获取或缓存商品信息
  47. *
  48. * @param string|null $domain
  49. * @param int|null $productId
  50. * @param int $seconds 缓存时间(秒)
  51. * @return Product|null
  52. */
  53. public static function getProduct(?string $domain = null, ?int $productId = null, int $seconds = 300): ?Product
  54. {
  55. if (is_null($domain) || is_null($productId)) {
  56. return null; // 如果未传入 domain 或 productId,返回 null
  57. }
  58. return Cache::tags([$domain, 'product'])->remember("product_{$productId}", $seconds, function () use ($productId) {
  59. return Product::find($productId);
  60. });
  61. }
  62. /**
  63. * 清除商品信息缓存
  64. *
  65. * @param string|null $domain
  66. * @param int|null $productId
  67. * @return void
  68. */
  69. public static function clearProductCache(?string $domain = null, ?int $productId = null): void
  70. {
  71. if (is_null($domain) || is_null($productId)) {
  72. return; // 如果未传入 domain 或 productId,不执行清除操作
  73. }
  74. Cache::tags([$domain, 'product'])->forget("product_{$productId}");
  75. }
  76. /**
  77. * 获取或缓存文章信息
  78. *
  79. * @param string|null $domain
  80. * @param int|null $articleId
  81. * @param int $seconds 缓存时间(秒)
  82. * @return Article|null
  83. */
  84. public static function getArticle(?string $domain = null, ?int $articleId = null, int $seconds = 300): ?Article
  85. {
  86. if (is_null($domain) || is_null($articleId)) {
  87. return null; // 如果未传入 domain 或 articleId,返回 null
  88. }
  89. return Cache::tags([$domain, 'article'])->remember("article_{$articleId}", $seconds, function () use ($articleId) {
  90. return Article::find($articleId);
  91. });
  92. }
  93. /**
  94. * 清除文章信息缓存
  95. *
  96. * @param string|null $domain
  97. * @param int|null $articleId
  98. * @return void
  99. */
  100. public static function clearArticleCache(?string $domain = null, ?int $articleId = null): void
  101. {
  102. if (is_null($domain) || is_null($articleId)) {
  103. return; // 如果未传入 domain 或 articleId,不执行清除操作
  104. }
  105. Cache::tags([$domain, 'article'])->forget("article_{$articleId}");
  106. }
  107. /**
  108. * 清除某一域名下的所有缓存
  109. *
  110. * @param string $domain
  111. * @return void
  112. */
  113. public static function clearAllDomainCache(string $domain): void
  114. {
  115. Cache::tags([$domain])->flush(); // 清除该域名标签下的所有缓存
  116. }
  117. /**
  118. * 清除某一域名下特定类型的缓存
  119. *
  120. * @param string $domain
  121. * @param string $type
  122. * @return void
  123. */
  124. public static function clearSpecificTypeCache(string $domain, string $type): void
  125. {
  126. Cache::tags([$domain, $type])->flush(); // 清除该域名下特定类型的缓存
  127. }
  128. }