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::classbefore or afterErrorHandler::classor 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 (Seecustom configurationsbelow).
Custom Configurations
-
default_ttl
Default time to life cache in second, default 3600 (or 1 hour).... 'mezzio_response_cache' => [ 'default_ttl' => 3600, ], ...
-
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', ], ], ...
-
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', ], ], ...
-
cache_handler_class
Class to handle cache mechanism. Custom class to handle cache middleware.... 'mezzio_response_cache' => [ 'cache_handler_class' => CacheHandler\CacheHandler::class, ], ...
-
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, ],
-
cache_metadata_location
Path location for metadata cache. Path directory location wherecache_storage_handler_classwill save cache metadata for default cache storage.'mezzio_response_cache' => [ 'cache_metadata_location' => './data/cache/response/content', ],
-
cache_content_location
Path location for actual cache content. Path directory location wherecache_storage_handler_classwill 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/helperto 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 :
o
The operation,clearordelete.k
Sha1 cache key, default generate withsha1($uriPath).p
Cache path, if you don't know what sha1 cache key, you can use path cache instead. If you usekandpat same time, helper will pickkvalue instead and ignorepquery parameter.
Documentation
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 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 7
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2022-07-14