Laravel 9 保姆級(jí)視頻教程,想學(xué)不會(huì)都難!進(jìn)入學(xué)習(xí)
LaraCache 是一個(gè)基于 ORM 的 Laravel 包, 用于基于模型查詢創(chuàng)建、更新和管理緩存項(xiàng)。使用此包,您可以緩存在整個(gè)應(yīng)用程序中大量使用的查詢。
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(); }) ]; } }
登錄后復(fù)制
使用 cacheEntities
方法來(lái)定義緩存的查詢,Laracache 會(huì)處理剩下的事情。要使用緩存查詢,您將調(diào)用模型,如下例所示:
use MostafaznvLaraCacheFacadesLaraCache; $cache = Article::cache()->get('latest'); // 或者 $cache = LaraCache::retrieve(Article::class, 'latest');
登錄后復(fù)制
使用此軟件包,您可以使用以下功能控制緩存:
- 啟用/禁用緩存
- 手動(dòng)更新緩存
- 手動(dòng)更新所有緩存實(shí)體
- 刪除緩存
- 使用 fluent 方法或
ttl()
方法控制CacheEntity
持續(xù)時(shí)間
我認(rèn)為以下手動(dòng)緩存更新方法很簡(jiǎn)潔,可以即時(shí)刷新緩存:
Article::cache()->update('latest');2// or3LaraCache::update(Article::class, 'latest');
登錄后復(fù)制
您可以了解此軟件包、獲取完整的安裝說(shuō)明,并在 GitHub 上查看 源代碼 。
原文地址:https://laravel-news.com/laracache-orm-caching-package-for-laravel
譯文地址:https://learnku.com/laravel/t/68860
【