Describes how to use the Vary header in Django cache Processing

Source: Internet
Author: User
This article describes how to use the Vary header in Django cache processing. Django is the most popular Pythonweb development framework, you can refer to the Vary header to define which request header should be taken into account when the cache mechanism constructs its cache key value. For example, if the content of a Web page depends on your language preferences, the page is called a different language.

By default, Django's cache system uses the requested path (for example, "/stories/2005/jun/23/bank_robbed/") to create a cache key. This means that each request uses the same Cache version, regardless of the client cookie and language configuration. Unless you use the Vary header notification cache mechanism, the page output depends on the cookie and language settings in the request header.

To do this in Django, you can use the convenient vary_on_headers view decorator, as shown below:

from django.views.decorators.vary import vary_on_headers# Python 2.3 syntax.def my_view(request):  # ...my_view = vary_on_headers(my_view, 'User-Agent')# Python 2.4+ decorator syntax.@vary_on_headers('User-Agent')def my_view(request):  # ...

In this case, the cache mechanism (such as Django's own cache middleware) will cache an independent page version for each individual user browser.

The advantage of using the vary_on_headers modifier instead of manually setting the Vary header (using code such as response ['vary '] = 'user-agent') is that the modifier (which may already exist) vary, instead of setting from scratch, and may overwrite the existing settings.

You can pass multiple head labels to vary_on_headers:

@vary_on_headers('User-Agent', 'Cookie')def my_view(request):  # ...

The code in this section notifies the upstream cache to perform different operations on the two. That is to say, each combination of user-agent and cookie should obtain its own cache value. For example, a request that uses Mozilla as the user-agent and foo = bar as the cookie value should be considered different from a request that uses Mozilla as the user-agent and foo = ham.

The vary_on_cookie decorator is available because it is common to treat each cookie separately. The following two views are equivalent:

@vary_on_cookiedef my_view(request):  # ...@vary_on_headers('Cookie')def my_view(request):  # ...

The input vary_on_headers header is case-insensitive. "User-Agent" is identical to "user-agent.

You can also use the help function: django. utils. cache. patch_vary_headers. This function sets or adds a Vary header, for example:

from django.utils.cache import patch_vary_headersdef my_view(request):  # ...  response = render_to_response('template_name', context)  patch_vary_headers(response, ['Cookie'])  return response

Patch_vary_headers takes an HttpResponse instance as the first parameter and uses a case-insensitive header name list or tuples as the second parameter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.