Weave 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

Laravel package that caches entire responses to speed up your app. By default it stores successful GET responses returning HTML or JSON for a week, serving cached content without hitting the application. Supports flexible stale-while-revalidate caching.

View on GitHub
Deep Wiki
Context7

spatie/laravel-responsecache caches entire HTTP responses to dramatically speed up Laravel apps. By default it stores all successful GET requests returning text-based content (HTML, JSON) for a week, then serves cached responses without booting the full application.

Route::middleware(CacheResponse::for(minutes(10)))->group(...);
  • Route middleware with configurable cache lifetimes
  • Flexible caching: stale responses served instantly while refreshing in the background
  • Cache profiles for customizing serialization and storage
  • Invalidation via tags, URLs, or custom logic
  • Works with any Laravel cache driver
Frequently asked questions about Laravel Responsecache
How do I install and configure spatie/laravel-responsecache in Laravel?
Install via Composer with composer require spatie/laravel-responsecache. Register the middleware on routes or groups using CacheResponse::for(...) or the #[Cache] attribute. The package uses Laravel's cache stores, so no extra configuration is required beyond choosing a cache driver.
How does spatie/laravel-responsecache cache entire responses in Laravel?
It caches successful GET responses that return text-based content such as HTML and JSON. The middleware saves the response on the first request and returns the cached copy on subsequent identical requests without hitting your application. Cache keys are derived from request attributes like URI and headers, and writes are deferred to the terminate phase for performance.
What Laravel and PHP versions does spatie/laravel-responsecache support?
The current version requires Laravel 12+ and PHP 8.4+. Older versions have deprecation paths, so check the package's composer constraints if you are on an older Laravel release. It is designed to integrate with Laravel's HTTP stack without monkeypatching.
How do I clear or invalidate cached responses in spatie/laravel-responsecache?
You can clear cached responses using the package's Artisan command (e.g., php artisan responsecache:clear) or by clearing the underlying cache store. You can also use cache tags with CacheResponse::for()->withTags() to invalidate specific groups. For flexible caching, stale content refreshes in the background after the lifetime expires.
How can I use cache tags with spatie/laravel-responsecache?
Add tags to cached responses via CacheResponse::for(...)->withTags(['posts', 'posts:'.$id]). Tags let you selectively invalidate related responses when data changes. You need a cache store that supports tags, such as Redis or Memcached, not the file driver. Be disciplined with tagging to avoid stale data or excessive cache misses.
What is flexible caching in spatie/laravel-responsecache and when should I use it?
Flexible caching serves a stale cached response instantly after the lifetime expires while refreshing the cache in the background. Use it for pages where brief staleness is acceptable, like dashboards or public listings. After a grace period, the cache is considered expired and the next request recalculates fully.
How do I exclude routes or user-specific content from response caching in Laravel?
Apply the CacheResponse middleware only to routes or groups you want cached, rather than globally. You can also use CacheResponse::for(...)->unless(...) to exclude specific conditions or routes. For dynamic content like CSRF tokens or user-specific data, you may need additional middleware tweaks like CsrfTokenReplacer.
How do I test Laravel applications that use spatie/laravel-responsecache?
Cached responses can bypass application logic, so disable response caching in tests using CacheResponse::disable() or exclude test routes. You can also mock the middleware or use Laravel's HttpTests trait. Ensure cache stores are cleared between tests to avoid cross-test contamination.
What cache store should I use with spatie/laravel-responsecache in production?
The package works with any Laravel cache driver, including file, Redis, database, APCu, and DynamoDB. Redis is best for distributed systems due to low latency and pub/sub support for invalidation. File-based caching is simpler but may not scale well for multi-server deployments. Consider your invalidation strategy and monitoring needs.
What are alternatives to spatie/laravel-responsecache and is it actively maintained?
Alternatives include Laravel's built-in response caching, HTTP cache packages, or custom middleware. spatie/laravel-responsecache is actively maintained by Spatie and supports current Laravel versions. Check Packagist and GitHub for recent releases and test coverage before adopting.
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.
dbravoan/dbcms-admin-theme-dbcms
dbravoan/dbcms-theme-db-cyber
dbravoan/dbcms-blocks-basic
dbravoan/dbcms-module-content-audit
dbravoan/dbcms-module-ai-assistant
paygate-to/paygate-php
padosoft/askmydocs-connector-base
defstudio/supercharged-carbon
bernskioldmedia/laravel-matomo
bernskioldmedia/laravel-harvest
bernskiold/laravel-data-scrubber
bernskiold/laravel-currency-converter
bernskiold/laravel-campaign-monitor
bernskioldmedia/laravel-autotranslate
siberfx/telegram-bot
roy-sandip/laravel-view-extended
gregsphotography/site-builder-core
roy-sandip/laravel-view
anodyne/blade-fluentui-icons
webx-ui/module-catalog-collections