2005-07-15 17:05:43 +00:00
|
|
|
========================
|
|
|
|
Django's cache framework
|
|
|
|
========================
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
So, you got slashdotted_. Now what?
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-07-19 20:49:07 +00:00
|
|
|
Django's cache framework gives you three methods of caching dynamic pages in
|
|
|
|
memory or in a database. You can cache the output of entire pages, you can
|
|
|
|
cache only the pieces that are difficult to produce, or you can cache your
|
|
|
|
entire site.
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
.. _slashdotted: http://en.wikipedia.org/wiki/Slashdot_effect
|
|
|
|
|
2005-07-15 17:05:43 +00:00
|
|
|
Setting up the cache
|
|
|
|
====================
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
The cache framework allows for different "backends" -- different methods of
|
|
|
|
caching data. There's a simple single-process memory cache (mostly useful as a
|
|
|
|
fallback) and a memcached_ backend (the fastest option, by far, if you've got
|
|
|
|
the RAM).
|
2005-07-15 17:05:43 +00:00
|
|
|
|
|
|
|
Before using the cache, you'll need to tell Django which cache backend you'd
|
2005-07-19 20:49:07 +00:00
|
|
|
like to use. Do this by setting the ``CACHE_BACKEND`` in your settings file.
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
The ``CACHE_BACKEND`` setting is a "fake" URI (really an unregistered scheme).
|
2005-07-21 19:05:10 +00:00
|
|
|
Examples:
|
2005-07-15 17:05:43 +00:00
|
|
|
|
|
|
|
============================== ===========================================
|
|
|
|
CACHE_BACKEND Explanation
|
|
|
|
============================== ===========================================
|
2005-07-19 20:49:07 +00:00
|
|
|
memcached://127.0.0.1:11211/ A memcached backend; the server is running
|
2005-10-29 17:05:19 +00:00
|
|
|
on localhost port 11211. You can use
|
|
|
|
multiple memcached servers by separating
|
|
|
|
them with semicolons.
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-09-27 04:25:08 +00:00
|
|
|
db://tablename/ A database backend in a table named
|
2005-09-25 22:03:30 +00:00
|
|
|
"tablename". This table should be created
|
|
|
|
with "django-admin createcachetable".
|
|
|
|
|
|
|
|
file:///var/tmp/django_cache/ A file-based cache stored in the directory
|
|
|
|
/var/tmp/django_cache/.
|
|
|
|
|
2005-07-15 17:05:43 +00:00
|
|
|
simple:/// A simple single-process memory cache; you
|
|
|
|
probably don't want to use this except for
|
2005-07-19 20:49:07 +00:00
|
|
|
testing. Note that this cache backend is
|
2005-10-09 00:55:08 +00:00
|
|
|
NOT thread-safe!
|
2005-09-27 04:25:08 +00:00
|
|
|
|
|
|
|
locmem:/// A more sophisticated local memory cache;
|
2005-09-25 22:03:30 +00:00
|
|
|
this is multi-process- and thread-safe.
|
2005-07-15 17:05:43 +00:00
|
|
|
============================== ===========================================
|
|
|
|
|
2005-07-19 20:49:07 +00:00
|
|
|
All caches may take arguments -- they're given in query-string style. Valid
|
2005-07-15 17:05:43 +00:00
|
|
|
arguments are:
|
|
|
|
|
2005-07-19 20:49:07 +00:00
|
|
|
timeout
|
|
|
|
Default timeout, in seconds, to use for the cache. Defaults to 5
|
|
|
|
minutes (300 seconds).
|
|
|
|
|
|
|
|
max_entries
|
2005-07-15 17:05:43 +00:00
|
|
|
For the simple and database backends, the maximum number of entries
|
|
|
|
allowed in the cache before it is cleaned. Defaults to 300.
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
cull_percentage
|
|
|
|
The percentage of entries that are culled when max_entries is reached.
|
2005-07-15 17:05:43 +00:00
|
|
|
The actual percentage is 1/cull_percentage, so set cull_percentage=3 to
|
|
|
|
cull 1/3 of the entries when max_entries is reached.
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-07-15 17:05:43 +00:00
|
|
|
A value of 0 for cull_percentage means that the entire cache will be
|
2005-07-19 20:49:07 +00:00
|
|
|
dumped when max_entries is reached. This makes culling *much* faster
|
2005-07-15 17:05:43 +00:00
|
|
|
at the expense of more cache misses.
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-07-15 17:05:43 +00:00
|
|
|
For example::
|
|
|
|
|
2005-09-11 15:12:30 +00:00
|
|
|
CACHE_BACKEND = "memcached://127.0.0.1:11211/?timeout=60"
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
Invalid arguments are silently ignored, as are invalid values of known
|
2005-07-15 17:05:43 +00:00
|
|
|
arguments.
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
.. _memcached: http://www.danga.com/memcached/
|
|
|
|
|
2005-07-19 20:49:07 +00:00
|
|
|
The per-site cache
|
2005-07-15 17:05:43 +00:00
|
|
|
==================
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
Once the cache is set up, the simplest way to use the cache is to cache your
|
|
|
|
entire site. Just add ``django.middleware.cache.CacheMiddleware`` to your
|
|
|
|
``MIDDLEWARE_CLASSES`` setting, as in this example::
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
MIDDLEWARE_CLASSES = (
|
|
|
|
"django.middleware.cache.CacheMiddleware",
|
2005-09-02 21:22:49 +00:00
|
|
|
"django.middleware.common.CommonMiddleware",
|
2005-07-19 20:49:07 +00:00
|
|
|
)
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
(The order of ``MIDDLEWARE_CLASSES`` matters. See "Order of MIDDLEWARE_CLASSES"
|
|
|
|
below.)
|
2005-09-02 21:22:49 +00:00
|
|
|
|
2005-11-20 05:17:55 +00:00
|
|
|
Then, add the following required settings to your Django settings file:
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
* ``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.
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
The cache middleware caches every page that doesn't have GET or POST
|
|
|
|
parameters. Additionally, ``CacheMiddleware`` automatically sets a few headers
|
|
|
|
in each ``HttpResponse``:
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
* Sets the ``Last-Modified`` header to the current date/time when a fresh
|
|
|
|
(uncached) version of the page is requested.
|
2005-10-09 00:55:08 +00:00
|
|
|
* 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.
|
|
|
|
|
|
|
|
See the `middleware documentation`_ for more on middleware.
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
.. _`middleware documentation`: http://www.djangoproject.com/documentation/middleware/
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
The per-page cache
|
|
|
|
==================
|
|
|
|
|
|
|
|
A more granular way to use the caching framework is by caching the output of
|
|
|
|
individual views. ``django.views.decorators.cache`` defines a ``cache_page``
|
|
|
|
decorator that will automatically cache the view's response for you. It's easy
|
|
|
|
to use::
|
2005-07-15 17:05:43 +00:00
|
|
|
|
|
|
|
from django.views.decorators.cache import cache_page
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-07-15 17:05:43 +00:00
|
|
|
def slashdot_this(request):
|
|
|
|
...
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
slashdot_this = cache_page(slashdot_this, 60 * 15)
|
2005-07-15 17:05:43 +00:00
|
|
|
|
|
|
|
Or, using Python 2.4's decorator syntax::
|
|
|
|
|
|
|
|
@cache_page(60 * 15)
|
|
|
|
def slashdot_this(request):
|
|
|
|
...
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
``cache_page`` takes a single argument: the cache timeout, in seconds. In the
|
|
|
|
above example, the result of the ``slashdot_this()`` view will be cached for 15
|
|
|
|
minutes.
|
2005-07-15 17:05:43 +00:00
|
|
|
|
|
|
|
The low-level cache API
|
|
|
|
=======================
|
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
Sometimes, however, caching an entire rendered page doesn't gain you very much.
|
|
|
|
For example, you may find it's only necessary to cache the result of an
|
|
|
|
intensive database. In cases like this, you can use the low-level cache API to
|
|
|
|
store objects in the cache with any level of granularity you like.
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-07-19 20:49:07 +00:00
|
|
|
The cache API is simple::
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
# The cache module exports a cache object that's automatically
|
|
|
|
# created from the CACHE_BACKEND setting.
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> from django.core.cache import cache
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
# The basic interface is set(key, value, timeout_seconds) and get(key).
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.set('my_key', 'hello, world!', 30)
|
|
|
|
>>> cache.get('my_key')
|
|
|
|
'hello, world!'
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
# (Wait 30 seconds...)
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.get('my_key')
|
|
|
|
None
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
# get() can take a default argument.
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.get('my_key', 'has_expired')
|
|
|
|
'has_expired'
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
# There's also a get_many() interface that only hits the cache once.
|
|
|
|
# Also, note that the timeout argument is optional and defaults to what
|
|
|
|
# you've given in the settings file.
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.set('a', 1)
|
|
|
|
>>> cache.set('b', 2)
|
|
|
|
>>> cache.set('c', 3)
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
# get_many() returns a dictionary with all the keys you asked for that
|
|
|
|
# actually exist in the cache (and haven't expired).
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.get_many(['a', 'b', 'c'])
|
|
|
|
{'a': 1, 'b': 2, 'c': 3}
|
2005-07-19 20:49:07 +00:00
|
|
|
|
|
|
|
# There's also a way to delete keys explicitly.
|
2005-07-15 17:05:43 +00:00
|
|
|
>>> cache.delete('a')
|
2005-07-19 20:49:07 +00:00
|
|
|
|
2005-08-31 17:06:07 +00:00
|
|
|
That's it. The cache has very few restrictions: You can cache any object that
|
|
|
|
can be pickled safely, although keys must be strings.
|
2005-07-15 17:05:43 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
Controlling cache: Using Vary headers
|
|
|
|
=====================================
|
|
|
|
|
|
|
|
The Django cache framework works with `HTTP Vary headers`_ to allow developers
|
|
|
|
to instruct caching mechanisms to differ their cache contents depending on
|
|
|
|
request HTTP headers.
|
|
|
|
|
|
|
|
Essentially, the ``Vary`` response HTTP header defines which request headers a
|
|
|
|
cache mechanism should take into account when building its cache key.
|
|
|
|
|
|
|
|
By default, Django's cache system creates its cache keys using the requested
|
|
|
|
path -- e.g., ``"/stories/2005/jun/23/bank_robbed/"``. This means every request
|
|
|
|
to that URL will use the same cached version, regardless of user-agent
|
|
|
|
differences such as cookies or language preferences.
|
|
|
|
|
|
|
|
That's where ``Vary`` comes in.
|
|
|
|
|
|
|
|
If your Django-powered page outputs different content based on some difference
|
|
|
|
in request headers -- such as a cookie, or language, or user-agent -- you'll
|
|
|
|
need to use the ``Vary`` header to tell caching mechanisms that the page output
|
|
|
|
depends on those things.
|
|
|
|
|
|
|
|
To do this in Django, use the convenient ``vary_on_headers`` view decorator,
|
|
|
|
like so::
|
|
|
|
|
|
|
|
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, a caching mechanism (such as Django's own cache middleware) will
|
|
|
|
cache a separate version of the page for each unique user-agent.
|
|
|
|
|
|
|
|
The advantage to using the ``vary_on_headers`` decorator rather than manually
|
|
|
|
setting the ``Vary`` header (using something like
|
|
|
|
``response['Vary'] = 'user-agent'``) is that the decorator adds to the ``Vary``
|
|
|
|
header (which may already exist) rather than setting it from scratch.
|
|
|
|
|
2005-10-09 01:03:04 +00:00
|
|
|
Note that you can pass multiple headers to ``vary_on_headers()``::
|
2005-10-09 00:55:08 +00:00
|
|
|
|
|
|
|
@vary_on_headers('User-Agent', 'Cookie')
|
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
|
|
|
|
Because varying on cookie is such a common case, there's a ``vary_on_cookie``
|
|
|
|
decorator. These two views are equivalent::
|
|
|
|
|
|
|
|
@vary_on_cookie
|
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
|
|
|
|
@vary_on_headers('Cookie')
|
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
|
|
|
|
Also note that the headers you pass to ``vary_on_headers`` are not case
|
|
|
|
sensitive. ``"User-Agent"`` is the same thing as ``"user-agent"``.
|
|
|
|
|
|
|
|
You can also use a helper function, ``patch_vary_headers()``, directly::
|
|
|
|
|
|
|
|
from django.utils.cache import patch_vary_headers
|
|
|
|
def 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 its first argument
|
|
|
|
and a list/tuple of header names as its second argument.
|
|
|
|
|
|
|
|
.. _`HTTP Vary headers`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.44
|
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
Controlling cache: Using other headers
|
|
|
|
======================================
|
|
|
|
|
|
|
|
Another problem with caching is the privacy of data and the question of where
|
|
|
|
data should be stored in a cascade of caches.
|
|
|
|
|
|
|
|
A user usually faces two kinds of caches: his own browser cache (a private
|
|
|
|
cache) and his provider's cache (a public cache). A public cache is used by
|
|
|
|
multiple users and controlled by someone else. This poses problems with
|
|
|
|
sensitive data: You don't want, say, your banking-account number stored in a
|
|
|
|
public cache. So Web applications need a way to tell caches which data is
|
|
|
|
private and which is public.
|
|
|
|
|
|
|
|
The solution is to indicate a page's cache should be "private." To do this in
|
|
|
|
Django, use the ``cache_control`` view decorator. Example::
|
|
|
|
|
|
|
|
from django.views.decorators.cache import cache_control
|
|
|
|
@cache_control(private=True)
|
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
|
|
|
|
This decorator takes care of sending out the appropriate HTTP header behind the
|
|
|
|
scenes.
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
There are a few other ways to control cache parameters. For example, HTTP
|
|
|
|
allows applications to do the following:
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
* Define the maximum time a page should be cached.
|
|
|
|
* Specify whether a cache should always check for newer versions, only
|
|
|
|
delivering the cached content when there are no changes. (Some caches
|
|
|
|
might deliver cached content even if the server page changed -- simply
|
|
|
|
because the cache copy isn't yet expired.)
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
In Django, use the ``cache_control`` view decorator to specify these cache
|
2005-10-31 03:17:39 +00:00
|
|
|
parameters. In this example, ``cache_control`` tells caches to revalidate the
|
|
|
|
cache on every access and to store cached versions for, at most, 3600 seconds::
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
from django.views.decorators.cache import cache_control
|
|
|
|
@cache_control(must_revalidate=True, max_age=3600)
|
2005-10-29 17:00:20 +00:00
|
|
|
def my_view(request):
|
|
|
|
...
|
|
|
|
|
2005-10-31 03:17:39 +00:00
|
|
|
Any valid ``Cache-Control`` directive is valid in ``cache_control()``. For a
|
|
|
|
full list, see the `Cache-Control spec`_. Just pass the directives as keyword
|
2005-10-31 03:14:57 +00:00
|
|
|
arguments to ``cache_control()``, substituting underscores for hyphens. For
|
|
|
|
directives that don't take an argument, set the argument to ``True``.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
2005-10-31 03:17:39 +00:00
|
|
|
* ``@cache_control(max_age=3600)`` turns into ``max-age=3600``.
|
|
|
|
* ``@cache_control(public=True)`` turns into ``public``.
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
(Note that the caching middleware already sets the cache header's max-age with
|
|
|
|
the value of the ``CACHE_MIDDLEWARE_SETTINGS`` setting. If you use a custom
|
|
|
|
``max_age`` in a ``cache_control`` decorator, the decorator will take
|
|
|
|
precedence, and the header values will be merged correctly.)
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-31 03:14:57 +00:00
|
|
|
.. _`Cache-Control spec`: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
|
2005-10-29 17:00:20 +00:00
|
|
|
|
2005-10-09 00:55:08 +00:00
|
|
|
Other optimizations
|
|
|
|
===================
|
|
|
|
|
|
|
|
Django comes with a few other pieces of middleware that can help optimize your
|
|
|
|
apps' performance:
|
|
|
|
|
|
|
|
* ``django.middleware.http.ConditionalGetMiddleware`` adds support for
|
|
|
|
conditional GET. This makes use of ``ETag`` and ``Last-Modified``
|
|
|
|
headers.
|
|
|
|
|
|
|
|
* ``django.middleware.gzip.GZipMiddleware`` compresses content for browsers
|
|
|
|
that understand gzip compression (all modern browsers).
|
|
|
|
|
|
|
|
Order of MIDDLEWARE_CLASSES
|
|
|
|
===========================
|
|
|
|
|
|
|
|
If you use ``CacheMiddleware``, it's important to put it in the right place
|
|
|
|
within the ``MIDDLEWARE_CLASSES`` setting, because the cache middleware needs
|
|
|
|
to know which headers by which to vary the cache storage. Middleware always
|
|
|
|
adds something the ``Vary`` response header when it can.
|
|
|
|
|
|
|
|
Put the ``CacheMiddleware`` after any middlewares that might add something to
|
|
|
|
the ``Vary`` header. The following middlewares do so:
|
|
|
|
|
|
|
|
* ``SessionMiddleware`` adds ``Cookie``
|
2005-10-09 01:02:30 +00:00
|
|
|
* ``GZipMiddleware`` adds ``Accept-Encoding``
|