kolirt / laravel-cacheable
轻松缓存并控制类方法,无需记住缓存键名
1.1.0
2024-09-26 20:09 UTC
Requires
- php: >=8.1
- laravel/framework: >=10.0
This package is auto-updated.
Last update: 2024-09-26 20:13:03 UTC
README
轻松缓存并控制类方法,无需记住缓存键名
结构
入门
轻松缓存并控制类方法,无需记住缓存键名
要求
- PHP >= 8.1
- Laravel >= 10
安装
composer require kolirt/laravel-cacheable
设置
发布配置文件
php artisan cacheable:install
默认情况下,Laravel存在多标签问题,导致缓存中数据过度重复,清除和读取缓存时出现意外行为。您可以在此处了解更多信息这里。要解决这个问题,将以下代码添加到composer.json中,并运行composer dump-autoload
{
"autoload": {
"exclude-from-classmap": [
"vendor/laravel/framework/src/Illuminate/Cache/TaggedCache.php"
],
"files": [
"vendor/kolirt/laravel-cacheable/src/Overrides/TaggedCache.php"
]
}
}
在目标类中使用Cacheable特性
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; }
控制台命令
cacheable:install- 安装缓存包cacheable:publish-config- 发布配置文件
方法
cache
使用cache方法,缓存您需要的一切
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function exampleMethod() { return $this->cache(fn () => 'example data'); } public function exampleMethodWithParams(int $id) { return $this->cache(fn () => 'example data with id ' . $id); } }
clearCache
要清除缓存,请使用clearCache方法
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function clearExampleMethod() { $this->clearCache('exampleMethod'); } public function clearExampleMethodWithParams(int $id) { $this->clearCache('exampleMethodWithParams', $id); } }
updateCache
要更新缓存,请使用updateCache方法
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function updateExampleMethod() { $this->updateCache('exampleMethod', 'new example data'); } }
refreshCache
要刷新缓存,请使用refreshCache方法
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function exampleMethod() { return $this->cache(fn () => 'example data'); } public function refreshExampleMethod() { $this->refreshCache('exampleMethod'); } }
setCacheTime
要设置缓存时间,请使用setCacheTime方法
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function __construct() { $this->setCacheTime(now()->endOfDay()); } }
flushAllCache
清除所有缓存支持标签。您需要将类切换到可标记模式
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; protected bool $taggable = true; }
或者,您可以通过使用appendCacheTags方法在不启用可标记模式的情况下给类添加标签
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; public function __construct() { $this->appendCacheTags(['tag1', 'tag2']); } }
要清除所有缓存,请使用flushAllCache方法
$example = new Example(); $example->flushAllCache();
appendCacheTags
除了在可标记模式下自动添加的基本标签外,您还可以使用appendCacheTags方法添加您需要的其他标签
use Kolirt\Cacheable\Traits\Cacheable; class Example { use Cacheable; protected bool $taggable = true; /** add additional tags for all methods */ public function __construct() { $this->appendCacheTags(['tag1', 'tag2']); } /** or add additional tags for specific method */ public function exampleMethod() { $this->appendCacheTags(['tag1', 'tag2']); return $this->cache(fn () => 'example data'); } }
然后,通过Cache外观,您可以删除您需要的标签的缓存
use Illuminate\Support\Facades\Cache; Cache::tags(['tag1'])->flush(); Cache::tags(['tag2'])->flush(); Cache::tags(['tag1', 'tag2'])->flush();
常见问题解答
检查已关闭的问题以获取最常问问题的答案
许可证
其他包
请访问我的GitHub个人资料查看我的其他包