- 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.