Stale cache responses served after timeout with CacheMiddleware
0 reputation · 16 Nov 2025, 10:54 UTC
The goal is to guarantee that responses cached by Django’s CacheMiddleware or the @cache_page decorator are not served stale after their configured timeout, even when the underlying cache backend employs lazy expiration (e.g., Redis without active eviction).
Because Django delegates expiration handling to the backend, a key may remain stored past its timeout and be returned on the next request until the backend lazily removes it. Moreover, Django does not automatically invalidate cached pages when model data changes, leaving developers to manage versioning or manual flushing. This raises uncertainty about whether the framework should provide built‑in signal‑based invalidation or additional freshness checks.
Should Django’s caching layer automatically hook into model‑signals to purge related cache keys? Is relying solely on timeout‑based expiration acceptable when using backends with lazy eviction? What mechanism could be added to CacheMiddleware to validate a cached response’s freshness before it is served?