composer require rtheunissen/guzzle-cache-handler
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
]);