devouted/symfony-as-cached-attribute
最新稳定版本:1.1.0
Composer 安装命令:
composer require devouted/symfony-as-cached-attribute
包简介
About
README 文档
README
Symfony attribute-based HTTP response caching. Mark controller actions with #[AsCachedResponse] to automatically cache responses.
Installation
composer require devouted/symfony-as-cached-attribute
Usage
Service configuration
Add to your services.yaml:
services: Devouted\AsCachedAttribute\Listener\CachedResponseListener: arguments: $cache: '@cache.app' $logger: '@monolog.logger' # optional, set to ~ to disable $modifyResponseCacheHeaders: true # optional, set to false to skip Cache-Control headers tags: - { name: kernel.event_listener, event: kernel.controller_arguments, priority: -1 } - { name: kernel.event_listener, event: kernel.response, priority: 1000 }
Basic usage
#[AsCachedResponse] public function __invoke(): Response { return new Response('cached content'); }
or on a regular controller action:
#[AsCachedResponse] public function someAction(Request $request): Response { return new Response('cached content'); }
Cache key parametrization
Use #[AsCachedRequestParameter] on method parameters or DTO properties to build unique cache keys per request:
On method parameters:
#[AsCachedResponse] public function show( #[AsCachedRequestParameter] int $id, #[AsCachedRequestParameter] string $slug ): Response { return new Response("id={$id}, slug={$slug}"); }
On DTO properties (e.g. with #[MapQueryString] or #[MapRequestPayload]):
#[AsCachedResponse] public function list(#[MapQueryString] ProductFilter $filter): Response { return new Response("filtered list"); } class ProductFilter { public function __construct( #[AsCachedRequestParameter] public int $categoryId, #[AsCachedRequestParameter] public ?string $sort = null, ) {} }
AsCachedResponse options
#[AsCachedResponse(
ttl: 3600, // cache lifetime in seconds (default: 3600)
etag: 'my-etag', // optional ETag header value
expires: 'tomorrow', // optional Expires header (any strtotime-compatible string)
isPublic: true, // public (shared) or private cache (default: true)
)]
Response headers
Cached responses include an X-Cache header:
Miss— response was generated and stored in cacheHit— response was served from cache
统计信息
- 总下载量: 15
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-02-09