0
0
Djangoframework~8 mins

Per-view caching in Django - Performance & Optimization

Choose your learning style9 modes available
Performance: Per-view caching
HIGH IMPACT
Per-view caching improves page load speed by storing the full response of a view, reducing server processing time on repeated requests.
Speeding up repeated page loads by caching full view responses
Django
@cache_page(60 * 15)
def my_view(request):
    data = expensive_database_query()
    return render(request, 'template.html', {'data': data})
Caches the entire response for 15 minutes, so repeated requests skip query and rendering.
📈 Performance GainReduces server processing to near zero for cached requests, improving LCP significantly
Speeding up repeated page loads by caching full view responses
Django
def my_view(request):
    data = expensive_database_query()
    return render(request, 'template.html', {'data': data})
Every request runs the expensive query and template rendering, causing slow response times.
📉 Performance CostBlocks rendering for 200-500ms per request depending on query complexity
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
No cachingN/A (server-side)N/AHigh (slow response delays paint)[X] Bad
Per-view cachingN/A (server-side)N/ALow (fast response enables quick paint)[OK] Good
Rendering Pipeline
Per-view caching bypasses the server-side processing stage by serving stored HTML directly, reducing time before the browser receives content.
Server Processing
Network Transfer
Browser Rendering
⚠️ BottleneckServer Processing (database queries and template rendering)
Core Web Vital Affected
LCP
Per-view caching improves page load speed by storing the full response of a view, reducing server processing time on repeated requests.
Optimization Tips
1Cache full view responses to reduce server processing time.
2Set appropriate cache expiration to balance freshness and speed.
3Use per-view caching to improve Largest Contentful Paint (LCP).
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using per-view caching in Django?
AIt decreases the size of HTML sent to the browser.
BIt reduces server processing time by serving cached full responses.
CIt improves CSS rendering speed on the client.
DIt automatically optimizes database queries.
DevTools: Network
How to check: Open DevTools, go to Network tab, reload the page twice. Check if the second request has a faster response time and possibly a 304 status with cache headers.
What to look for: Look for reduced response time and presence of cache headers indicating cached content delivery.