rtheunissen/guzzle-cache-handler

Guzzle 6/7 handler used to cache responses

v2.0.0 2023-07-19 22:04 UTC

This package is auto-updated.

Last update: 2024-12-20 01:00:09 UTC


README

Author License Latest Version Build Status Scrutinizer Scrutinizer Coverage

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
]);