Weave Code
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

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.

View on GitHub
Deep Wiki
Context7

kevinrob/guzzle-cache-middleware adds an RFC 7234–compliant HTTP cache to Guzzle 6+ via a simple middleware you push onto the top of a HandlerStack. It’s designed for transparent performance wins when your app repeatedly calls the same APIs during page rendering.

Pick a cache strategy and plug in your preferred storage implementation (including Laravel). It stays PSR-7 compatible and focuses on correctness and speed.

  • RFC 7234 caching semantics for real HTTP behavior
  • Drop-in Guzzle middleware integration
  • Multiple storages: Laravel Cache, Flysystem, PSR-6, PSR-16, WordPress Object Cache
  • Supports private/public caching strategies
  • Emphasis on performance and transparency
Frequently asked questions about Guzzle Cache Middleware
How do I integrate this package with Laravel’s HTTP client?
Use Laravel’s `HttpClient` facade or create a custom Guzzle client. Push the `CacheMiddleware` onto the `HandlerStack` before initializing the client. For Laravel Cache integration, wrap the middleware with `LaravelCacheStorage` and your preferred cache strategy (e.g., `PrivateCacheStrategy`). Example: `$client = new Client(['handler' => $stack]);` where `$stack` has the middleware pushed.
Which Laravel cache drivers are supported (Redis, database, etc.)?
All Laravel cache drivers are supported via `LaravelCacheStorage`. Configure it with your preferred store (e.g., `Cache::store('redis')`) and pass it to the middleware. The package abstracts driver-specific logic, so you can use Redis, Memcached, database, or file-based caching seamlessly.
Can I use this for authenticated API calls (e.g., private caching)?
Yes, use the `PrivateCacheStrategy` for authenticated or user-specific requests. This strategy respects `Authorization` headers and avoids caching responses for different users. Combine it with `LaravelCacheStorage` for Laravel’s cache drivers.
What’s the difference between `PublicCacheStrategy` and `PrivateCacheStrategy`?
`PublicCacheStrategy` caches responses for all users (e.g., public APIs) and ignores `Authorization` headers. `PrivateCacheStrategy` caches per-user responses and respects auth headers. Choose based on whether your API is shared (public) or user-specific (private).
Does this work with Laravel’s queue jobs (e.g., delayed API calls)?
Cached responses in queued jobs may not reflect real-time changes if the cache expires before the job executes. For critical data, use shorter TTLs or disable caching for queued jobs. Monitor cache hits/misses to validate behavior in production.
How do I handle APIs that don’t provide `Cache-Control` headers?
Use the `GreedyCacheStrategy` to override missing or unreliable `Cache-Control` headers. This forces caching but may violate API contracts. Reserve it for APIs with no caching metadata or inconsistent responses.
Is there a performance impact if the cache backend fails (e.g., Redis downtime)?
If the cache backend fails, the middleware falls back to no caching, but this adds latency for every request. For high availability, consider a fallback strategy (e.g., local filesystem cache) or implement retry logic for transient failures.
Can I test cache invalidation in unit tests?
Mock the cache layer using PHP’s `ArrayCachePool` (PSR-6) or Laravel’s `FakeCache` for unit tests. Verify invalidation by checking cache keys after updates or manually clearing the cache in tests. Avoid real backends to keep tests fast and deterministic.
How do I monitor cache hits/misses in production?
Log cache events (hits/misses) using Laravel’s logging or tools like Telescope. Track metrics like `cache_hit_ratio` to optimize TTLs or storage backends. For PSR-6 caches, some implementations (e.g., Predis) provide built-in stats.
What’s the best storage backend for high-traffic Laravel apps?
For high throughput, use Redis (via Laravel’s cache driver) or a dedicated PSR-6 cache like Predis. Avoid filesystem or database backends under heavy load, as they can become bottlenecks. Benchmark your storage with realistic traffic patterns.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport