- How do I integrate this Guzzle cache middleware into a Laravel application?
- Install via Composer (`composer req csa/guzzle-cache-middleware`), then register it in your Guzzle handler stack. For Laravel, wrap it in a service provider to inject Laravel’s Cache facade (Redis, file, etc.) as the PSR-16-compliant store. Example: `$stack->push(Middleware::cache($cacheStore));`.
- Does this work with Laravel’s built-in HTTP client (Laravel 8+)?
- Yes, but indirectly. Since Laravel’s HTTP client uses Guzzle under the hood, you can extend its middleware stack by creating a custom middleware that delegates to this package. Alternatively, use it with standalone Guzzle clients in your app.
- What cache backends are supported (Redis, file, database, etc.)?
- Any PSR-16-compliant cache store, including Laravel’s default drivers (Redis, Memcached, file, database). The package itself is backend-agnostic, so you can use Laravel’s `Cache::store()` or inject a custom PSR-16 implementation like `Predis` or `Doctrine Cache`.
- How do I set cache TTL (time-to-live) or invalidate stale data?
- Configure TTL when creating the middleware: `Middleware::cache($cacheStore, 300)` for 5-minute caching. For invalidation, manually clear keys or use Laravel’s cache tags. For API-driven invalidation, listen to events (e.g., `Model::saved`) and clear relevant cache entries.
- Is this package actively maintained? Should I use it in production?
- The project is archived, so evaluate alternatives like `guzzlehttp/cache` or Symfony’s HTTP client for active maintenance. If you proceed, test thoroughly in staging and monitor cache hit/miss ratios. Consider forking if critical bugs arise.
- Can I cache POST/PUT requests or only GET/HEAD?
- This middleware caches **only GET/HEAD** requests by design, as caching mutable HTTP methods (POST, PUT, DELETE) is unsafe and violates HTTP standards. For POST caching, consider storing request/response pairs in a dedicated cache layer.
- How do I handle cache misses or failed requests?
- The middleware falls back to the original request on cache misses. For failed requests, configure Guzzle’s retry middleware alongside this one. Log cache misses via Guzzle’s `onRequest`/`onResponse` hooks or wrap the middleware in a Laravel service to track metrics.
- Will this work with Laravel 10 and Guzzle 7?
- Yes, the package supports Guzzle 6+, which includes Guzzle 7. Laravel 10’s default Guzzle version (v7) is fully compatible. Test with your specific Guzzle version, as minor breaking changes may exist between Guzzle 6 and 7.
- How do I test this middleware in Laravel’s PHPUnit tests?
- Mock the Guzzle client and middleware stack using Laravel’s `Http::fake()` or PHPUnit’s `Mockery`. Example: `$this->mock(GuzzleCacheMiddleware::class)->shouldReceive('__invoke')->andReturn($response);`. For integration tests, use Laravel’s HTTP tests with a test cache driver.
- What are the alternatives to this package for Laravel?
- Consider `guzzlehttp/cache` (more maintained, but less Laravel-friendly), Symfony’s `HttpClient` (built-in caching), or Laravel’s `Cache::remember()` for simple cases. For a Laravel-native solution, explore `spatie/laravel-guzzle-promise` or build a custom facade wrapping this middleware.