Django’s cache framework¶
A fundamental trade-off in dynamic websites is, well, they’re dynamic. Each time a user requests a page, the Web server makes all sorts of calculations – from database queries to template rendering to business logic – to create the page that your site’s visitor sees. This is a lot more expensive, from a processing-overhead perspective, than your standard read-a-file-off-the-filesystem server arrangement.
For most Web applications, this overhead isn’t a big deal. Most Web
applications aren’t washingtonpost.com or slashdot.org; they’re simply
small- to medium-sized sites with so-so traffic. But for medium- to
high-traffic sites, it’s essential to cut as much overhead as possible.
That’s where caching comes in.
To cache something is to save the result of an expensive calculation so that you don’t have to perform the calculation next time. Here’s some pseudocode explaining how this would work for a dynamically generated Web page:
given a URL, try finding that page in the cache
if the page is in the cache:
return the cached page
else:
generate the page
save the generated page in the cache (for next time)
return the generated page
Django comes with a robust cache system that lets you save dynamic pages so they don’t have to be calculated for each request. For convenience, Django offers different levels of cache granularity: You can cache the output of specific views, you can cache only the pieces that are difficult to produce, or you can cache your entire site.
Django also works well with “downstream” caches, such as Squid and browser-based caches. These are the types of caches that you don’t directly control but to which you can provide hints (via HTTP headers) about which parts of your site should be cached, and how.
See also
The Cache Framework design philosophy explains a few of the design decisions of the framework.
Setting up the cache¶
The cache system requires a small amount of setup. Namely, you have to tell it where your cached data should live – whether in a database, on the filesystem or directly in memory. This is an important decision that affects your cache’s performance; yes, some cache types are faster than others.
Your cache preference goes in the CACHES setting in your
settings file. Here’s an explanation of all available values for
CACHES.
Memcached¶
The fastest, most efficient type of cache supported natively by Django, Memcached is an entirely memory-based cache server, originally developed to handle high loads at LiveJournal.com and subsequently open-sourced by Danga Interactive. It is used by sites such as Facebook and Wikipedia to reduce database access and dramatically increase site performance.
Memcached runs as a daemon and is allotted a specified amount of RAM. All it does is provide a fast interface for adding, retrieving and deleting data in the cache. All data is stored directly in memory, so there’s no overhead of database or filesystem usage.
After installing Memcached itself, you’ll need to install a Memcached binding. There are several Python Memcached bindings available; the two most common are python-memcached and pylibmc.
To use Memcached with Django:
- Set
BACKENDtodjango.core.cache.backends.memcached.MemcachedCacheordjango.core.cache.backends.memcached.PyLibMCCache(depending on your chosen memcached binding) - Set
LOCATIONtoip:portvalues, whereipis the IP address of the Memcached daemon andportis the port on which Memcached is running, or to aunix:pathvalue, wherepathis the path to a Memcached Unix socket file.
In this example, Memcached is running on localhost (127.0.0.1) port 11211, using
the python-memcached binding:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '127.0.0.1:11211',
}
}
In this example, Memcached is available through a local Unix socket file
/tmp/memcached.sock using the python-memcached binding:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': 'unix:/tmp/memcached.sock',
}
}
When using the pylibmc binding, do not include the unix:/ prefix:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.PyLibMCCache',
'LOCATION': '/tmp/memcached.sock',
}
}
One excellent feature of Memcached is its ability to share a cache over
multiple servers. This means you can run Memcached daemons on multiple
machines, and the program will treat the group of machines as a single
cache, without the need to duplicate cache values on each machine. To take
advantage of this feature, include all server addresses in
LOCATION, either separated by semicolons or as
a list.
In this example, the cache is shared over Memcached instances running on IP address 172.19.26.240 and 172.19.26.242, both on port 11211:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': [
'172.19.26.240:11211',
'172.19.26.242:11211',
]
}
}
In the following example, the cache is shared over Memcached instances running on the IP addresses 172.19.26.240 (port 11211), 172.19.26.242 (port 11212), and 172.19.26.244 (port 11213):
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': [
'172.19.26.240:11211',
'172.19.26.242:11212',
'172.19.26.244:11213',
]
}
}
A final point about Memcached is that memory-based caching has a disadvantage: because the cached data is stored in memory, the data will be lost if your server crashes. Clearly, memory isn’t intended for permanent data storage, so don’t rely on memory-based caching as your only data storage. Without a doubt, none of the Django caching backends should be used for permanent storage – they’re all intended to be solutions for caching, not storage – but we point this out here because memory-based caching is particularly temporary.
Database caching¶
Django can store its cached data in your database. This works best if you’ve got a fast, well-indexed database server.
To use a database table as your cache backend:
- Set
BACKENDtodjango.core.cache.backends.db.DatabaseCache - Set
LOCATIONtotablename, the name of the database table. This name can be whatever you want, as long as it’s a valid table name that’s not already being used in your database.
In this example, the cache table’s name is my_cache_table:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.db.DatabaseCache',
'LOCATION': 'my_cache_table',
}
}
Creating the cache table¶
Before using the database cache, you must create the cache table with this command:
python manage.py createcachetable
This creates a table in your database that is in the proper format that
Django’s database-cache system expects. The name of the table is taken from
LOCATION.
If you are using multiple database caches, createcachetable creates
one table for each cache.
If you are using multiple databases, createcachetable observes the
allow_migrate() method of your database routers (see below).
Like migrate, createcachetable won’t touch an existing
table. It will only create missing tables.
To print the SQL that would be run, rather than run it, use the
createcachetable --dry-run option.
Multiple databases¶
If you use database caching with multiple databases, you’ll also need
to set up routing instructions for your database cache table. For the
purposes of routing, the database cache table appears as a model named
CacheEntry, in an application named django_cache. This model
won’t appear in the models cache, but the model details can be used
for routing purposes.
For example, the following router would direct all cache read
operations to cache_replica, and all write operations to
cache_primary. The cache table will only be synchronized onto
cache_primary:
class CacheRouter(object):
"""A router to control all database cache operations"""
def db_for_read(self, model, **hints):
"All cache read operations go to the replica"
if model._meta.app_label == 'django_cache':
return 'cache_replica'
return None
def db_for_write(self, model, **hints):
"All cache write operations go to primary"
if model._meta.app_label == 'django_cache':
return 'cache_primary'
return None
def allow_migrate(self, db, app_label, model_name=None, **hints):
"Only install the cache model on primary"
if app_label == 'django_cache':
return db == 'cache_primary'
return None
If you don’t specify routing directions for the database cache model,
the cache backend will use the default database.
Of course, if you don’t use the database cache backend, you don’t need to worry about providing routing instructions for the database cache model.
Filesystem caching¶
The file-based backend serializes and stores each cache value as a separate
file. To use this backend set BACKEND to
"django.core.cache.backends.filebased.FileBasedCache" and
LOCATION to a suitable directory. For example,
to store cached data in /var/tmp/django_cache, use this setting:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
}
}
If you’re on Windows, put the drive letter at the beginning of the path, like this:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': 'c:/foo/bar',
}
}
The directory path should be absolute – that is, it should start at the root of your filesystem. It doesn’t matter whether you put a slash at the end of the setting.
Make sure the directory pointed-to by this setting exists and is readable and
writable by the system user under which your Web server runs. Continuing the
above example, if your server runs as the user apache, make sure the
directory /var/tmp/django_cache exists and is readable and writable by the
user apache.
Local-memory caching¶
This is the default cache if another is not specified in your settings file. If
you want the speed advantages of in-memory caching but don’t have the capability
of running Memcached, consider the local-memory cache backend. This cache is
per-process (see below) and thread-safe. To use it, set BACKEND to "django.core.cache.backends.locmem.LocMemCache". For
example:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.locmem.LocMemCache',
'LOCATION': 'unique-snowflake',
}
}
The cache LOCATION is used to identify individual
memory stores. If you only have one locmem cache, you can omit the
LOCATION; however, if you have more than one local
memory cache, you will need to assign a name to at least one of them in
order to keep them separate.
Note that each process will have its own private cache instance, which means no cross-process caching is possible. This obviously also means the local memory cache isn’t particularly memory-efficient, so it’s probably not a good choice for production environments. It’s nice for development.
Dummy caching (for development)¶
Finally, Django comes with a “dummy” cache that doesn’t actually cache – it just implements the cache interface without doing anything.
This is useful if you have a production site that uses heavy-duty caching in
various places but a development/test environment where you don’t want to cache
and don’t want to have to change your code to special-case the latter. To
activate dummy caching, set BACKEND like so:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
}
}
Using a custom cache backend¶
While Django includes support for a number of cache backends out-of-the-box,
sometimes you might want to use a customized cache backend. To use an external
cache backend with Django, use the Python import path as the
BACKEND of the CACHES setting, like so:
CACHES = {
'default': {
'BACKEND': 'path.to.backend',
}
}
If you’re building your own backend, you can use the standard cache backends
as reference implementations. You’ll find the code in the
django/core/cache/backends/ directory of the Django source.
Note: Without a really compelling reason, such as a host that doesn’t support them, you should stick to the cache backends included with Django. They’ve been well-tested and are easy to use.
Cache arguments¶
Each cache backend can be given additional arguments to control caching
behavior. These arguments are provided as additional keys in the
CACHES setting. Valid arguments are as follows:
TIMEOUT: The default timeout, in seconds, to use for the cache. This argument defaults to300seconds (5 minutes). You can setTIMEOUTtoNoneso that, by default, cache keys never expire. A value of0causes keys to immediately expire (effectively “don’t cache”).OPTIONS: Any options that should be passed to the cache backend. The list of valid options will vary with each backend, and cache backends backed by a third-party library will pass their options directly to the underlying cache library.Cache backends that implement their own culling strategy (i.e., the
locmem,filesystemanddatabasebackends) will honor the following options:MAX_ENTRIES: The maximum number of entries allowed in the cache before old values are deleted. This argument defaults to300.CULL_FREQUENCY: The fraction of entries that are culled whenMAX_ENTRIESis reached. The actual ratio is1 / CULL_FREQUENCY, so setCULL_FREQUENCYto2to cull half the entries whenMAX_ENTRIESis reached. This argument should be an integer and defaults to3.A value of
0forCULL_FREQUENCYmeans that the entire cache will be dumped whenMAX_ENTRIESis reached. On some backends (databasein particular) this makes culling much faster at the expense of more cache misses.
KEY_PREFIX: A string that will be automatically included (prepended by default) to all cache keys used by the Django server.See the cache documentation for more information.
VERSION: The default version number for cache keys generated by the Django server.See the cache documentation for more information.
KEY_FUNCTIONA string containing a dotted path to a function that defines how to compose a prefix, version and key into a final cache key.See the cache documentation for more information.
In this example, a filesystem backend is being configured with a timeout of 60 seconds, and a maximum capacity of 1000 items:
CACHES = {
'default': {
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
'LOCATION': '/var/tmp/django_cache',
'TIMEOUT': 60,
'OPTIONS': {
'MAX_ENTRIES': 1000
}
}
}
Invalid arguments are silently ignored, as are invalid values of known arguments.
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 setting, as in this example:
MIDDLEWARE = [
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
]
Note
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 below 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-Modifiedheader to the current date/time when a fresh (not cached) version of the page is requested. - Sets the
Expiresheader to the current date/time plus the definedCACHE_MIDDLEWARE_SECONDS. - Sets the
Cache-Controlheader to give a max age for the page – again, from theCACHE_MIDDLEWARE_SECONDSsetting.
See Middleware for more on middleware.
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). See the
using other headers section for more on these decorators.
If USE_I18N is set to True then the generated cache key will
include the name of the active language – see also
How Django discovers language preference). 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.
The per-view cache¶
-
django.views.decorators.cache.cache_page()¶
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:
from django.views.decorators.cache import cache_page
@cache_page(60 * 15)
def my_view(request):
...
cache_page takes a single argument: the cache timeout, in seconds. In the
above example, the result of the my_view() view will be cached for 15
minutes. (Note that we’ve written it as 60 * 15 for the purpose of
readability. 60 * 15 will be evaluated to 900 – that is, 15 minutes
multiplied by 60 seconds per minute.)
The per-view cache, like the per-site cache, is keyed off of the URL. If
multiple URLs point at the same view, each URL will be cached separately.
Continuing the my_view example, if your URLconf looks like this:
urlpatterns = [
url(r'^foo/([0-9]{1,2})/$', my_view),
]
then requests to /foo/1/ and /foo/23/ will be cached separately, as
you may expect. But once a particular URL (e.g., /foo/23/) has been
requested, subsequent requests to that URL will use the cache.
cache_page can also take an optional keyword argument, cache,
which directs the decorator to use a specific cache (from your
CACHES setting) when caching view results. By default, the
default cache will be used, but you can specify any cache you
want:
@cache_page(60 * 15, cache="special_cache")
def my_view(request):
...
You can also override the cache prefix on a per-view basis. cache_page
takes an optional keyword argument, key_prefix,
which works in the same way as the CACHE_MIDDLEWARE_KEY_PREFIX
setting for the middleware. It can be used like this:
@cache_page(60 * 15, key_prefix="site1")
def my_view(request):
...
The key_prefix and cache arguments may be specified together. The
key_prefix argument and the KEY_PREFIX
specified under CACHES will be concatenated.
Specifying per-view cache in the URLconf¶
The examples in the previous section have hard-coded the fact that the view is
cached, because cache_page alters the my_view function in place. This
approach couples your view to the cache system, which is not ideal for several
reasons. For instance, you might want to reuse the view functions on another,
cache-less site, or you might want to distribute the views to people who might
want to use them without being cached. The solution to these problems is to
specify the per-view cache in the URLconf rather than next to the view functions
themselves.
Doing so is easy: simply wrap the view function with cache_page when you
refer to it in the URLconf. Here’s the old URLconf from earlier:
urlpatterns = [
url(r'^foo/([0-9]{1,2})/$', my_view),
]
Here’s the same thing, with my_view wrapped in cache_page:
from django.views.decorators.cache import cache_page
urlpatterns = [
url(r'^foo/([0-9]{1,2})/$', cache_page(60 * 15)(my_view)),
]
Template fragment caching¶
If you’re after even more control, you can also cache template fragments using
the cache template tag. To give your template access to this tag, put
{% load cache %} near the top of your template.
The {% cache %} template tag caches the contents of the block for a given
amount of time. It takes at least two arguments: the cache timeout, in seconds,
and the name to give the cache fragment. The name will be taken as is, do not
use a variable. For example:
{% load cache %}
{% cache 500 sidebar %}
.. sidebar ..
{% endcache %}
Sometimes you might want to cache multiple copies of a fragment depending on
some dynamic data that appears inside the fragment. For example, you might want a
separate cached copy of the sidebar used in the previous example for every user
of your site. Do this by passing additional arguments to the {% cache %}
template tag to uniquely identify the cache fragment:
{% load cache %}
{% cache 500 sidebar request.user.username %}
.. sidebar for logged in user ..
{% endcache %}
It’s perfectly fine to specify more than one argument to identify the fragment.
Simply pass as many arguments to {% cache %} as you need.
If USE_I18N is set to True the per-site middleware cache will
respect the active language. For the cache template
tag you could use one of the
translation-specific variables available in
templates to achieve the same result:
{% load i18n %}
{% load cache %}
{% get_current_language as LANGUAGE_CODE %}
{% cache 600 welcome LANGUAGE_CODE %}
{% trans "Welcome to example.com" %}
{% endcache %}
The cache timeout can be a template variable, as long as the template variable
resolves to an integer value. For example, if the template variable
my_timeout is set to the value 600, then the following two examples are
equivalent:
{% cache 600 sidebar %} ... {% endcache %}
{% cache my_timeout sidebar %} ... {% endcache %}
This feature is useful in avoiding repetition in templates. You can set the timeout in a variable, in one place, and just reuse that value.
By default, the cache tag will try to use the cache called “template_fragments”.
If no such cache exists, it will fall back to using the default cache. You may
select an alternate cache backend to use with the using keyword argument,
which must be the last argument to the tag.
{% cache 300 local-thing ... using="localcache" %}
It is considered an error to specify a cache name that is not configured.
-
django.core.cache.utils.make_template_fragment_key(fragment_name, vary_on=None)¶
If you want to obtain the cache key used for a cached fragment, you can use
make_template_fragment_key. fragment_name is the same as second argument
to the cache template tag; vary_on is a list of all additional arguments
passed to the tag. This function can be useful for invalidating or overwriting
a cached item, for example:
>>> from django.core.cache import cache
>>> from django.core.cache.utils import make_template_fragment_key
# cache key for {% cache 500 sidebar username %}
>>> key = make_template_fragment_key('sidebar', [username])
>>> cache.delete(key) # invalidates cached template fragment