Laravel缓存永远记得

时间:2016-10-22 15:37:47

标签: laravel

我有这段代码,我认为会永远记住缓存:

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

但它根本不记得,有人告诉我,通过0来记住功能会让它永远记住。由于这不起作用,我可以将项目永远放在缓存中吗?

2 个答案:

答案 0 :(得分:18)

只需将remember更改为rememberForever并删除时间参数

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

Referance for rememberForever

答案 1 :(得分:2)

你可以试试这个:

if (! \Cache::has('website_description')) {
    \Cache::forever('website_description', App\Models\Setting::where('name', 'website_description')->first());
}