laravel 在laravel 5中缓存查询中的所有记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32719875/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:23:21  来源:igfitidea点击:

Cache all records from query in laravel 5

laravelcaching

提问by Muhammed Khalander

I'm trying to cache all records of the query for 60 minutes by the following method (Method 1)

我正在尝试通过以下方法(方法 1)将查询的所有记录缓存 60 分钟

Route::get('categoryList', function() {
   return app\CategoryDetails::remember(60)->get();
});

I followed this tutorial link(Tip 5: Cache Database Queries)

我按照本教程链接(提示 5:缓存数据库查询)

But I'm getting this error:

但我收到此错误:

Call to undefined method Illuminate\Database\Query\Builder::remember()

调用未定义的方法 Illuminate\Database\Query\Builder::remember()

I don't know what I'm missing here.

我不知道我在这里错过了什么。

BTW, I know I can cache entire records by the following method (Method 2):

顺便说一句,我知道我可以通过以下方法(方法 2)缓存整个记录:

Route::get('categoryList', function() {    
    $category = Cache::remember('category', 10, function() {
        return \App\CategoryDetails::all();
    });

    return $category;
});

and this is working perfectly.

这是完美的工作。

I am just curious why the first method is not working for me.

我只是好奇为什么第一种方法对我不起作用。

回答by Joseph Silber

Laravel 5 removed this functionality. You now have to store the cache yourself:

Laravel 5 移除了这个功能。您现在必须自己存储缓存

Route::get('categoryList', function () {
    return Cache::remember('category-details', 60, function () {
        return App\CategoryDetails::all();
    });
});

From the upgrade docs:

升级文档

Eloquent no longer provides the remembermethod for caching queries. You now are responsible for caching your queries manually using the Cache::rememberfunction.

Eloquent 不再提供remember缓存查询的方法。您现在负责使用该Cache::remember函数手动缓存您的查询。

回答by Cengkuru Michael

Consider using a laravel eloquent query caching library called rememberable

考虑使用名为rememberable的 laravel eloquent 查询缓存库

It does a pretty good job.

它做得很好。