Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Responsecache

Laravel Responsecache Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Speed up a Laravel app by caching the entire response

Frequently asked questions about Laravel Responsecache
How do I cache specific routes in Laravel using spatie/laravel-responsecache?
Use the `CacheResponse` middleware in route groups or individual routes. For example, `Route::middleware(CacheResponse::for(minutes(10)))->group(...)` caches responses for 10 minutes. You can also apply it to single routes like `Route::get('/posts', ...)->middleware(CacheResponse::for(hours(1)))`.
Does spatie/laravel-responsecache work with Laravel 11 or older versions?
The package officially supports Laravel 12+ and PHP 8.4+. For older versions, check the GitHub releases for compatibility or consider forking the package. Laravel 11 may work with minor adjustments, but no official support is guaranteed.
Can I cache JSON API responses with this package?
Yes, the package automatically caches successful text-based responses, including JSON. Ensure your API endpoints return JSON with a `200 OK` status. For dynamic data (e.g., user-specific responses), exclude routes using `unless()` or handle it via middleware logic.
How do I handle cache invalidation when data changes in my Laravel app?
Use cache tags with `CacheResponse::for()->withTags(['posts', 'user:123'])` to group related responses. When data changes, manually clear tags using `Cache::tags(['posts'])->flush()`. For complex cases, listen to `CacheMissedEvent` or use `FlexibleCacheResponse` for background recaching.
What’s the difference between `CacheResponse` and `FlexibleCacheResponse`?
`CacheResponse` serves stale responses only after the cache expires. `FlexibleCacheResponse` uses a grace period: after expiration, it serves stale responses instantly while refreshing in the background. This reduces latency for users during cache rebuilds. Use `FlexibleCacheResponse::for(minutes(5))->withGracePeriod(minutes(2))` for this behavior.
Will this package work with Redis or other cache backends?
Yes, it supports all Laravel cache drivers (file, Redis, DynamoDB, etc.). Redis is recommended for distributed systems due to its low latency and pub/sub capabilities for invalidation. Configure your cache driver in `config/cache.php` as usual.
How do I exclude certain routes from caching in tests?
Use `CacheResponse::for()->unless(fn ($request) => app()->environment('testing'))` to disable caching in tests. Alternatively, wrap test routes in `Route::middleware(['web'])->group(...)` and exclude the middleware globally in `app/Http/Kernel.php` during tests.
Can I monitor cache hit/miss ratios or storage usage?
Yes, log cache events by extending the package or using Laravel’s logging. For metrics, integrate with tools like Prometheus or track `CacheMissedEvent` occurrences. Monitor storage usage with `Cache::storeSize()` or run `php artisan cache:clear` manually.
What are the risks of caching user-specific responses (e.g., dashboards)?
Caching user-specific data can lead to stale content if not handled carefully. Exclude dynamic routes (e.g., authenticated user data) or use conditional caching with `unless(fn ($request) => $request->user())`. For dashboards, consider `FlexibleCacheResponse` with short grace periods.
Are there alternatives to spatie/laravel-responsecache for Laravel response caching?
Alternatives include `laravel-cache-manager` (for full-page caching) or `stripe/laravel-caching` (for partial caching). However, `spatie/laravel-responsecache` is optimized for full-response caching with Laravel’s middleware stack, offering granular control via tags and flexible caching. Compare based on your need for simplicity vs. advanced features.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codraw/framework-extra-bundle
codraw/messenger
codraw/security
codraw/mailer
codraw/contracts
codraw/profiling
codraw/dependency-injection
codraw/tester
codraw/core
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony