spatie/laravel-responsecache
Cache full Laravel responses to speed up your app. Automatically caches successful text-based GET requests (HTML/JSON), with easy middleware per route, configurable lifetimes, and optional stale-while-revalidate “grace” caching to refresh in the background.
Cache facade). It integrates seamlessly with Laravel’s routing, middleware groups, and response handling, reducing architectural friction.Cache contract, allowing alignment with existing infrastructure. Middleware can be chained or overridden for granular control.terminate() phase (post-response), minimizing blocking operations during request processing.CacheResponse::for()) or PHP attributes (#[Cache]), with zero changes to core Laravel logic. Example:
Route::middleware(CacheResponse::for(minutes(10)))->group(...);
Cache facade, Artisan commands (e.g., cache:clear), and debugging tools (e.g., dd() for cache inspection). Compatible with Laravel Forge/Vapor for deployment.CacheResponse::for()->withTags()) but requires discipline in tagging strategies.FlexibleCacheResponse for grace periods or implement event listeners (e.g., CacheMissedEvent) to trigger recache on data changes.X-Requested-With) may require middleware tweaks (e.g., CsrfTokenReplacer).spatie/laravel-responsecache:clear or Cache::storeSize().CacheResponse::disable() in tests is necessary.CacheResponse::for()->unless() to exclude test routes or leverage Laravel’s HttpTests trait.posts:123)? Over-tagging increases cache misses; under-tagging risks stale data.FlexibleCacheResponse, what grace periods balance staleness vs. recache latency? Monitor CacheMissedEvent frequency.laravel/framework, spatie/laravel-permission).GET semantics).#[NoCache] attributes./about, /blog) using CacheResponse::for(hours(1)).CacheMissedEvent or custom logging.web group for HTML, api for JSON).FlexibleCacheResponse for dashboards where staleness is acceptable.CacheResponse after auth/middleware that modifies the request (e.g., VerifyCsrfToken) but before middleware that alters the response (e.g., AppendsAuthLastActiveAt).$middleware = [
\App\Http\Middleware\VerifyCsrfToken::class,
\Spatie\ResponseCache\Middlewares\CacheResponse::for(minutes(10)),
\App\Http\Middleware\AppendAuthLastActiveAt::class,
];
spatie/php-attribute-reader) are optional for attribute-based caching.JsonResource).terminate() logic).Cache-Control: no-store.composer require spatie/laravel-responsecache
php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"
config/response-cache.php:
'default_lifetime_in_seconds' => 60 * 60, // 1 hour
.env:
CACHE_DRIVER=redis
Route::middleware(CacheResponse::for(minutes(10)))->group(function () {
Route::get('/posts', [PostController::class, 'index']);
});
#[Cache(lifetime: '10 minutes')]
public function index() { ... }
GET routes):
$middleware = [
\Spatie\ResponseCache\Middlewares\CacheResponse::for(minutes(10)),
];
php artisan cache:clear
php artisan response-cache:clear
ResponseCache::clear() or Artisan commands:
php artisan response-cache:clear
php artisan response-cache:clear --tags=posts
PostUpdated) to clear tags:
Post::updated(function ($post) {
ResponseCache::clearTags(['posts:'.$post->id]);
});
entity:action:id) to avoid tag sprawl.config/response-cache.php to avoid hardcoded values in routes/controllers.How can I help you explore Laravel packages today?