Onkar Lapate’s Post

Django has three caching levels. Most engineers use only one. Level 1 : Per-site caching - UpdateCacheMiddleware and FetchFromCacheMiddleware wrap the entire request stack. - FetchFromCacheMiddleware checks the cache before the request reaches the view. Cache hit and the response is returned immediately. View never runs. - UpdateCacheMiddleware caches the full response on the way out. - This is the most aggressive caching level. Entire pages cached as raw HTTP responses. - Only caches GET and HEAD requests. Does not cache responses with cookies or session data by default. Level 2 : Per-view caching - @cache_page decorator wraps individual views. - caches the entire response, not just the data. Scoped to a specific views only. - The trap is @cache_page uses the URL as the cache key by default. - Two users hitting the same URL get the same cached response. A view that returns user-specific data will serves one user's data to another. Level 3 : Low-level caching - cache.get() and cache.set() directly. No middleware. No decorators. - Full control of caching exactly what's needed, for exactly as long as needed. Running all three simultaneously means the same data can exist in cache at multiple levels. Invalidating low-level cache does nothing to a cached response at the per-view level. Caching at the wrong level doesn't just miss, it serves wrong data confidently. Have you ever had a caching level serve stale or wrong data silently? #Python #Django #BackendDevelopment #SoftwareEngineering

  • No alternative text description for this image

To view or add a comment, sign in

Explore content categories