rtheunissen / guzzle-cache-handler
Guzzle 6/7 handler used to cache responses
Installs: 131 884
Dependents: 1
Suggesters: 0
Security: 0
Stars: 12
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: >=7.2
- guzzlehttp/guzzle: ~6.0|~7.0
- psr/log: ~1.0|~2.0|~3.0
- rtheunissen/cache: ~0.1
Requires (Dev)
- doctrine/cache: ~1.4
- mockery/mockery: ^1.6
- phpunit/phpunit: ^9.3
README
Installation
composer require rtheunissen/guzzle-cache-handler
Usage
This is a handler which caches responses for a given amount of time.
You will need an implemented CacheInterface. See rtheunissen/cache for more details.
use Concat\Http\Handler\CacheHandler; use Doctrine\Common\Cache\FilesystemCache; use GuzzleHttp\Client; // Basic directory cache example $cacheProvider = new FilesystemCache(__DIR__ . '/cache'); // Guzzle will determine an appropriate default handler if `null` is given. $defaultHandler = null; // Create a cache handler with a given cache provider and default handler. $handler = new CacheHandler($cacheProvider, $defaultHandler, [ /** * @var array HTTP methods that should be cached. */ 'methods' => ['GET', 'HEAD', 'OPTIONS'], /** * @var integer Time in seconds to cache a response for. */ 'expire' => 60, /** * @var callable Accepts a request and returns true if it should be cached. */ 'filter' => null, ]); // Use a PSR-3 compliant logger to log when bundles are stored or fetched. $handler->setLogger($logger); // Create a Guzzle 6/7 client, passing the cache handler as 'handler'. $client = new Client([ 'handler' => $handler ]);