kevinrob/guzzle-cache-middleware
RFC 7234-compliant HTTP cache middleware for Guzzle 6+. Add to a HandlerStack to transparently cache responses and speed up API calls. Supports PSR-7 and multiple backends: Laravel Cache, Flysystem, PSR-6/16, and WordPress object cache.
HandlerStack middleware pattern, making it a natural fit for Laravel applications that already use Guzzle (e.g., for API calls, HTTP clients, or external service integrations). It integrates seamlessly without requiring architectural overhauls.Illuminate\Cache, League\Flysystem).LaravelCacheStorage. No additional infrastructure needed for most use cases.HandlerStack). Advanced configurations (e.g., delegating strategies) add complexity but remain optional.HttpClient facade). No version conflicts with Laravel’s default Guzzle version.PublicCacheStrategy for authenticated endpoints) could lead to stale data or security leaks. Requires careful validation of Vary/Authorization headers.Cache-Control headers (via GreedyCacheStrategy) may violate API contracts. Should be used only for unreliable APIs.GreedyCacheStrategy)?ArrayCachePool)?HttpClient facade or custom Guzzle instances.LaravelCacheStorage.shouldQueue) that fetch external data.PrivateCacheStrategy + Laravel Redis for low-risk testing.PrivateCacheStrategy with Authorization headers excluded from cache keys).NullCacheStrategy as a default, with feature flags to toggle caching per client.composer require kevinrob/guzzle-cache-middleware.$app->singleton(CacheMiddleware::class, fn() => new CacheMiddleware(
new PrivateCacheStrategy(new LaravelCacheStorage(Cache::store('redis')))
));
HandlerStack in a service or facade:
use GuzzleHttp\HandlerStack;
use Kevinrob\GuzzleCache\CacheMiddleware;
$stack = HandlerStack::create();
$stack->push(app(CacheMiddleware::class), 'cache');
$client = new Client(['handler' => $stack]);
RequestMatcherInterface for dynamic routing.Cache-Control headers.Vary/Authorization headers are handled correctly.$middleware = new CacheMiddleware($strategy);
$middleware->onCacheHit(fn($request, $response) => Log::debug('Cache hit', ['url' => $request->getUri()]));
CacheMiddleware::inspectAll() to debug entries.stream_get_contents() is used for large responses (fixed in v7.0.0).How can I help you explore Laravel packages today?