https://github.com/spatie/laravel-responsecache
Speed up a Laravel app by caching the entire response
https://github.com/spatie/laravel-responsecache
cache laravel performance php varnish
Last synced: 4 months ago
JSON representation
Speed up a Laravel app by caching the entire response
- Host: GitHub
- URL: https://github.com/spatie/laravel-responsecache
- Owner: spatie
- License: mit
- Created: 2015-07-16T13:31:04.000Z (almost 11 years ago)
- Default Branch: main
- Last Pushed: 2025-05-12T21:56:47.000Z (about 1 year ago)
- Last Synced: 2025-05-14T22:02:52.730Z (about 1 year ago)
- Topics: cache, laravel, performance, php, varnish
- Language: PHP
- Homepage: https://freek.dev/1351-caching-the-entire-response-of-a-laravel-app
- Size: 818 KB
- Stars: 2,586
- Watchers: 39
- Forks: 230
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
- favorite-link - 通过缓存整个响应来加速 Laravel 应用程序。
- awesome-laravel - Laravel Responsecache - Speed up app by caching the entire response (Popular Packages)
- awesome-laravel - Laravel Responsecache - Acelere la aplicación almacenando en caché toda la respuesta (Paquetes utiles)
- fucking-awesome-laravel - Laravel Responsecache - Speed up app by caching the entire response (Popular Packages)
- awesome-laravel-framework - Laravel Responsecache - Speed up a Laravel app by caching the entire response (Popular Packages)
- laravel-awesome - Laravel Responsecache - Speed up a Laravel app by caching the entire response (Popular Packages)
README
Speed up an app by caching the entire response
[](https://packagist.org/packages/spatie/laravel-responsecache)
[](https://github.com/spatie/laravel-responsecache/actions?query=workflow%3Arun-tests+branch%3Amain)
[](https://github.com/spatie/laravel-responsecache/actions?query=workflow%3A"Fix+PHP+code+style+issues"+branch%3Amain)
[](https://packagist.org/packages/spatie/laravel-responsecache)
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:
```php
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:
```php
use Spatie\ResponseCache\Middlewares\FlexibleCacheResponse;
Route::get('/dashboard', [DashboardController::class, 'index'])
->middleware(FlexibleCacheResponse::for(lifetime: hours(1), grace: minutes(5)));
```
## Support us
[
](https://spatie.be/github-ad-click/laravel-responsecache)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Documentation
All documentation is available [on our documentation site](https://spatie.be/docs/laravel-responsecache).
## Testing
You can run the tests with:
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.