SiteCache.php 4.1 KB

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