Speed up a Laravel application by caching the entire response
spatie/laravel-responsecache is a Laravel package for speed up a laravel application by caching the entire response.
It currently has 2.802 GitHub stars and 8.926.402 downloads on Packagist (latest version 8.4.1).
Install it with composer require spatie/laravel-responsecache.
Discover more Laravel packages by spatie
or browse all Laravel packages to compare alternatives.
Last updated
This Laravel package can cache an entire response. By default it will cache all successful GET requests that return text based content (such as HTML and JSON) for a week. This could potentially speed up the response quite considerably.
The first time a request comes in, the package will save the response before sending it to the user. When the same request comes in again, the cached response is returned without going through the entire application.
Here's a quick example:
use Spatie\ResponseCache\Middlewares\CacheResponse;
Route::middleware(CacheResponse::for(minutes(10)))->group(function () {
Route::get('/posts', [PostController::class, 'index']);
Route::get('/posts/{post}', [PostController::class, 'show']);
});
For pages where brief staleness is acceptable, you can use flexible caching. After the lifetime expires, the stale response is still served instantly while the cache refreshes in the background. Once the grace period is over, the cache is considered expired and the next request will be fully recalculated:
use Spatie\ResponseCache\Middlewares\FlexibleCacheResponse;
Route::get('/dashboard', [DashboardController::class, 'index'])
->middleware(FlexibleCacheResponse::for(lifetime: hours(1), grace: minutes(5)));
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
All documentation is available on our documentation site.
You can run the tests with:
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.