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+ using a HandlerStack. Improve API call performance with transparent caching. Supports PSR-7 and multiple storages: Laravel cache, Flysystem, PSR-6/16, and WordPress object cache.

View on GitHub
Deep Wiki
Context7

A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.

Frequently asked questions about Guzzle Cache Middleware
How do I integrate this package with Laravel’s HttpClient facade?
Use the `cached` macro to inject the middleware into Laravel’s HttpClient. Example: `Http::macro('cached', fn($ttl = null) => Http::withOptions(['handler' => HandlerStack::create()->push(new CacheMiddleware(...), 'cache')]))`. This works seamlessly with Laravel 8+ and Guzzle 6+.
Which Laravel cache drivers are supported for storage?
The package supports all Laravel cache drivers (Redis, Memcached, database, file) via the `LaravelCacheStorage` adapter. Configure it by passing `Cache::store('driver')` to the storage adapter. For example, use `Cache::store('redis')` for Redis-backed caching.
Can I use this with non-Laravel Guzzle clients?
Yes. Inject the middleware directly into Guzzle’s `HandlerStack` for any client. Example: `$stack->push(new CacheMiddleware(new PrivateCacheStrategy(new LaravelCacheStorage(Cache::store('redis')))), 'cache');` Works with standalone Guzzle or custom HTTP clients.
What caching strategies are available, and when should I use each?
The package offers `PrivateCacheStrategy` (user-specific data), `PublicCacheStrategy` (shared data), `GreedyCacheStrategy` (aggressive caching), and `DelegateCacheStrategy` (dynamic routing). Use `Private` for sensitive endpoints, `Public` for static assets, and `Greedy` for APIs with unreliable `Cache-Control` headers.
How do I handle cache invalidation for dynamic API responses?
Use the `delete` method on the storage adapter to manually invalidate entries. Example: `$storage->delete($cacheKey)`. For automated invalidation, leverage Laravel’s cache tags or implement a `Cache-Control: no-cache` header check in your `GreedyCacheStrategy` configuration.
Does this package support caching binary responses like images or PDFs?
Yes, but ensure you’re using version 7.0.0+ to avoid truncation issues with binary data. The package handles binary responses transparently, provided the storage backend (e.g., Flysystem) supports binary storage.
How can I monitor cache hits and misses in Laravel?
Log cache events using Laravel’s `tap` or middleware. Example: `$stack->push(Middleware::tap(fn($request) => Log::info('Cache hit/miss', ['url' => $request->getUri()])));`. Alternatively, use a PSR-16 cache adapter with built-in logging.
What are the PHP and Laravel version requirements?
The package requires PHP 8.2+ and Laravel 8+ (for HttpClient integration). For Guzzle 6+ standalone use, PHP 7.4+ is sufficient. Check the [README](https://github.com/Kevinrob/guzzle-cache-middleware) for version-specific notes.
How do I test cache behavior in CI without hitting real APIs?
Mock storage backends using PHPUnit or Laravel’s `Cache::shouldReceive()`. For PSR-6/16, use `ArrayCachePool` or `ArrayCache` for in-memory testing. Example: `$storage = new LaravelCacheStorage(Cache::fake());` to simulate cache interactions.
Are there alternatives to this package for Laravel HTTP caching?
Alternatives include `spatie/laravel-http-cache` (Laravel-specific) or `symfony/http-client-cache` (Symfony-based). This package stands out for its Guzzle-native approach, RFC 7234 compliance, and support for multiple storage backends like Flysystem and PSR-6/16.
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.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope