HTTP
Caching
Cache-Control, ETag, and conditional requests.
By EZ4Code Team
cacheetagconditional
Code
# Response caching headers
HTTP/1.1 200 OK
Cache-Control: public, max-age=3600
ETag: "abc123"
Last-Modified: Wed, 01 Jan 2024 00:00:00 GMT
Vary: Accept-Encoding
---
# Conditional request (revalidation)
GET /api/data HTTP/1.1
Host: example.com
If-None-Match: "abc123"
If-Modified-Since: Wed, 01 Jan 2024 00:00:00 GMT
---
# Response when unchanged
HTTP/1.1 304 Not Modified
ETag: "abc123"Explanation
Cache-Control directs how long and where a response may be cached, with max-age in seconds and public/private scoping. ETag and Last-Modified enable conditional requests: the client sends them back as If-None-Match or If-Modified-Since, and the server replies 304 when the cached copy is still valid. The Vary header tells caches to key on additional request headers.