Irakli
Irakli

Reputation: 31

Laravel remember cache

I'm trying to remember cache for 6 minute on home page but it's errors. I want 10 parameters in one key ( homepage-data ) so what i'm doing wrong?

public function index()
{
    $rememberKey = 'homepage-data';
    $minutes = 600;
    \Cache::remember($rememberKey, $minutes, function () {

        $breakingNews = BreakingNews::published();
        $rightSlider = Article::rightSlider()->take(20)->get();
        $leftSlider = Article::leftSlider()->take(9)->get();
        $shows = Show::publishedOrderShows()->take(10)->get();
        $blogs = Article::allBlogs()->take(12)->get();
        $latestNews = Article::latestNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id')))->take(5)->get();
        $otherNews = Article::otherNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id')))->take(2)->get();
        $otherNewsDown = Article::otherNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id'))->merge($otherNews->pluck('id')))->take(8)->get();
        $analyticNews = Article::allAnalytics()->take(4)->get();
        $populars = Article::popular()->take(10)->get();
        $quizzes = Quizze::quizOrderBy();

        return view('pages/home-page')->with([
            'breakingNews' => $breakingNews, 'rightSlider' => $rightSlider,
            'leftSlider' => $leftSlider, 'shows' => $shows, 'blogs' => $blogs, 'latestNews' => $latestNews,
            'otherNews' => $otherNews, 'otherNewsDown' => $otherNewsDown, 'analyticNews' => $analyticNews,
            'populars' => $populars, 'quizzes' => $quizzes
        ]);
    });
}

I'm getting Serialization of 'Closure' is not allowed error.

Upvotes: 2

Views: 1700

Answers (1)

aynber
aynber

Reputation: 23001

With your original question, you were defeating the purpose of the cache by having all of your queries outside of the cache. Now, you have too much inside of your closure, and it's trying to serialize the view. Move the view outside of the cache, assign the retrieved cache to $data, then use that as an array to send back to the view.

public function index()
{
   
    $rememberKey = 'homepage-data';
    $minutes = 600;
    $data = \Cache::remember($rememberKey, $minutes, function ()  {
        $breakingNews = BreakingNews::published();
        $rightSlider = Article::rightSlider()->take(20)->get();
        $leftSlider = Article::leftSlider()->take(9)->get();
        $shows = Show::publishedOrderShows()->take(10)->get();
        $blogs = Article::allBlogs()->take(12)->get();
        $latestNews = Article::latestNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id')))->take(5)->get();
        $otherNews = Article::otherNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id')))->take(2)->get();
        $otherNewsDown = Article::otherNews()->whereNotIn('id', $rightSlider->pluck('id')->merge($leftSlider->pluck('id'))->merge($otherNews->pluck('id')))->take(8)->get();
        $analyticNews = Article::allAnalytics()->take(4)->get();
        $populars = Article::popular()->take(10)->get();
        $quizzes = Quizze::quizOrderBy();

        // CACHE
        return ['breakingNews' => $breakingNews, 
           'rightSlider'=>  $rightSlider, 
            'leftSlider' => $leftSlider, 
            'shows' => $shows, 
            'blogs' => $blogs, 
            'latestNews' => $latestNews, 
            'otherNews' => $otherNews, 
            'otherNewsDown' => $otherNewsDown, 
            'analyticNews' => $analyticNews,
            'populars' => $populars,
            'quizzes' => $quizzes];
       
    });

    return view('pages/home-page')->with([
        'breakingNews' => $data['breakingNews'], 'rightSlider' => $data['rightSlider'],
        'leftSlider' => $data['leftSlider'], 'shows' => $data['shows'], 'blogs' => $data['blogs'], 'latestNews' => $data['latestNews'],
        'otherNews' => $data['otherNews'], 'otherNewsDown' => $data['otherNewsDown'], 'analyticNews' => $data['analyticNews'],
        'populars' => $data['populars'], 'quizzes' => $data['quizzes']
    ]);
}

Upvotes: 1

Related Questions