ryudith/mezzio-response-cache

为 Mezzio 缓存响应的 URL 路由键。

dev-master 2022-07-12 08:05 UTC

This package is auto-updated.

Last update: 2024-09-14 17:43:06 UTC


README

Ryudith\MezzioResponseCache 是 Mezzio 框架的中间件,用于保存由请求产生的响应(默认使用请求路径作为缓存键)。

安装

要安装,请运行以下命令

$ composer require ryudith/mezzio-response-cache

用法

Ryudith\MezzioResponseCache\ConfigProvider 添加到 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']);

...

Ryudith\MezzioResponseCache\ResponseCacheMiddleware 添加到 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

    ...

};

您可以将 ResponseCacheMiddleware::class 放在 ErrorHandler::class 之前或之后,或者放在您需要的任何位置。基本上,中间件会检查缓存路径键(默认行为),如果不存在,则生成响应,否则会提供缓存。它还支持自定义配置来排除路径或 IP 从缓存(例如,见下面的“自定义配置”)。

自定义配置

  1. default_ttl
    默认缓存的生命周期时间(以秒为单位),默认为 3600(或 1 小时)。

    ...
    'mezzio_response_cache' => [
       'default_ttl' => 3600,
    ],
    ...
  2. exclude_ip_from_cache
    要排除缓存的 IP 列表(列表数组),默认为空数组。

    ...
    'mezzio_response_cache' => [
       'exclude_ip_from_cache' => [
           '192.168.0.1', 
           '127.0.0.1',
       ],
    ],
    ...
  3. exlcude_path_from_cache
    要排除缓存的路由路径列表(列表数组),默认为空数组。

    ...
    'mezzio_response_cache' => [
       'exlcude_path_from_cache' => [
           '/about', 
           '/api/post',
       ],
    ],
    ...
  4. cache_handler_class
    用于处理缓存机制的类。用于处理缓存中间件的自定义类。

    ...
    'mezzio_response_cache' => [
       'cache_handler_class' => CacheHandler\CacheHandler::class,
    ],
    ...
  5. cache_storage_handler_class
    用于处理缓存存储机制的类。用于处理中间件中缓存处理程序的缓存存储的自定义类,默认为文件系统存储。

    'mezzio_response_cache' => [
       'cache_storage_handler_class' => Storage\FileSystemCacheHandler::class,
    ],
  6. cache_metadata_location
    元数据缓存的路径位置。默认缓存存储中 cache_storage_handler_class 将保存缓存元数据的路径目录位置。

    'mezzio_response_cache' => [
       'cache_metadata_location' => './data/cache/response/content',
    ],
  7. cache_content_location
    实际缓存内容的路径位置。默认缓存存储中 cache_storage_handler_class 将保存缓存内容数据的路径目录位置。

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

简单助手

Web

Ryudith\MezzioResponseCache\Helper\WebHandlerCache 添加到 factories 配置,通常在文件 config/dependencies.global.php 中。

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

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

然后在文件 config/route.php 中注册助手到路由。

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

将路由路径 /cacheresponse/helper 更改为您自己的路由路径。

然后您可以通过浏览器从 https://:8080/cacheresponse/helper?o=clearhttps://:8080/cacheresponse/helper?o=delete&p=/about 访问简单的 Web 助手。

根据您的 Mezzio 应用程序配置更改地址(上面的地址只是示例)。

简单 Web 助手使用的查询参数是

  1. o
    操作,cleardelete
  2. k
    Sha1 缓存键,默认使用 sha1($uriPath) 生成。
  3. p
    缓存路径,如果您不知道 Sha1 缓存键,可以使用路径缓存。如果您同时使用 kp,助手将选择 k 的值并忽略 p 查询参数。

文档

API 文档

问题或疑问