
LaraCache 是一个基于 ORM 的 Laravel 包, 用于基于模型查询创建、更新和管理缓存项。使用此包,您可以缓存在整个应用程序中大量使用的查询。
use MostafaznvLaraCacheTraitsLaraCache;class Article extends Model{ use LaraCache; public static function cacheEntities(): array { return [ CacheEntity::make('list.forever') ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('latest') ->validForRestOfDay() ->cache(function() { return Article::query()->latest()->first(); }) ]; }}
使用 cacheEntities 方法来定义缓存的查询,Laracache 会处理剩下的事情。要使用缓存查询,您将调用模型,如下例所示:
use MostafaznvLaraCacheFacadesLaraCache;$cache = Article::cache()->get('latest');// 或者$cache = LaraCache::retrieve(Article::class, 'latest');
使用此软件包,您可以使用以下功能控制缓存:
启用/禁用缓存手动更新缓存手动更新所有缓存实体删除缓存使用 fluent 方法或 ttl()方法控制CacheEntity 持续时间
我认为以下手动缓存更新方法很简洁,可以即时刷新缓存:
豆包AI编程
豆包推出的AI编程助手
483 查看详情
Article::cache()->update('latest');2// or3LaraCache::update(Article::class, 'latest');
您可以了解此软件包、获取完整的安装说明,并在 GitHub 上查看 源代码 。
原文地址:https://laravel-news.com/laracache-orm-caching-package-for-laravel译文地址:https://learnku.com/laravel/t/68860
【相关推荐:laravel视频教程】
以上就是Laravel扩展推荐:ORM 缓存包 “LaraCache”的详细内容,更多请关注php中文网其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/624364.html
微信扫一扫
支付宝扫一扫