- How do I cache specific routes in Laravel using spatie/laravel-responsecache?
- Use the `CacheResponse` middleware in route groups or individual routes. For example, `Route::middleware(CacheResponse::for(minutes(10)))->group(...)` caches responses for 10 minutes. You can also apply it to single routes like `Route::get('/posts', ...)->middleware(CacheResponse::for(hours(1)))`.
- Does spatie/laravel-responsecache work with Laravel 11 or older versions?
- The package officially supports Laravel 12+ and PHP 8.4+. For older versions, check the GitHub releases for compatibility or consider forking the package. Laravel 11 may work with minor adjustments, but no official support is guaranteed.
- Can I cache JSON API responses with this package?
- Yes, the package automatically caches successful text-based responses, including JSON. Ensure your API endpoints return JSON with a `200 OK` status. For dynamic data (e.g., user-specific responses), exclude routes using `unless()` or handle it via middleware logic.
- How do I handle cache invalidation when data changes in my Laravel app?
- Use cache tags with `CacheResponse::for()->withTags(['posts', 'user:123'])` to group related responses. When data changes, manually clear tags using `Cache::tags(['posts'])->flush()`. For complex cases, listen to `CacheMissedEvent` or use `FlexibleCacheResponse` for background recaching.
- What’s the difference between `CacheResponse` and `FlexibleCacheResponse`?
- `CacheResponse` serves stale responses only after the cache expires. `FlexibleCacheResponse` uses a grace period: after expiration, it serves stale responses instantly while refreshing in the background. This reduces latency for users during cache rebuilds. Use `FlexibleCacheResponse::for(minutes(5))->withGracePeriod(minutes(2))` for this behavior.
- Will this package work with Redis or other cache backends?
- Yes, it supports all Laravel cache drivers (file, Redis, DynamoDB, etc.). Redis is recommended for distributed systems due to its low latency and pub/sub capabilities for invalidation. Configure your cache driver in `config/cache.php` as usual.
- How do I exclude certain routes from caching in tests?
- Use `CacheResponse::for()->unless(fn ($request) => app()->environment('testing'))` to disable caching in tests. Alternatively, wrap test routes in `Route::middleware(['web'])->group(...)` and exclude the middleware globally in `app/Http/Kernel.php` during tests.
- Can I monitor cache hit/miss ratios or storage usage?
- Yes, log cache events by extending the package or using Laravel’s logging. For metrics, integrate with tools like Prometheus or track `CacheMissedEvent` occurrences. Monitor storage usage with `Cache::storeSize()` or run `php artisan cache:clear` manually.
- What are the risks of caching user-specific responses (e.g., dashboards)?
- Caching user-specific data can lead to stale content if not handled carefully. Exclude dynamic routes (e.g., authenticated user data) or use conditional caching with `unless(fn ($request) => $request->user())`. For dashboards, consider `FlexibleCacheResponse` with short grace periods.
- Are there alternatives to spatie/laravel-responsecache for Laravel response caching?
- Alternatives include `laravel-cache-manager` (for full-page caching) or `stripe/laravel-caching` (for partial caching). However, `spatie/laravel-responsecache` is optimized for full-response caching with Laravel’s middleware stack, offering granular control via tags and flexible caching. Compare based on your need for simplicity vs. advanced features.