- How do I install and configure spatie/laravel-responsecache in Laravel?
- Install via Composer with composer require spatie/laravel-responsecache. Register the middleware on routes or groups using CacheResponse::for(...) or the #[Cache] attribute. The package uses Laravel's cache stores, so no extra configuration is required beyond choosing a cache driver.
- How does spatie/laravel-responsecache cache entire responses in Laravel?
- It caches successful GET responses that return text-based content such as HTML and JSON. The middleware saves the response on the first request and returns the cached copy on subsequent identical requests without hitting your application. Cache keys are derived from request attributes like URI and headers, and writes are deferred to the terminate phase for performance.
- What Laravel and PHP versions does spatie/laravel-responsecache support?
- The current version requires Laravel 12+ and PHP 8.4+. Older versions have deprecation paths, so check the package's composer constraints if you are on an older Laravel release. It is designed to integrate with Laravel's HTTP stack without monkeypatching.
- How do I clear or invalidate cached responses in spatie/laravel-responsecache?
- You can clear cached responses using the package's Artisan command (e.g., php artisan responsecache:clear) or by clearing the underlying cache store. You can also use cache tags with CacheResponse::for()->withTags() to invalidate specific groups. For flexible caching, stale content refreshes in the background after the lifetime expires.
- How can I use cache tags with spatie/laravel-responsecache?
- Add tags to cached responses via CacheResponse::for(...)->withTags(['posts', 'posts:'.$id]). Tags let you selectively invalidate related responses when data changes. You need a cache store that supports tags, such as Redis or Memcached, not the file driver. Be disciplined with tagging to avoid stale data or excessive cache misses.
- What is flexible caching in spatie/laravel-responsecache and when should I use it?
- Flexible caching serves a stale cached response instantly after the lifetime expires while refreshing the cache in the background. Use it for pages where brief staleness is acceptable, like dashboards or public listings. After a grace period, the cache is considered expired and the next request recalculates fully.
- How do I exclude routes or user-specific content from response caching in Laravel?
- Apply the CacheResponse middleware only to routes or groups you want cached, rather than globally. You can also use CacheResponse::for(...)->unless(...) to exclude specific conditions or routes. For dynamic content like CSRF tokens or user-specific data, you may need additional middleware tweaks like CsrfTokenReplacer.
- How do I test Laravel applications that use spatie/laravel-responsecache?
- Cached responses can bypass application logic, so disable response caching in tests using CacheResponse::disable() or exclude test routes. You can also mock the middleware or use Laravel's HttpTests trait. Ensure cache stores are cleared between tests to avoid cross-test contamination.
- What cache store should I use with spatie/laravel-responsecache in production?
- The package works with any Laravel cache driver, including file, Redis, database, APCu, and DynamoDB. Redis is best for distributed systems due to low latency and pub/sub support for invalidation. File-based caching is simpler but may not scale well for multi-server deployments. Consider your invalidation strategy and monitoring needs.
- What are alternatives to spatie/laravel-responsecache and is it actively maintained?
- Alternatives include Laravel's built-in response caching, HTTP cache packages, or custom middleware. spatie/laravel-responsecache is actively maintained by Spatie and supports current Laravel versions. Check Packagist and GitHub for recent releases and test coverage before adopting.