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 application by caching the entire response

Frequently asked questions about Laravel Responsecache
How do I cache responses globally for all routes in Laravel using spatie/laravel-responsecache?
Add the `CacheResponse` middleware to your `app/Http/Kernel.php` under the `$middlewareGroups['web']` or `$middlewareGroups['api']` array. This will cache all successful GET requests returning text-based content (HTML/JSON) by default for 1 week. Adjust the TTL by passing a custom duration to the middleware constructor.
Can I use this package to cache API responses (JSON) in Laravel?
Yes, the package caches all text-based responses, including JSON APIs. Simply apply the `CacheResponse` middleware to your API routes. For APIs with dynamic data, consider using flexible caching with a short grace period to avoid stale responses.
What Laravel versions does spatie/laravel-responsecache support?
The package supports Laravel 10, 11, and 12. It requires PHP 8.1+ and is regularly updated to ensure compatibility with the latest Laravel releases. Check the [GitHub repo](https://github.com/spatie/laravel-responsecache) for version-specific details.
How do I exclude specific routes from caching in Laravel?
Use the `DoNotCacheResponse` middleware to exclude routes. For example, add it to your `Kernel.php` or apply it directly to sensitive routes like `/admin` or `/auth`. This prevents caching for non-GET requests or routes requiring fresh data.
What’s the difference between rigid caching and flexible caching in this package?
Rigid caching serves a stale response only after the TTL expires, causing a delay until the cache refreshes. Flexible caching (stale-while-revalidate) serves the stale response immediately while refreshing it in the background, reducing latency. Use flexible caching for dashboards or less critical data.
How do I invalidate cached responses when data changes (e.g., after a database update)?
Use Laravel’s cache tags to invalidate specific responses. For example, tag cached responses with `posts` and clear them via `Cache::tags('posts')->flush()`. Alternatively, trigger cache invalidation in model observers or events (e.g., `Post::updated()`).
Does spatie/laravel-responsecache work with Redis or only file-based caching?
The package supports all Laravel cache drivers, including Redis, Memcached, and file caching. Redis is recommended for production due to its speed, tag support, and distributed invalidation capabilities. Configure the cache driver in your `.env` file under `CACHE_DRIVER`.
How can I monitor cache hit/miss rates and performance impact in Laravel?
Leverage Laravel events like `CacheResponse::cached` and `CacheResponse::missed` to log hit/miss rates. Use tools like Laravel Debugbar or custom metrics to track performance. For advanced monitoring, integrate with APM tools or write a custom middleware to log cache statistics.
What’s the best cache TTL (time-to-live) for high-traffic Laravel APIs?
Start with a conservative TTL (e.g., 5–15 minutes for APIs) and adjust based on data volatility. For read-heavy APIs with infrequent updates, extend to hours or days. Use flexible caching for critical data to balance freshness and performance. Profile cache hit rates to optimize TTLs.
Are there alternatives to spatie/laravel-responsecache for caching Laravel responses?
Yes, alternatives include Laravel’s built-in `Cache::remember()` for controller-level caching, or packages like `laravel-cache-manager` for advanced strategies. However, `spatie/laravel-responsecache` uniquely caches full HTTP responses (HTML/JSON) at the middleware level, reducing server load for repeated requests without manual caching logic.
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.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai