Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Guzzle Cache Middleware

Guzzle Cache Middleware Laravel Package

kevinrob/guzzle-cache-middleware

A PSR-7 compatible HTTP cache middleware for Guzzle 6+ with RFC 7234 compliance. Pluggable storage backends include Laravel cache, Flysystem, PSR-6, PSR-16, and WordPress Object Cache. Add it to the HandlerStack to speed up repeated API calls.

View on GitHub
Deep Wiki
Context7

kevinrob/guzzle-cache-middleware is a transparent HTTP cache for Guzzle 6+, added as a simple middleware at the top of the HandlerStack. It follows RFC 7234 to store and reuse responses, cutting down repeated API calls and speeding up page rendering.

  • Multiple storage backends — Laravel cache, Flysystem, PSR-6, PSR-16, and WordPress Object Cache
  • RFC 7234 compliant caching with public and private strategies
  • PSR-7 compatible for seamless integration
  • Zero-config start — one push() call enables caching
  • Performance-focused and transparent to existing Guzzle clients
Frequently asked questions about Guzzle Cache Middleware
How do I install kevinrob/guzzle-cache-middleware in a Laravel application?
Run composer require kevinrob/guzzle-cache-middleware, then push CacheMiddleware onto a Guzzle HandlerStack and pass that stack as the client handler. In Laravel you can wrap LaravelCacheStorage around any Illuminate\Contracts\Cache\Repository, such as Cache::store('redis'). The package does not ship a Laravel service provider, so you register the configured client or singleton yourself.
Does this package support Laravel 10, 11, and 12?
The middleware is framework-agnostic and is not tied to a specific Laravel release; its Laravel integration is the LaravelCacheStorage adapter, which accepts an Illuminate\Contracts\Cache\Repository. The 7.x line is the stable choice for existing applications, while 8.0.0-RC1 targets PHP 8.2+ and newer Laravel/Symfony platform versions. Check the package's Composer constraints against your PHP and Laravel versions before upgrading.
How do I use Laravel's Redis cache store as the Guzzle cache backend?
Create a PrivateCacheStrategy with a LaravelCacheStorage instance that wraps Cache::store('redis'), then push the CacheMiddleware onto your HandlerStack. The same adapter works with any Laravel cache repository, so you can start on Redis and move to another store later without changing cache logic. Scope that store or prefix per upstream if you need isolation.
Should I add the cache middleware globally to Laravel's HTTP client?
Do not attach it globally by default. A global HandlerStack can cache authenticated or user-scoped responses in a shared store and serve them to another user or tenant. Prefer a named client per upstream with the stack baked in, and only use Http::globalOptions(['handler' => $stack]) after confirming your Laravel version supports it and you have explicit cache-key rules.
What is the difference between PrivateCacheStrategy, PublicCacheStrategy, and GreedyCacheStrategy?
PrivateCacheStrategy is intended for private/client-side cache semantics, while PublicCacheStrategy models a shared cache and must not be used blindly on authenticated endpoints. GreedyCacheStrategy caches more aggressively and is commonly paired with KeyValueHttpHeader(['Authorization']) so credentials participate in the key. There is also NullCacheStrategy to disable caching and DelegatingCacheStrategy with RequestMatcherInterface for per-request policy selection.
How do I stop cached authenticated responses from leaking between users?
Cache keys are derived from method, URI, and Vary headers, so an upstream that omits Vary: Authorization can cause a private response to be served from a shared Redis store to another user or tenant. Never use PublicCacheStrategy on authenticated endpoints; use GreedyCacheStrategy with KeyValueHttpHeader(['Authorization']) or namespace the cache store per tenant. Make this a written rule, not a code-review convention.
How can I test cache expiry deterministically with PHPUnit?
Use a PSR-6 ArrayCachePool for the storage and, on v8, inject a PSR-20 clock so expiry assertions do not depend on real time. You can then advance the fake clock and assert hits, misses, and revalidation without sleeping in tests. Also test Vary handling and Authorization key isolation, since those are the highest-risk behaviors.
Is kevinrob/guzzle-cache-middleware actively maintained and safe for production?
It is a small MIT-licensed package with a single primary maintainer and relatively low recorded dependents, so maintenance concentration is a real risk rather than a functional defect. The 7.x line is the stable choice; v8.0.0-RC1 adds newer platform support but is still an RC. Pin the version, review release notes, and keep an exit plan because the code is small enough to fork if needed.
Which Guzzle and PHP versions does this HTTP cache middleware support?
The package is a Guzzle 6+ HandlerStack middleware and is PSR-7 compatible. The stable 7.x releases cover common PHP versions, while 8.0.0-RC1 requires PHP 8.2 or newer and targets newer Laravel and Symfony support. Always verify the exact Composer platform constraints for the version you pin.
What are alternatives to kevinrob/guzzle-cache-middleware for caching Guzzle requests?
Alternatives include php-http/cache-plugin and Symfony's CachingHttpClient, though the latter is an awkward fit in a Laravel app that already standardizes on Guzzle. Application-level Cache::remember is useful but requires wrapping every call site and does not honor upstream Cache-Control or Vary headers. If this package is abandoned, its small MIT codebase is practical to fork.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
andydefer/laravel-notification
adachsoft/cli-process-tester
jhaskell13/laraflow
sebastienheyd/boilerplate-media-manager
ccast/tagixo-primix
pretendtrue/laravel-builder
sawmainek/laravel-scout-tntsearch-driver
tomshaw/laravel-calendar-table
dibakar/laravel-ownership
steadrun/laravel-monitor
labyrinthe/connect-laravel
elmogy/layan
drewdan/sentinel-client
dskripchenko/laravel-llm
fatryst/nuonuo
stevenay/myanfont
gmostafa/php-graphql-client
xeviant/paystack
snowsoft/faker
meditationsteps/blogetc