Laravel cache implement for multiple section
public function extraIndex()
{
$cacheDuration = 60 * 60 * 24; // Cache for 24 hours (adjust as needed)
// Cache General Setting
$gs = Cache::remember('general_setting', $cacheDuration, function () {
return Generalsetting::findOrFail(1);
});
// Cache Banners
$bottom_small_banners = Cache::remember('bottom_small_banners', $cacheDuration, function () {
return DB::table('banners')->where('type', '=', 'BottomSmall')->get();
});
$large_banners = Cache::remember('large_banners', $cacheDuration, function () {
return DB::table('banners')->where('type', '=', 'Large')->get();
});
// Cache Page Settings
$ps = Cache::remember('page_settings', $cacheDuration, function () {
return DB::table('pagesettings')->find(1);
});
// Cache Partners
$partners = Cache::remember('partners', $cacheDuration, function () {
return DB::table('partners')->get();
});
// Cache Products
$discount_products = Cache::remember('discount_products', $cacheDuration, function () use ($gs) {
return Product::where('is_discount', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(8)->get();
});
$best_products = Cache::remember('best_products', $cacheDuration, function () use ($gs) {
return Product::where('best', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(8)->get();
});
$top_products = Cache::remember('top_products', $cacheDuration, function () use ($gs) {
return Product::where('top', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(8)->get();
});
$big_products = Cache::remember('big_products', $cacheDuration, function () use ($gs) {
return Product::where('big', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(8)->get();
});
$hot_products = Cache::remember('hot_products', $cacheDuration, function () use ($gs) {
return Product::where('hot', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(10)->get();
});
$latest_products = Cache::remember('latest_products', $cacheDuration, function () use ($gs) {
return Product::where('latest', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(10)->get();
});
$trending_products = Cache::remember('trending_products', $cacheDuration, function () use ($gs) {
return Product::where('trending', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(10)->get();
});
$sale_products = Cache::remember('sale_products', $cacheDuration, function () use ($gs) {
return Product::where('sale', '=', 1)->where('status', '=', 1)
->when($gs->affilate_product == 0, function ($q) {
return $q->where('product_type', '=', 'normal');
})->orderBy('id', 'desc')->take(10)->get();
});
return view('front.extraindex', compact(
'ps', 'large_banners', 'bottom_small_banners', 'best_products',
'top_products', 'hot_products', 'latest_products', 'big_products',
'trending_products', 'sale_products', 'discount_products', 'partners'
));
}
No comments