Laravel Response Cache
Speed up a Laravel application by caching the entire HTTP response
Repository Health
Technical Analysis
Laravel Response Cache (spatie/laravel-responsecache) is a Spatie package that speeds up a Laravel application by caching the entire response. By default it caches all successful GET requests returning text-based content such as HTML and JSON, so repeat requests are served straight from the cache without running through the full application stack.
Caching is applied through middleware and cache profiles, with fine-grained control via attributes, request builders, and tag-aware invalidation. It also supports flexible stale-while-revalidate caching, where an expired response is served instantly while the cache refreshes in the background, and ships with a console command and events for clearing and reacting to cached responses.
What You Get
- Middleware (CacheResponse and FlexibleCacheResponse) to cache routes or route groups
- Configurable cache profiles controlling what gets cached and for how long
- Attributes (Cache, FlexibleCache, NoCache) for per-controller cache control
- Flexible stale-while-revalidate caching with lifetime and grace periods
- An artisan clear command, cache tagging, and events for invalidation hooks
Common Use Cases
- Serving high-traffic marketing or content pages instantly from cache
- Caching read-heavy JSON API endpoints to reduce database load
- Applying stale-while-revalidate to dashboards where brief staleness is acceptable
Under The Hood
Architecture - The package registers via ResponseCacheServiceProvider and exposes a central ResponseCache service backed by ResponseCacheRepository. Caching decisions run through CacheProfiles (e.g. CacheAllSuccessfulGetRequests implementing CacheProfile), applied by Middlewares such as CacheResponse and flexible variants. Supporting namespaces include Hasher for building cache keys, Serializers for storing responses, Replacers for dynamic placeholders, CacheItemSelector for targeted invalidation, and Attributes for declarative per-route control.
Tech Stack - Pure PHP targeting PHP 8.4, built on Laravel 12/13 components (illuminate/cache, console, container, http, support), Carbon for time handling, and spatie/laravel-package-tools plus spatie/php-attribute-reader for package scaffolding and attribute parsing.
Code Quality - The tests/ suite is extensive and uses Pest with Orchestra Testbench, covering profiles, serialization, hashing, tagging, flexible caching, and the clear command. Code style is enforced with Laravel Pint and static analysis with Larastan, reflecting Spatie’s consistent open-source standards.
API Design - The developer experience is minimal-boilerplate and idiomatic: attaching CacheResponse::for(minutes(10)) to a route group or annotating controllers with #[Cache] attributes is enough to start caching. The README documents profiles, tagging, flexible caching, and invalidation clearly, so common setups require almost no configuration.