123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- <?php
- namespace App\Helpers;
- use Illuminate\Support\Facades\Cache;
- use Illuminate\Support\Facades\Request;
- use App\Models\DistAdminDistributor;
- use App\Models\DistAppearance;
- use App\Models\DistAppearancePublishList;
- use App\Services\MenuService;
- class SiteCache
- {
-
- public static function getDist(?string $domain = null, int $seconds = 300): ?string
- {
- if (is_null($domain)) {
- return null;
- }
- self::checkAndClearCache($domain, 'dist');
- return Cache::tags([$domain, 'dist'])->remember("dist_{$domain}", $seconds, function () use ($domain) {
- $dist= DistAdminDistributor::findByDomain($domain);
-
- if (!$dist)
- {
-
- return null;
- }
- if (!empty($dist['appearance_id']))
- {
- $appearance = DistAppearance::getTemplateById($dist['appearance_id']);
- $publishList = DistAppearancePublishList::findByDistAndAppearance($dist['id'], $dist['appearance_id']);
- if ($appearance && $publishList) {
- $dist['appearance'] = $appearance;
- $dist['publishList'] = $publishList;
- $dist['template_name'] = $appearance['folder'];
- } else {
- return null;
- }
- }
- else
- {
- return null;
- }
-
- return serialize($dist);
- });
- }
-
- public static function clearDistCache(?string $domain = null): void
- {
- if (is_null($domain)) {
- return;
- }
- Cache::tags([$domain, 'dist'])->forget("dist_{$domain}");
- }
-
- public static function getProduct(?string $domain = null, ?int $productId = null, int $seconds = 300): ?Product
- {
- if (is_null($domain) || is_null($productId)) {
- return null;
- }
- return Cache::tags([$domain, 'product'])->remember("product_{$productId}", $seconds, function () use ($productId) {
- return Product::find($productId);
- });
- }
-
- public static function clearProductCache(?string $domain = null, ?int $productId = null): void
- {
- if (is_null($domain) || is_null($productId)) {
- return;
- }
- Cache::tags([$domain, 'product'])->forget("product_{$productId}");
- }
-
- public static function getArticle(?string $domain = null, ?int $articleId = null, int $seconds = 300): ?Article
- {
- if (is_null($domain) || is_null($articleId)) {
- return null;
- }
- return Cache::tags([$domain, 'article'])->remember("article_{$articleId}", $seconds, function () use ($articleId) {
- return Article::find($articleId);
- });
- }
-
- public static function clearArticleCache(?string $domain = null, ?int $articleId = null): void
- {
- if (is_null($domain) || is_null($articleId)) {
- return;
- }
- Cache::tags([$domain, 'article'])->forget("article_{$articleId}");
- }
-
- public static function clearAllDomainCache(string $domain): void
- {
- Cache::tags([$domain])->flush();
- }
-
- public static function clearSpecificTypeCache(string $domain, string $type): void
- {
-
- if(!empty($domain)) {
- $dist= DistAdminDistributor::findByDomain($domain);
- if($dist?->secondary_domain) {
- Cache::tags([$dist?->secondary_domain, $type])->flush();
- }
- if($dist?->custom_domain) {
- Cache::tags([$dist?->custom_domain, $type])->flush();
- }
- }
- }
-
- public static function getMenu(string $domain,int $menu_location=0, int $dist_id=0,int $seconds = 300): array
- {
-
- self::checkAndClearCache($domain, 'menu');
-
- return Cache::tags([$domain, "menu_{$menu_location}"])->remember('menu', $seconds, function () use ($menu_location,$dist_id) {
- $menuService = new MenuService();
- return $menuService->getMultiLevelMenu($menu_location,$dist_id);
- });
- }
-
- public static function clearMenuCache(?string $domain = null): void
- {
- Cache::tags([$domain,'menu'])->forget();
- }
-
- public static function checkAndClearCache(?string $domain, string $type, ?int $id = null): void
- {
- if (Request::has('__clear_cache') && Request::input('__clear_cache') == 1) {
- if ($id) {
- self::clearSpecificTypeCache($domain, "{$type}_{$id}");
- } else {
- self::clearSpecificTypeCache($domain, $type);
- }
- }
- }
- }
|