0
点赞
收藏
分享

微信扫一扫

Laravel查询加缓存


在查询的时候加缓存好像在thinkphp有这个功能,直接在查询语句中添加->cache() 的方法。

今天介绍一种laravel 的查询缓存。

composer require rennokki/laravel-eloquent-query-cache

需要在模型中添加 trait

use QueryCacheable;
public $cacheFor = 3600; // cache time, in seconds

查询语句中的使用

如果不使用 cacheTags 标签,则为默认的where 标签
’也可以在语句中 使用 ​​​cacheFor(60 * 60)​​ 来设定缓存的时间

$parentObj = GoodsCat::whereName('纸尿裤')->cacheTags(['zhiniaoku:纸尿裤'])->first();

手动清除缓存需要用

GoodsCat::flushQueryCache(['zhiniaoku:纸尿裤']);

自动检测更新

protected static $flushCacheOnUpdate = true;


举报

相关推荐

0 条评论