- How do I cache responses globally for all routes in Laravel using spatie/laravel-responsecache?
- Add the `CacheResponse` middleware to your `app/Http/Kernel.php` under the `$middlewareGroups['web']` or `$middlewareGroups['api']` array. This will cache all successful GET requests returning text-based content (HTML/JSON) by default for 1 week. Adjust the TTL by passing a custom duration to the middleware constructor.
- Can I use this package to cache API responses (JSON) in Laravel?
- Yes, the package caches all text-based responses, including JSON APIs. Simply apply the `CacheResponse` middleware to your API routes. For APIs with dynamic data, consider using flexible caching with a short grace period to avoid stale responses.
- What Laravel versions does spatie/laravel-responsecache support?
- The package supports Laravel 10, 11, and 12. It requires PHP 8.1+ and is regularly updated to ensure compatibility with the latest Laravel releases. Check the [GitHub repo](https://github.com/spatie/laravel-responsecache) for version-specific details.
- How do I exclude specific routes from caching in Laravel?
- Use the `DoNotCacheResponse` middleware to exclude routes. For example, add it to your `Kernel.php` or apply it directly to sensitive routes like `/admin` or `/auth`. This prevents caching for non-GET requests or routes requiring fresh data.
- What’s the difference between rigid caching and flexible caching in this package?
- Rigid caching serves a stale response only after the TTL expires, causing a delay until the cache refreshes. Flexible caching (stale-while-revalidate) serves the stale response immediately while refreshing it in the background, reducing latency. Use flexible caching for dashboards or less critical data.
- How do I invalidate cached responses when data changes (e.g., after a database update)?
- Use Laravel’s cache tags to invalidate specific responses. For example, tag cached responses with `posts` and clear them via `Cache::tags('posts')->flush()`. Alternatively, trigger cache invalidation in model observers or events (e.g., `Post::updated()`).
- Does spatie/laravel-responsecache work with Redis or only file-based caching?
- The package supports all Laravel cache drivers, including Redis, Memcached, and file caching. Redis is recommended for production due to its speed, tag support, and distributed invalidation capabilities. Configure the cache driver in your `.env` file under `CACHE_DRIVER`.
- How can I monitor cache hit/miss rates and performance impact in Laravel?
- Leverage Laravel events like `CacheResponse::cached` and `CacheResponse::missed` to log hit/miss rates. Use tools like Laravel Debugbar or custom metrics to track performance. For advanced monitoring, integrate with APM tools or write a custom middleware to log cache statistics.
- What’s the best cache TTL (time-to-live) for high-traffic Laravel APIs?
- Start with a conservative TTL (e.g., 5–15 minutes for APIs) and adjust based on data volatility. For read-heavy APIs with infrequent updates, extend to hours or days. Use flexible caching for critical data to balance freshness and performance. Profile cache hit rates to optimize TTLs.
- Are there alternatives to spatie/laravel-responsecache for caching Laravel responses?
- Yes, alternatives include Laravel’s built-in `Cache::remember()` for controller-level caching, or packages like `laravel-cache-manager` for advanced strategies. However, `spatie/laravel-responsecache` uniquely caches full HTTP responses (HTML/JSON) at the middleware level, reducing server load for repeated requests without manual caching logic.