ryudith / mezzio-response-cache
Cache response based URL route key for Mezzio.
Installs: 0
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ryudith/mezzio-response-cache
Requires
- php: ^8.0
- laminas/laminas-diactoros: ^2.7
- psr/cache: ^1.0.1
- psr/container: ^1.0
- psr/http-server-middleware: ^1.0
- psr/simple-cache: ^1.0.1
- symfony/console: ^6
Requires (Dev)
- guzzlehttp/guzzle: ^7.4.5
- phpunit/phpunit: ^9.5.11
This package is auto-updated.
Last update: 2025-10-14 20:00:51 UTC
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.