suitmedia / laravel-cacheable
装饰您的仓库并使其可缓存
1.12.0
2024-03-13 12:30 UTC
Requires
- php: ^8.0
- illuminate/cache: ^8.0|^9.0|^10.0|^11.0
- illuminate/database: ^8.0|^9.0|^10.0|^11.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0
Requires (Dev)
- ekino/phpstan-banned-code: ^1.0
- fakerphp/faker: ^1.14
- larastan/larastan: ^1.0|^2.0
- mockery/mockery: ^1.4
- orchestra/testbench: ^6.0|^7.0|^8.0|^9.0
- phpmd/phpmd: ^2.11
- phpstan/phpstan-deprecation-rules: ^1.0
- phpunit/phpunit: ^9.5|^10.0|^11.0
README
Laravel Cacheable
装饰您的仓库并使其可缓存
摘要
此包将帮助您使仓库可缓存,无需担心如何管理缓存,并提供一种简单的方式来使缓存无效。Laravel Cacheable 包使用动态装饰器类来包装现有的仓库,并在其中添加自动缓存功能。
目录
兼容性
要求
此包要求您使用支持标签的缓存存储,如memcached或redis。如果您使用不支持标签的任何缓存存储,则可能会出现错误。
设置
使用Composer安装包
$ composer require suitmedia/laravel-cacheable
如果您使用Laravel版本5.5及以上,则可以跳过在应用程序中注册服务提供者和包别名。
注册服务提供者
在您的 config/app.php
中添加包服务提供者
'providers' => [ // ... \Suitmedia\Cacheable\ServiceProvider::class, ];
注册包别名
在您的 config/app.php
中添加包别名
'aliases' => [ // ... 'Cacheable' => \Suitmedia\Cacheable\Facade::class, ];
配置
使用 php artisan
命令发布配置文件
$ php artisan vendor:publish --provider="Suitmedia\Cacheable\ServiceProvider"
上述命令会将新的配置文件复制到 /config/cacheable.php
return [ /* |-------------------------------------------------------------------------- | Custom Tags |-------------------------------------------------------------------------- | | Define all of Eloquent Models which should add custom cache tags | automatically to the cached objects. | */ 'customTags' => \App\User::class, /* |-------------------------------------------------------------------------- | Default Cache Duration |-------------------------------------------------------------------------- | | Define the default cache duration here. | Setting the cache duration to '0' will make the cache lasts forever. | */ 'duration' => 0, /* |-------------------------------------------------------------------------- | Methods which shouldn't be cached |-------------------------------------------------------------------------- | | Define a collection of method names which you don't wish | to be cached. | */ 'except' => [ 'cacheDuration', 'cacheExcept', 'cacheKey', 'cacheTags', 'create', 'delete', 'restore', 'update', 'updateOrCreate', ], ];
用法
准备您的模型
您希望缓存的每个模型都应该实现 CacheableModel
接口并使用 CacheableTrait
。该特性将为您的模型添加额外的功能,并观察模型以发现任何未来的变化。这样,当观察到的模型被更新时,包会立即清除与受影响模型相关的缓存。
namespace App; use Illuminate\Database\Eloquent\Model; use Suitmedia\Cacheable\Contracts\CacheableModel; use Suitmedia\Cacheable\Traits\Model\CacheableTrait; class Article extends Model implements CacheableModel { use CacheableTrait; }
准备您的仓库
您需要缓存的每个仓库也必须实现 CacheableRepository
接口。您可以通过使用 CacheableTrait
简单地实现该接口,如下所示
namespace App\Repositories; use App\Article; use Suitmedia\Cacheable\Traits\Repository\CacheableTrait; use Suitmedia\Cacheable\Contracts\CacheableRepository; class ArticleRepository implements CacheableRepository { use CacheableTrait; private $article; public function __construct(Article $article) { $this->article = $article; } public function model() { return $this->article; } /* |-------------------------------------------------------------------------- | Repository's method definition starts from here |-------------------------------------------------------------------------- */ public function all() { return $this->model->all(); } public function find($id) { return $this->model->find($videoId); } }
从仓库检索数据并缓存结果
使用此包,您无需为每个仓库创建新类来装饰。您只需使用 Cacheable
门面来装饰它们,然后仓库方法的全部结果将被自动缓存。
// Lets decorate the repository first $repo = Cacheable::wrap(ArticleRepository::class); // The result of these codes will be cached automatically $repo->all(); $repo->find(1);
缓存失效
此包将帮助您在 CacheableModel
更新时自动使缓存无效。它将根据在模型中定义的缓存标签使缓存无效。
但是,您也可以使用 Cacheable
门面手动使缓存无效。
// Flush everything Cacheable::flush(); // Flush the cache using a specific tag Cacheable::flush('Article'); // Flush the cache using a specific tag, // and only for the cache which belongs to a specific user Cacheable::flush('LikedArticle:User:7');
许可协议
MIT许可协议(MIT)。有关更多信息,请参阅许可文件。