whtht / perfectly-cache
Laravel 数据库缓存包
v4.3
2021-06-22 12:01 UTC
Requires (Dev)
- orchestra/testbench: ^6.0
README
Laravel Eloquent 查询缓存包。
它用于缓存并使用你提出的任何查询,而无需对数据库、系统或查询进行任何更改。
PerfectlyCache 会自动将相同的查询重定向到模型,当你需要相同的查询时,它会通过缓存模型上执行的查询结果来实现。
安装
- Composer 执行以下命令以获取包的最新版本
composer require whtht/perfectly-cache
- 发布配置
php artisan vendor:publish --provider="Whtht\PerfectlyCache\Providers\PerfectlyCacheServiceProvider"
- 在模型上使用特性
在你的模型中添加以下代码 / 或者像这样添加你的基模型
<?php namespace App; use Illuminate\Database\Eloquent\Model; use Whtht\PerfectlyCache\Traits\PerfectlyCachable; class User extends Model { use PerfectlyCachable; }
你通过此模型发出的所有数据库查询都将被缓存,并在需要时从缓存中读取,而不是从数据库中读取。
配置
// config('perfectly-cache.(name)') // Eq: config('perfecyly-cache.enabled') return [ "enabled" => true, // Is cache enabled? cache for production --> !env('APP_DEBUG', false) "minutes" => 1, // Cache minutes. /** * If this event is triggered on this model, * the cache of that table is deleted. */ "clear_events" => [ "created", "updated", "deleted" ], ];
用法
// Basic cache $results = \App\Category::find($id); // Basic cache skip $results = \App\Category::skipCache()->find($id); // Basic usage with eager load $results = \App\Category::with("_list_category_tags")->find($id); // Basic cache skip usage with eager load $results = \App\Category::with("^_list_category_tags")->find($id);
跳过缓存
- 链式调用
// ->skipCache(); $result = Category::select("id", "name")->skipCache()->get();
- 预加载
/** * Thanks to the ^ sign, you can prevent your relationships from being cached. */ $results = Category::select("id", "name")->with([ "^_list_category_tags:id,category_id,name,slug" ])->find($id); /** * It will no longer be hidden in the cache for the tag table of the categories. */
- 在模型中跳过
使用
$isCacheEnable
变量管理你的模型。
<?php namespace App; class Category extends BaseModel { /* Cache disabled by this variable */ protected $isCacheEnable = false; }
缓存时间调整
你可以在配置中设置缓存时间(perfectly-cache.minutes
)
你可以从模型或直接在查询期间全局指定,你可以通过编辑设置来应用所有模型。缓存时间可以在查询、模型和设置中编辑。
- 在配置中
...
"minutes" => 30,
- 在模型
$cacheMinutes
<?php namespace App; class Module extends BaseModel { protected $table = "modules"; protected $cacheMinutes = 20; // Now cache time 20 minutes. }
- 在查询
->remember(:minutes)
$modules = \App\Module::remember(10)->select("id", "name")->get();
此查询将被缓存10分钟。
- 在预加载中
$modules = \App\Module::with([ "(10)categories:id,name,module_id" ])->select("id", "name")->get(); // Categories will be cached for 10 minutes.
程序化缓存重载
如果你想逻辑上刷新查询,你可以使用 ->reloadCache()
如下。
$module = Module::select("id", "name", "need_cache_reload")->first(); if($module->need_cache_reload) { // simple true value $module->reloadCache(); }
命令
# Clear all caches. php artisan perfectly-cache:clear #Clear all users table caches php artisan perfectly-cache:clear users #Clear all users and modules tables caches php artisan perfectly-cache:clear users modules # Infinity table names #Show cache details php artisan perfectly-cache:list
注意
如果你已经对查询使用了时间,并且此查询将被缓存,如下所示,
$modules2 = Module::select("id", "name") ->where("created_time_unix", ">=", time()) ->get();
你需要在此查询上添加 ->skipCache()
方法。
因为:每次运行此查询时,它将创建不同的缓存。
许可证
该项目使用 MIT 许可证许可 - 有关详细信息,请参阅 LICENSE 文件