Simple caching middleware for Guzzle, works well with Laravel or with any cache system implementing the PSR-16 caching interface.
composer require brightfish/caching-guzzle
use Brightfish\CachingGuzzle\Client;
/** @var \Psr\SimpleCache\CacheInterface $store */
$store = app('cache')->store('database');
$client = new Client($store, [
'cache_ttl' => 12345,
'cache_log' => app()->environment('local')
'base_uri' => 'https://example.org/api'
]);
# This response will be cached
$response_1 = $client->get('/resource', [
'cache_ttl' => 3600
]);
# This response will not be cached
$response_2 = $client->post('/resource/84', [
'cache' => false
]);
# Get response_1 from cache
$cached_response = $store->get('//example.org/api/resource');
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Brightfish\CachingGuzzle\Middleware;
$store = app('cache')->store('database');
$handler = new CacheMiddleware($store, 3600);
$stack = HandlerStack::create();
$stack->push($handler);
$client = new Client(['handler' => $stack]);
$cache (bool)
Whether to disable the cache for this specific request$cache_ttl (int)
Specific time to live in minutes for this request
$response_1 = $client->get('/resource', [
'cache' => false
'cache_ttl' => 3600
]);
$cache (\Psr\SimpleCache\CacheInterface)
Cache handler implementation$cache_ttl (int)
Default time to live in minutes$cache_log (bool)
Whether to log the cache requests (in Laravel)
$client = new Client($cache, [
'cache_ttl' => 12345,
'cache_log' => app()->environment('local')
'base_uri' => 'https://example.org/api'
]);
$cache (\Psr\SimpleCache\CacheInterface)
Cache handler implementation$ttl (int)
Default time to live in minutes$log (bool)
Whether to log the cache requests (in Laravel)
$handler = new CacheMiddleware($cache, $ttl, $log);
GNU General Public License (GPL). Please see License File for more information.