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