danilovl/cache-response-bundle 问题修复 & 功能扩展

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

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

danilovl/cache-response-bundle

Composer 安装命令:

composer require danilovl/cache-response-bundle

包简介

Symfony bundle provides cache controller response

README 文档

README

phpunit downloads latest Stable Version license

CacheResponseBundle

About

This Symfony bundle provides simple response caching.

Before:

Alt text

After:

Alt text

Requirements

  • PHP 8.5 or higher
  • Symfony 8.0 or higher

1. Installation

Install the danilovl/cache-response-bundle package with Composer:

composer require danilovl/cache-response-bundle

Add the CacheResponseBundle to your application's bundles if it is not added automatically:

<?php
// config/bundles.php

return [
    // ...
    Danilovl\CacheResponseBundle\CacheResponseBundle::class => ['all' => true]
];

2. Usage

You can define a custom cache service which implements CacheItemPoolInterface.

Default cache service used for all attributes. You may leave it empty; the DI container will use the default service for CacheItemPoolInterface defined in your application.

# config/packages/danilovl_cache_response.yaml

danilovl_cache_response:
  enable: true/false
  cache_adapter: 'Class::class'
  kernel_controller_priority: 0
  kernel_response_priority: 0

CacheResponseAttribute options:

| Parameter         | Type                   | Default | Required        | Description                                                             |
| ----------------- | ---------------------- | ------- | --------------- | ----------------------------------------------------------------------- |
| $key              | ?string                | null    |  yes || factory | A custom cache key. If null, a key will be generated automatically.     |
| $factory          | ?string                | null    |  yes || key     | The class of the factory used to generate the value.                    |
| $cacheAdapter     | ?string                | null    |  no             | The class of the cache adapter.                                         |
| $expiresAfter     | int|DateInterval|null  | null    |  no             | Time after which the value expires. Can be seconds or a DateInterval.   |
| $expiresAt        | ?DateTimeInterface     | null    |  no             | Exact expiration time for the value.                                    |
| $useSession       | bool                   | false   |  no             | Whether to include session data in the cache key generation.            |
| $useRoute         | bool                   | false   |  no             | Whether to include the current route in the cache key.                  |
| $useQuery         | bool                   | false   |  no             | Whether to include query parameters in the cache key.                   |
| $useRequest       | bool                   | false   |  no             | Whether to include full request data in the cache key.                  |
| $useEnv           | bool                   | false   |  no             | Whether to include environment variables in the cache key.              |
| $disableOnQuery   | bool                   | false   |  no             | Disable cache entirely if GET (query) parameters are present.           |
| $disableOnRequest | bool                   | false   |  no             | Disable cache entirely if POST (request body) parameters are present.   |
|--------------------------------------------------------------------------------------------------------------------------------------------------|

2.1 Controller

Add the CacheResponseAttribute to a controller method.

#[CacheResponseAttribute(
    key: 'index', 
    expiresAfter: 60, 
    useSession: true, 
    useRoute: true, 
    useQuery: true, 
    useRequest: true,
    useEnv: true
)]
public function index(Request $request): Response
{
    return new Response('content');
}

Or, a better solution if you have duplicate controller and method names.

#[CacheResponseAttribute(
    key: __METHOD__, 
    expiresAfter: 60, 
    useQuery: true, 
    useRequest: true,
    useEnv: true
)]
public function index(Request $request): Response
{
    return new Response('content');
}

Use a custom factory service to create the cache key. It must implement the CacheKeyFactoryInterface.

#[CacheResponseAttribute(factory: CacheKeyFactoryClass::class)]
public function index(Request $request): Response
{
    return new Response('content');
}

2.2 Command

List all CacheResponseAttribute cache keys.

php bin/console danilovl:cache-response:list 

Alt text

Clear all CacheResponseAttribute caches.

php bin/console danilovl:cache-response:clear --all=true

Clear only a specific CacheResponseAttribute cache key.

php bin/console danilovl:cache-response:clear --cacheKey=index

Clear all caches with similar CacheResponseAttribute cache key names.

0 => "danilovl.cache_response.8414b2ff0a6fafcddc0f42d6d5a5b908d34925c3"
1 => "danilovl.cache_response.8414b2ff08997b2bd029eaab1a04598a500a0034"
php bin/console danilovl:cache-response:clear --similarCacheKey=8414b2ff0
0 => "danilovl.cache_response.5f9cf7121290f93c"
1 => "danilovl.cache_response.a202b43aa495f0f3"
php bin/console danilovl:cache-response:clear --cacheKey=similar

2.3 Event Subscriber

Clear all caches.

$this->eventDispatcher->dispatch(new ClearCacheResponseAllEvent);

Clear only a specific cache key.

$this->eventDispatcher->dispatch(new ClearCacheResponseKeyEvent('cache_key'));

License

The CacheResponseBundle is open-sourced software licensed under the MIT license.

danilovl/cache-response-bundle 适用场景与选型建议

danilovl/cache-response-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 143 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「php」 「bundle」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 danilovl/cache-response-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 danilovl/cache-response-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 danilovl/cache-response-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-06-09