Use Content Caching with Vary Headers
A snippet showing how to set Vary headers for caching.
# views.py
from django.views.decorators.vary import vary_on_headers
from django.http import HttpResponse
@vary_on_headers("User-Agent", "Accept-Language")
def news_feed(request):
return HttpResponse("Vary by UA and language")
Explanation:
-
Consider
@vary_on_cookiefor authenticated vs anonymous content. - Downstream caches key the response by specified request headers, avoiding cache poisoning and mismatches.
- Category Performance & Optimization
- Total Views 184
- Last Modified 07 May, 2026
- Tags #performance #caching #headers #optimization
Previous snippet
Enable GZip Middleware
Next snippet