The Per-Site Cache

Once the cache is set up, the simplest way to use caching is to cache your entire site. You’ll need to

add                                                                     ‘django.middleware.cache.UpdateCacheMiddleware’ and ‘django.middleware.cache.FetchFromCacheMiddleware’ to your MIDDLEWARE_CLASSES setting, as in this example:

MIDDLEWARE_CLASSES = [
‘django.middleware.cache.UpdateCacheMiddleware’,
‘django.middleware.common.CommonMiddleware’,
‘django.middleware.cache.FetchFromCacheMiddleware’,
]

No, that’s not a typo: the update middleware must be first in the list, and the fetch middleware must be last. The details are a bit obscure, but see Order of MIDDLEWARE_CLASSES in the next chapter if you’d like the full story.

Then, add the following required settings to your Django settings file:

  • CACHE_MIDDLEWARE_ALIAS – The cache alias to use for storage.
  • CACHE_MIDDLEWARE_SECONDS – The number of seconds each page should be cached.
  • CACHE_MIDDLEWARE_KEY_PREFIX – If the cache is shared across multiple sites using the same Django installation, set this to the name of the site, or some other string that is unique to this Django instance, to prevent key collisions. Use an empty string if you don’t care.

FetchFromCacheMiddleware caches GET and HEAD responses with status 200, where the request and response headers allow. Responses to requests for the same URL with different query parameters are considered to be unique pages and are cached separately. This middleware expects that a HEAD request is answered with the same response headers as the corresponding GET request; in which case it can return a cached GET response for HEAD request. Additionally, UpdateCacheMiddleware automatically sets a few headers in each HttpResponse:

 Sets the Last-Modified header to the current date/time when a fresh (not cached) version of the page is requested.
 Sets the Expires header to the current date/time plus the defined CACHE_MIDDLEWARE_SECONDS.
 Sets the Cache-Control header to give a max age for the page – again, from the CACHE_MIDDLEWARE_SECONDS setting.

If a view sets its own cache expiry time (i.e. it has a max-age section in its Cache-Control header) then the page will be cached until the expiry time, rather than CACHE_MIDDLEWARE_SECONDS. Using the decorators in django.views.decorators.cache you can easily set a view’s expiry time (using the cache_control decorator) or disable caching for a view (using the never_cache decorator).

If USE_I18N is set to True then the generated cache key will include the name of the active language. This allows you to easily cache multilingual sites without having to create the cache key yourself.

Cache keys also include the active language when USE_L10N is set to True and the current time zone when USE_TZ is set to True.

Back to Tutorial

Setting Up the Cache
The Per-View Cache

Get industry recognized certification – Contact us

keyboard_arrow_up
Open chat
Need help?
Hello 👋
Can we help you?