php
45 lines · 8 steps
Caching HTTP responses with Laravel middleware
A middleware that serves cached responses for safe GET requests and stores fresh ones on the way out.
Explained by
highlit
1<?php
2
3namespace App\Http\Middleware;
4
5use Closure;
6use Illuminate\Http\Request;
7use Illuminate\Support\Facades\Cache;
8use Symfony\Component\HttpFoundation\Response;
9
10class CacheResponse
11{
12 public function handle(Request $request, Closure $next, int $ttl = 300): Response
13 {
14 if (! $request->isMethod('GET') || $request->hasHeader('Authorization')) {
15 return $next($request);
16 }
17
18 $key = $this->cacheKey($request);
19
20 if ($cached = Cache::get($key)) {
21 return response($cached['content'], $cached['status'])
22 ->withHeaders($cached['headers'])
23 ->header('X-Cache', 'HIT');
24 }
25
26 $response = $next($request);
27
28 if ($response->isSuccessful()) {
29 Cache::put($key, [
30 'content' => $response->getContent(),
31 'status' => $response->getStatusCode(),
32 'headers' => [
33 'Content-Type' => $response->headers->get('Content-Type'),
34 ],
35 ], now()->addSeconds($ttl));
36 }
37
38 return $response->header('X-Cache', 'MISS');
39 }
40
41 protected function cacheKey(Request $request): string
42 {
43 return 'response:'.sha1($request->fullUrl());
44 }
45}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Only cache idempotent, unauthenticated requests to avoid serving one user's data to another.
- 2An X-Cache header makes it easy to verify hits versus misses in production.
- 3Storing status, content, and headers together lets you faithfully reconstruct a response from cache.
Related explainers
php
<?php class NameParser {
Parsing a full name into components in PHP
string-parsing
arrays
normalization
Intermediate
8 steps
php
<?php namespace App\Services\Checkout;
Validating coupons with Laravel's Pipeline
pipeline
chain of responsibility
transactions
Intermediate
7 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
php
<?php namespace App\Services;
How a password strength validator works in PHP
validation
regular-expressions
data-driven
Intermediate
8 steps
javascript
import { useState, useEffect, useCallback, useRef } from 'react'; const cache = new Map(); const inflight = new Map();
Building a stale-while-revalidate hook in React
caching
request-deduplication
custom-hooks
Advanced
10 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/caching-http-responses-with-laravel-middleware-explained-php-0075/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.