devouted/symfony-as-cached-attribute 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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 cache
  • Hit — response was served from cache

统计信息

  • 总下载量: 15
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-09

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固