danilovl / cache-response-bundle
Symfony 扩展提供缓存控制器响应
v0.5.3
2024-03-30 07:49 UTC
Requires
- php: ^8.3
- symfony/cache: ^7.0
- symfony/console: ^7.0
- symfony/event-dispatcher: ^7.0
- symfony/framework-bundle: ^7.0
- symfony/http-foundation: ^7.0
- symfony/http-kernel: ^7.0
Requires (Dev)
- phpunit/phpunit: ^10.2
README
CacheResponseBundle
关于
Symfony 扩展提供简单的缓存响应。
之前
之后
要求
- PHP 8.3 或更高版本
- Symfony 7.0 或更高版本
1. 安装
使用 Composer 安装 danilovl/cache-response-bundle 包
composer require danilovl/cache-response-bundle
如果自动添加不成功,请将 CacheResponseBundle 添加到您的应用程序的包中
<?php // config/bundles.php return [ // ... Danilovl\CacheResponseBundle\CacheResponseBundle::class => ['all' => true] ];
2. 使用方法
您可以定义一个自定义缓存服务,该服务实现了 CacheItemPoolInterface。
# config/packages/danilovl_cache_response.yaml danilovl_cache_response: service: You service name
2.1 控制器
将 CacheResponseAttribute 属性添加到控制器方法中。
#[CacheResponseAttribute( cacheKey: 'index', expiresAfter: 60, cacheKeyWithQuery: true, cacheKeyWithRequest: true )] public function index(Request $request): Response { return new Response('content'); }
如果您有重复的控制器名称和方法名称,这是一个更好的解决方案。
#[CacheResponseAttribute( cacheKey: __METHOD__, expiresAfter: 60, cacheKeyWithQuery: true, cacheKeyWithRequest: true )] public function index(Request $request): Response { return new Response('content'); }
使用自定义工厂服务来创建缓存键。必须实现 CacheKeyFactoryInterface 接口。
#[CacheResponseAttribute(cacheKeyFactory: CachKeyFactoryClass::class)] public function index(Request $request): Response { return new Response('content'); }
2.2 命令
显示所有使用的 CacheResponseAttribute 缓存键名称。
bin/console danilovl:cache-response:list
清除所有 CacheResponseAttribute 缓存。
bin/console danilovl:cache-response:clear --all=true
仅清除特定的 CacheResponseAttribute 缓存键名称。
bin/console danilovl:cache-response:clear --cacheKey=index
2.3 事件订阅者
清除所有缓存。
$this->eventDispatcher->dispatch(new ClearCacheResponseAllEvent);
仅清除特定的缓存键。
$this->eventDispatcher->dispatch(new ClearCacheResponseKeyEvent('cache_key'));
许可证
CacheResponseBundle 是开源软件,许可协议为 MIT 许可证。

