Caching
16 min
HTTP caching avoids re-fetching data that hasn't changed — the
Cache-Control header (on a response) tells clients and intermediate
caches (like a CDN) how long they're allowed to reuse a cached copy
before checking back with the server:
def parse_cache_control(header_value):
result = {}
for part in header_value.split(", "):
if "=" in part:
k, v = part.split("=")
result[k] = v
else:
result[part] = True
return result
print(parse_cache_control("max-age=3600, no-cache"))
# {'max-age': '3600', 'no-cache': True}
'no-store' (never cache) is stronger than 'no-cache' (can cache, but must revalidate before reusing) -- a common source of confusion since the names sound almost identical but mean genuinely different things.
A cached response is fresh as long as its age (time since it was
fetched) hasn't exceeded max-age — fresh responses can be reused
immediately, with zero network round-trip:
def is_cache_fresh(age_seconds, max_age):
return age_seconds < max_age
print(is_cache_fresh(100, 3600)) # True -- cached 100s ago, still good for another ~3500s
print(is_cache_fresh(4000, 3600)) # False -- stale, needs to be re-fetched
Once stale, a well-behaved client doesn't necessarily re-download the
whole response — it can send a conditional request (using an ETag
or Last-Modified header) asking "has this changed since I last saw
it?" If not, the server responds 304 Not Modified with no body at all,
saving bandwidth even on a cache miss.
Write `parse_cache_control(header_value)`: parse a Cache-Control header value like `'max-age=3600, no-cache'` into a dict — directives with a value map key to value; directives without one (like `no-cache`) map to `True`.
Write `is_cache_fresh(age_seconds, max_age)`, returning True if `age_seconds` is still less than `max_age`.
What does the Cache-Control: max-age=3600 header mean?
You can parse Cache-Control directives and determine whether a cached response is still fresh enough to reuse.