spatie/laravel-responsecache
Caches full Laravel responses to speed up apps. Automatically stores successful GET HTML/JSON for a configurable time and serves cached output on repeat requests. Supports per-route middleware, custom rules, and optional stale-while-revalidate style caching.
Install via Composer (composer require spatie/laravel-responsecache), publish the config (php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"), and start with the default setup: all successful GET requests (HTML/JSON) are cached for 7 days. For immediate wins, wrap public, read-heavy routes with CacheResponse middleware — e.g., Route::get('/blog', [BlogController::class, 'index'])->middleware(CacheResponse::for(days(1)));. First-time visitors still hit the app, but all subsequent identical GET requests bypass middleware, controllers, and DB (if data is cached), delivering near-instant responses.
CacheResponse::for($lifetime, tags: [...]) middleware or #[Cache(lifetime: ..., tags: [...])] attributes on controllers for granular TTLs (e.g., static pages = hours, dashboards = minutes).CacheResponse with FlexibleCacheResponse for pages needing fresh data without sacrificing speed: FlexibleCacheResponse::for(lifetime: minutes(30), grace: minutes(10)) serves stale content instantly during the grace period while refreshing in background.tags: ['posts']) and invalidate in model events (self::updated(fn () => ResponseCache::clear(['posts']))) to target only related pages, not full cache clear.php artisan responsecache:clear --url=/products/123 during debugging or deploys for atomic invalidation.'cache_for_authenticated_users' => false) for public pages to improve hit rates.CacheResponse won’t run on cached requests — avoid side-effect logic in such middleware.php artisan responsecache:show /path to inspect cache status, headers, TTL, and tags — invaluable for validating configuration.defer — ensure your queue workers are running if your refresh logic triggers jobs, or it may silently fail.CacheKeyGenerator, modify response eligibility with ResponseCacheProfile, or extend TTL via CacheLifeTime — great for multi-tenant or A/B-testing scenarios.How can I help you explore Laravel packages today?