mostafaznv / laracache
LaraCache 是一个可定制的缓存特性,用于缓存模型事件的查询
Requires
- php: ^8.0.2
- laravel/framework: ^8.40.0|^9.0|^10.0|^11.0
Requires (Dev)
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- pestphp/pest: ^1.20|^2.34
- phpunit/phpunit: ^9.5.10|^10.0
- spatie/pest-plugin-test-time: ^1.0|^2.1
README
使用此包,您可以缓存您的重用和最常用的查询。
您需要做的只是定义模型中的 CacheEntity
对象,并指定它们的合法名称和 ttl。
LaraCache 将自动处理剩余的过程。它将根据您为每个实体定义的 ttl 创建和更新缓存实体。
在派发模型事件(创建、更新和删除)后手动更新模型的缓存实体不是必需的,LaraCache 在后台管理它们,并确保每个缓存实体的最新版本。
除了核心的 LaraCache 包外,我还开发了一个互补包,名为 Nova LaraCache。Nova LaraCache 无缝地将 LaraCache 与 Laravel Nova 集成,这是 Laravel 应用的管理面板。它提供了 Laravel Nova 管理面板内的用户友好界面,使用户能够方便地管理和调节缓存实体。
我正在开源之旅上 🚀,我希望我能专注于我的开发路径,而不用担心我的财务状况。然而,生活并不完美,我必须考虑其他因素。
因此,如果您决定使用我的包,请考虑慷慨捐赠。任何金额,无论大小,都意义重大,非常感谢。🍺
要求
- PHP 8.0.2 或更高版本
- Laravel 8.40.0 或更高版本
安装
-
使用 composer 安装包
composer require mostafaznv/laracache
-
发布配置文件
php artisan vendor:publish --provider="Mostafaznv\LaraCache\LaraCacheServiceProvider"
-
完成
用法
-
将 LaraCache 特性添加到模型中
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mostafaznv\LaraCache\Traits\LaraCache; class Article extends Model { use LaraCache; /** * Define Cache Entities Entities * * @return CacheEntity[] */ 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(); }) ]; } }
-
检索缓存
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; $cache = Article::cache()->get('latest'); // or $cache = LaraCache::retrieve(Article::class, 'latest');
目录
CacheEntity 方法
禁用/启用缓存
如果您想禁用/启用缓存,您可以通过以下两种方式之一操作
禁用
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->disable(); // or LaraCache::disable(Article::class);
启用
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->enable(); // or LaraCache::enable(Article::class);
手动更新缓存
有时您可能想手动更新缓存实体。
更新一个实体
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->update('latest'); // or LaraCache::update(Article::class, 'latest');
更新所有实体
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->updateAll(); // or LaraCache::updateAll(Article::class);
更新所有 LaraCache 实体
这将更新使用 LaraCache 存储的所有缓存实体(跨所有模型)
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; LaraCache::updateAll();
手动删除缓存
有时您可能想手动删除缓存实体。使用这些方法,您可以这样做。
删除一个实体
使用此功能,您可以临时删除缓存实体。在 ttl 过期后,缓存实体将再次生成。
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->delete('latest'); // or LaraCache::delete(Article::class, 'latest');
永久删除一个实体
使用此功能,您可以永久删除缓存实体。缓存项将永远删除,并且每次尝试检索它时,您都将获得 null(或默认值)。
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->delete('latest', true); // or LaraCache::delete(Article::class, 'latest', true);
注意:创建或更新模型中的记录后,缓存实体将更新
删除所有模型实体
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->deleteAll(); // or LaraCache::deleteAll(Article::class);
永久删除所有模型实体
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; Article::cache()->deleteAll(true); // or LaraCache::deleteAll(Article::class, true);
删除所有 LaraCache 实体
这将删除使用 LaraCache 存储的所有缓存实体(跨所有模型)
use App\Models\Article; use Mostafaznv\LaraCache\Facades\LaraCache; LaraCache::deleteAll(); // forever LaraCache::deleteAll(forever: true);
Artisan 命令
此功能允许您通过控制台命令更新或删除一个或多个模型的一个或多组缓存实体。这意味着您可以在缓存周期之外以编程方式控制缓存数据。
您还可以在配置文件中创建模型及其实体的组,并轻松一次性更新或删除所有实体。
更新缓存
# updates all entities of article model php artisan laracache:update -m Article # updates specified entities of article model php artisan laracache:update -m Article -e latest -e featured # updates all entities of article and product models php artisan laracache:update -m Article -m Product # defines model with full namespace php artisan laracache:update -m "Domain\Article\Models\Article"
删除缓存
# deletes all entities of article model php artisan laracache:delete -m Article # deletes specified entities of article model php artisan laracache:delete -m Article -e latest -e featured # deletes all entities of article and product models php artisan laracache:delete -m Article -m Product # defines model with full namespace php artisan laracache:delete -m "Domain\Article\Models\Article"
注意:如果您没有指定任何实体,将操作所有实体。
注意:如果您指定了多个模型,则不能指定任何实体,所有模型的全部实体都将被操作。
分组操作
# updates all entities of models that are in group-1 php artisan laracache:update-group group-1 # deletes all entities of models that are in group-1 php artisan laracache:delete-group group-1
这是一个分组配置的示例。
# config/laracache.php return [ // ... 'groups' => [ 'group-1' => [ [ 'model' => \App\Models\User::class, 'entities' => [ 'users.latest', 'users.featured' ], ], [ 'model' => \App\Models\Article::class, 'entities' => [], ] ], 'group-2' => [ [ 'model' => \App\Models\Article::class, 'entities' => [ 'featured-list', 'latest' ], ], [ 'model' => \App\Models\User::class, 'entities' => ['users.latest'], ] ], ] ];
Laravel Nova 支持
Nova LaraCache 是一个强大的 Laravel Nova 扩展包,通过无缝集成到 Laravel Nova 中扩展了 LaraCache 的功能。它提供了 Laravel Nova 管理面板内的直观界面,使用户能够轻松地审核和管理缓存实体。
使用 Nova LaraCache,用户可以方便地监控缓存过期日期,审查缓存实体内容,重新生成缓存项,以及删除特定的缓存条目。
要解锁 Nova LaraCache 提供的缓存管理功能,请参阅安装说明,并咨询 LaraCache 的文档以获取为每个模型创建缓存实体的指导。
配置属性
完整示例
<?php namespace App\Models; use Illuminate\Database\Eloquent\Model; use Mostafaznv\LaraCache\Traits\LaraCache; class Article extends Model { use LaraCache; /** * Define Cache Entities Entities * * @return CacheEntity[] */ public static function cacheEntities(): array { return [ CacheEntity::make('list.forever') ->forever() ->setDriver('redis') ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('list.day') ->isQueueable() ->validForRestOfDay() ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('list.week') ->isQueueable(true, 'redis', 'low') ->validForRestOfWeek() ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('list.ttl') ->ttl(120) ->cache(function() { return Article::query()->latest()->get(); }), CacheEntity::make('latest') ->forever() ->cache(function() { return Article::query()->latest()->first(); }), CacheEntity::make('latest.no-create') ->refreshAfterCreate(false) ->cache(function() { return Article::query()->latest()->first(); }), CacheEntity::make('latest.no-update') ->refreshAfterUpdate(false) ->cache(function() { return Article::query()->latest()->first(); }), CacheEntity::make('latest.no-delete') ->refreshAfterDelete(false) ->cache(function() { return Article::query()->latest()->first(); }), CacheEntity::make('latest.no-restore') ->refreshAfterRestore(false) ->cache(function() { return Article::query()->latest()->first(); }), CacheEntity::make('empty.array') ->setDefault('empty value') ->cache(fn() => []), ]; } }
我正在开源之旅上 🚀,我希望我能专注于我的开发路径,而不用担心我的财务状况。然而,生活并不完美,我必须考虑其他因素。
因此,如果您决定使用我的包,请考虑慷慨捐赠。任何金额,无论大小,都意义重大,非常感谢。🍺
许可证
本软件遵循MIT 许可证 (MIT)发布。