Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel cache remember forever

Tags:

laravel

I have this piece of code that I thought would remember cache forever:

$setting = \Cache::remember('website_description', 0,  function() {
  return App\Models\Setting::where('name', 'website_description')->first();
});

But it doesn't remember at all, someone told me that passing 0 to remember function would make it remember forever. Since this doesn't work what other way can I put an item in cache forever?

like image 675
niko craft Avatar asked Oct 22 '16 15:10

niko craft


2 Answers

Just change remember to rememberForever and remove time parameter

 $setting = \Cache::rememberForever('website_description', function() {
            return App\Models\Setting::where('name', 'website_description')->first();
        });

Referance for rememberForever

like image 197
Sanath Samarasinghe Avatar answered Oct 17 '22 00:10

Sanath Samarasinghe


You can try this:

if (! \Cache::has('website_description')) {
    \Cache::forever('website_description', App\Models\Setting::where('name', 'website_description')->first());
}
like image 37
aceraven777 Avatar answered Oct 16 '22 22:10

aceraven777