SiteCache.php 4.0 KB

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