You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If you use Django's authorisation and session support (which everyone does), then Cookie will be added to your Vary header. If you have pages that don't change based on who is logged in, then you will get subpar cache performance.
Django include a vary_on_headers function which adds to the Vary header. This library is the compliment, it reduces the Vary header.
The Vary header
The Vary HTTP response header is used by caching systems to know what HTTP request headers to cache this page based on. A typical Vary header might look like this: Vary: Accept-Language, Cookie, which means that requests to that URL with the same Accept-Language and Cookie field can use the cached value. In this example, we say that "The page varies on 'Cookie' and 'Accept-Language'. It does not vary on 'User-Agent'".
Installation
pip install django-dont-vary-on
To use it you must put RemoveUnneededVaryHeadersMiddleware in your MIDDLEWARE_CLASSES and it must be below UpdateCacheMiddleware and above other middleware classes. Ideally put it straight under UpdateCacheMiddleware.
This is a more nuclear option, and the response will only vary the headers you mention. Any other vary header values will not be included, even if other middleware classes might add them.
from django_dont_vary_on.decorators import only_vary_on
@only_vary_on("Cookie")
def myview(request):
…
This view will have a Vary: Cookie header, nothing else in there. It will cache things based only on the 'Cookie' request header, no other request header.
If you have a page that is the same for all logged in users and anonymous users, then you can use @dont_vary_on('Cookie') to cache that page for all users, and only generate it once.
If you have a page that generates JSON data, and it doesn't matter what language the user has, you can use @dont_vary_on('Accept-Language') to cache that page regardless of the language.
Caveats
Sending an incorrect cached page can be very embarassing. People might be able to see other people's logged in and personal details. Ensure you know what you're doing.
About
Library for Django to give you more control over Django's caching, and improving you cache hits and performance