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 Package

spatie/laravel-responsecache

Caches full Laravel responses to speed up apps. Automatically stores successful GET HTML/JSON for a configurable time and serves cached output on repeat requests. Supports per-route middleware, custom rules, and optional stale-while-revalidate style caching.

View on GitHub
Deep Wiki
Context7

Getting Started

Install via Composer (composer require spatie/laravel-responsecache), publish the config (php artisan vendor:publish --provider="Spatie\ResponseCache\ResponseCacheServiceProvider"), and start with the default setup: all successful GET requests (HTML/JSON) are cached for 7 days. For immediate wins, wrap public, read-heavy routes with CacheResponse middleware — e.g., Route::get('/blog', [BlogController::class, 'index'])->middleware(CacheResponse::for(days(1)));. First-time visitors still hit the app, but all subsequent identical GET requests bypass middleware, controllers, and DB (if data is cached), delivering near-instant responses.

Implementation Patterns

  • Per-route lifetime control: Use CacheResponse::for($lifetime, tags: [...]) middleware or #[Cache(lifetime: ..., tags: [...])] attributes on controllers for granular TTLs (e.g., static pages = hours, dashboards = minutes).
  • Flexible caching for balance: Replace CacheResponse with FlexibleCacheResponse for pages needing fresh data without sacrificing speed: FlexibleCacheResponse::for(lifetime: minutes(30), grace: minutes(10)) serves stale content instantly during the grace period while refreshing in background.
  • Tagged invalidation: Tag cache entries (tags: ['posts']) and invalidate in model events (self::updated(fn () => ResponseCache::clear(['posts']))) to target only related pages, not full cache clear.
  • Hybrid approach: Combine global config defaults (for fallbacks) with route-level overrides (for precision). Use artisan commands like php artisan responsecache:clear --url=/products/123 during debugging or deploys for atomic invalidation.

Gotchas and Tips

  • Logged-in users get separate caches by default — ideal for user-specific pages, but be mindful of cache fragmentation; consider disabling per-user caching ('cache_for_authenticated_users' => false) for public pages to improve hit rates.
  • Non-idempotent requests bypass cache: Only GET requests are cached by default; POST/PUT/DELETE are ignored — ensure your front-end avoids accidental cache misses by using GET for read-only endpoints.
  • Cache hits ignore middleware: Any custom middleware after CacheResponse won’t run on cached requests — avoid side-effect logic in such middleware.
  • Debug caching: Use php artisan responsecache:show /path to inspect cache status, headers, TTL, and tags — invaluable for validating configuration.
  • Grace period gotcha: The background refresh uses Laravel’s defer — ensure your queue workers are running if your refresh logic triggers jobs, or it may silently fail.
  • Extension points: Customize cache keys via CacheKeyGenerator, modify response eligibility with ResponseCacheProfile, or extend TTL via CacheLifeTime — great for multi-tenant or A/B-testing scenarios.
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport