get(); 问题 在 MVC C-6ren">
gpt4 book ai didi

php - Laravel 5.2 中的缓存

转载 作者:搜寻专家 更新时间:2023-10-31 21:04:51 24 4
gpt4 key购买 nike

我在做什么

下面是我的数据库查询,它从数据库中获取所有用户。

$Users = \App\Models\User\User_Model::with("Role")->get();

问题

在 MVC CI 中,我们可以像下面那样进行数据库查询缓存 $this->_ci->db->cache_on(); Laravel 5.2 中有内置方法吗?




更新

As per the Docs here ...我正在编写以下代码来获取和设置缓存

$Categories = \Cache::get('Categories', function() {
return \App\Models\Skill\Category_Model::all();
});

以上代码不起作用,总是从数据库中获取数据。我错过了什么吗?

最佳答案

以这种方式使用它(只需根据您的需要修改它):

$users = Cache::remember('users', $minutes, function()
{
return DB::table('users')->get();
});

第一次会去数据库,下次会从缓存中加载。使用 $minutes 参数,您可以定义在从数据库中再次获取之前应该缓存多少分钟。

关于php - Laravel 5.2 中的缓存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34351400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com