ryudith/mezzio-response-cache 问题修复 & 功能扩展

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

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

ryudith/mezzio-response-cache

Composer 安装命令:

composer require ryudith/mezzio-response-cache

包简介

Cache response based URL route key for Mezzio.

README 文档

README

Ryudith\MezzioResponseCache is middleware for Mezzio framework to save response produce by request (default use request path as cache key).

Installation

To install run command :

$ composer require ryudith/mezzio-response-cache

Usage

Add Ryudith\MezzioResponseCache\ConfigProvider to config/config.php

...

$aggregator = new ConfigAggregator([
    ...
    \Laminas\Diactoros\ConfigProvider::class,

    Ryudith\MezzioResponseCache\ConfigProvider::class,  // <= add this line

    // Swoole config to overwrite some services (if installed)
    class_exists(\Mezzio\Swoole\ConfigProvider::class)
        ? \Mezzio\Swoole\ConfigProvider::class
        : function (): array {
            return [];
        },

    // Default App module config
    App\ConfigProvider::class,

    // Load application config in a pre-defined order in such a way that local settings
    // overwrite global settings. (Loaded as first to last):
    //   - `global.php`
    //   - `*.global.php`
    //   - `local.php`
    //   - `*.local.php`
    new PhpFileProvider(realpath(__DIR__) . '/autoload/{{,*.}global,{,*.}local}.php'),

    // Load development config if it exists
    new PhpFileProvider(realpath(__DIR__) . '/development.config.php'),
], $cacheConfig['config_cache_path']);

...

Add Ryudith\MezzioResponseCache\ResponseCacheMiddleware to config/pipeline.php

...

return function (Application $app, MiddlewareFactory $factory, ContainerInterface $container): void {
    // The error handler should be the first (most outer) middleware to catch
    // all Exceptions.
    $app->pipe(ErrorHandler::class);
    $app->pipe(ResponseCacheMiddleware::class);  // <= add this line

    ...

};

You can put ResponseCacheMiddleware::class before or after ErrorHandler::class or anywhere you want depend on you need. Basically, middleware do check cache path key (default behavior) if not exists then generate response else it will give cache. It also support custom configurations for exclude path or IP from cache for example (See custom configurations below).

Custom Configurations

  1. default_ttl
    Default time to life cache in second, default 3600 (or 1 hour).

    ...
    'mezzio_response_cache' => [
       'default_ttl' => 3600,
    ],
    ...
  2. exclude_ip_from_cache
    IP list to exclude cache (list array), default empty array.

    ...
    'mezzio_response_cache' => [
       'exclude_ip_from_cache' => [
           '192.168.0.1', 
           '127.0.0.1',
       ],
    ],
    ...
  3. exlcude_path_from_cache
    Route path list to exclude cache (list array), default empty array.

    ...
    'mezzio_response_cache' => [
       'exlcude_path_from_cache' => [
           '/about', 
           '/api/post',
       ],
    ],
    ...
  4. cache_handler_class
    Class to handle cache mechanism. Custom class to handle cache middleware.

    ...
    'mezzio_response_cache' => [
       'cache_handler_class' => CacheHandler\CacheHandler::class,
    ],
    ...
  5. cache_storage_handler_class
    Class to handle cache storage mechanism. Custom class to handle cache storage for cache handler in middleware, default is file system storage.

    'mezzio_response_cache' => [
       'cache_storage_handler_class' => Storage\FileSystemCacheHandler::class,
    ],
  6. cache_metadata_location
    Path location for metadata cache. Path directory location where cache_storage_handler_class will save cache metadata for default cache storage.

    'mezzio_response_cache' => [
       'cache_metadata_location' => './data/cache/response/content',
    ],
  7. cache_content_location
    Path location for actual cache content. Path directory location where cache_storage_handler_class will save cache content data for default cache storage.

    'mezzio_response_cache' => [
       'cache_content_location' => './data/cache/response/content',
    ],

Simple Helper

Web

Add Ryudith\MezzioResponseCache\Helper\WebHandlerCache to factories configuration, usually inside file config/dependencies.global.php.

...
'factories' => [
    ...

    // add this to enable web simple helper
    Ryudith\MezzioResponseCache\Helper\WebHandlerCache::class => Ryudith\MezzioResponseCache\Helper\WebHandlerCacheFactory::class
    ...
]
...

Then register helper to route in file config/route.php.

...
$app->get('/cacheresponse/helper', Ryudith\MezzioResponseCache\Helper\WebHandlerCache::class);
...

change route path /cacheresponse/helper to your own route path.

Next you can access simple web helper from http://localhost:8080/cacheresponse/helper?o=clear or http://localhost:8080/cacheresponse/helper?o=delete&p=/about from browser.

change address depend your Mezzio app configuration (address above is just example).

Query parameter used by simple web helper is :

  1. o
    The operation, clear or delete.
  2. k
    Sha1 cache key, default generate with sha1($uriPath).
  3. p
    Cache path, if you don't know what sha1 cache key, you can use path cache instead. If you use k and p at same time, helper will pick k value instead and ignore p query parameter.

Documentation

API Documentation

Issues or Questions

ryudith/mezzio-response-cache 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2022-07-14