danilovl/cache-response-bundle

Symfony 扩展提供缓存控制器响应

安装: 14

依赖者: 0

建议者: 0

安全: 0

星星: 0

观察者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

v0.5.3 2024-03-30 07:49 UTC

This package is auto-updated.

Last update: 2024-09-30 08:55:37 UTC


README

phpunit downloads latest Stable Version license

CacheResponseBundle

关于

Symfony 扩展提供简单的缓存响应。

之前

Alt text

之后

Alt text

要求

  • 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 许可证