Laravel 缓存永远记住
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40193927/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel cache remember forever
提问by niko craft
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?
但是它根本不记得,有人告诉我,传递0
给记住功能会让它永远记住。既然这不起作用,我可以通过什么其他方式将项目永远放入缓存中?
回答by Sanath Samarasinghe
Just change remember
to rememberForever
and remove time parameter
只要改变remember
到rememberForever
和删除时间参数
$setting = \Cache::rememberForever('website_description', function() {
return App\Models\Setting::where('name', 'website_description')->first();
});
回答by aceraven777
You can try this:
你可以试试这个:
if (! \Cache::has('website_description')) {
\Cache::forever('website_description', App\Models\Setting::where('name', 'website_description')->first());
}