foxws/laravel-modelcache
最新稳定版本:1.1.0
Composer 安装命令:
composer require foxws/laravel-modelcache
包简介
Cache helpers for Laravel Eloquent models
README 文档
README
This package allows the Laravel Cache driver to be easily used for model instances. By default, logged in users will have their own separate cache-prefix.
Installation
Install the package via composer:
composer require foxws/laravel-modelcache
Publish the config file with:
php artisan vendor:publish --tag="modelcache-config"
Usage
Model Concern
Implement the Foxws\ModelCache\Concerns\InteractsWithModelCache trait to your Eloquent model:
use Foxws\ModelCache\Concerns\InteractsWithModelCache; use Illuminate\Database\Eloquent\Model; class Video extends Model { use InteractsWithModelCache; }
Facade
It is also possible to use the ModelCache Facade directly:
use Foxws\ModelCache\Facades\ModelCache; class MyActionClass { public function handle(Video $model): void { if (! ModelCache::enabled()) { // modelcaching is disabled return; } ModelCache::cache($model, 'foo', 'bar'); ModelCache::hasBeenCached($model, 'foo'); ModelCache::getCachedValue($model, 'foo'); } }
Model instance
To put a cache value for a model instance:
Video::first()->modelCache('currentTime', 20); Video::first()->modelCache('randomSeed', 20, now()->addDay()); // cache for one day
To retrieve a cached model instance value:
Video::first()->modelCached('currentTime'); Video::first()->modelCached('randomSeed', $default); // with fallback
To validate if a cached model instance value exists:
$model = Video::findOrFail(10); if (! $model->hasModelCache('currentTime')) { $model->modelCache('currentTime', 20); } return $model->modelCached('currentTime');
To forget a cached model value:
Video::first()->modelCacheForget('currentTime'); Video::first()->modelCacheForget('randomSeed');
Model caching (global)
To put a model cache value based on its class:
Video::setModelCache('randomSeed', 0.1); Video::setModelCache('randomSeed', 0.1, now()->addDay()); // cache for one day
To retrieve a model class cached value:
Video::getModelCache('randomSeed'); Video::getModelCache('randomSeed', $default);
To validate if a model class cached value exists:
Video::hasModelCache('randomSeed');
To forget a model class cached value:
Video::forgetModelCache('randomSeed');
Creating a custom cache profile
To determine which values should be cached, a cache profile class is used. The default class that handles these questions is Foxws\ModelCache\CacheProfiles\CacheAllSuccessful.
You can create your own cache profile class by implementing the Foxws\ModelCache\CacheProfile\CacheProfile, and overruling the cache_profile in config/modelcache.php.
It is also possible to overrule the cache prefix using the model instance. For this create a method named cacheNameSuffix on the model instance:
use Foxws\ModelCache\Concerns\InteractsWithModelCache; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Auth; class Video extends Model { use InteractsWithModelCache; protected function cacheNameSuffix(string $key): string { // return Auth::check() // ? (string) Auth::id() // : ''; // return "{$key}:{$this->getMorphClass()}"; return ''; // do not use a separate cache for users } }
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
This package is entirely based on the space/laravel-responsecache package.
Please consider to sponsor Spatie, such as purchasing their excellent courses. :)
License
The MIT License (MIT). Please see License File for more information.
统计信息
- 总下载量: 4.16k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-28