Previous Page Next Page

Hour 23. Configuring Caching

What You'll Learn in This Hour

  • How to configure cache backends

  • How to implement a per-site cache

  • How to implement a per-view cache

  • How to manage access to cached pages

One of Django's best features is that it lets you easily generate web pages on-the-fly. It saves you a lot of development and maintenance time. However, this advantage comes at a price. Each time a request comes in, Django spends many cycles in database queries and page generation. On small to medium websites, this isn't much of a problem. However, on large sites that receive several requests per second, this can quickly become a problem.

Django's caching framework solves this problem nicely. Using the cache framework, you can cache web pages and objects so that subsequent requests for the same data can quickly be drawn from the cache rather than performing the resource-intensive query and processing again.

In this hour, we will discuss configuring caching and the different types of backends that are available. We will then discuss how to implement cache at the site, view, and object levels. Finally, we will cover how to use the response headers to manage caching in upstream caches.

Configuring Caching Backends

Django's caching system is easy to implement. The only thing you need to do is define in the settings.py file which backend you want Django to use for caching. Django includes several backends that you can use depending on your particular needs. To enable a caching backend, set the CACHE_BACKEND setting in the settings.py file. For example, to enable local memory caching, you would use the following setting in the settings.py file:

CACHE_BACKEND = 'locmem:///'

You can also configure how long Django caches data. You can add the following arguments to the CACHE_BACKEND setting:

For example, the following setting keeps data in the cache for 2 minutes and allows 200 cached entries:

CACHE_BACKEND = 'locmem:///?timeout=120&max_entries=200'

The following sections describe each of the available backends you can configure for your website.

Database Backend

The database backend allows you to create a table in the database that can then be used to store and retrieve cached data. An advantage of using the database backend is that the cached data is persistently stored and is available even after a server reboot.

Before you can enable the database backend, you need to create a table in the database to store the cache using Python's createcachetable application at the root of your project. The table can be given any valid table name as long as the database doesn't already have a table with that name. For example, the following command creates a database backend table called mysitecache:

python manage.py createcachetable mysitecache

To enable the database backend in the settings.py file, set the CACHE_BACKEND to the db:// backend and provide the name of the cache table. For example, to enable the database backend using the table just listed, you would use the following setting:

CACHE_BACKEND = 'db://mysitecache'

By the Way

The database backend uses the same database for the cache that you will use for the rest of the website. This can adversely affect the database's performance if your site receives a lot of hits.


File System Backend

The file system backend allows you to define a directory in which Django stores and retrieves cached data. An advantage of using the file system backend is that the cached data is stored persistently in the file system and is available even after a server reboot.

Did you Know?

Cached data is stored as individual files. The contents of the files are in Python pickled format. The file system backend uses the cache key as the filename (escaped for the purpose of security).


Before you can enable the file system backend, you need to create a directory in which to store the cached data.

Watch Out!

Django needs read and write permissions to that directory. For example, if your web server is running as the user apache, you need to grant the apache user read and write access to the directory.


To enable the file system backend in the settings.py file, you should set the CACHE_BACKEND to the file:// backend and provide the full path to the directory. For example, if you create a directory called /var/temp/mysitecache on a Linux system, you would use the following setting to the settings.py file:

CACHE_BACKEND = 'db:///mysitecache'

As another example, if you create a directory called c:\temp\mysitecache on a Windows system, you would use the following setting to the settings.py file:

CACHE_BACKEND = 'db://c:/temp/mysitecache'

Local Memory Backend

The local memory backend uses the system memory to store and retrieve cached data. An advantage of using the local memory backend is that the cached data is stored in memory, which is extremely quick to access. The local memory backend uses locking to ensure that it is multiprocess and thread-safe.

Watch Out!

Cached data that is stored in the local memory backend is lost if the server crashes. You should not rely on items in the local memory cache as any kind of data storage.


To enable the local memory backend in the settings.py file, set the CACHE_BACKEND to the locmem:/// backend:

CACHE_BACKEND = 'locmem:///'

Simple Backend

The simple backend caches data in memory for a single process. This is useful when you are developing the website and for testing purposes. However, you should not use it in production.

To enable the simple backend in the settings.py file, set the CACHE_BACKEND to the simple:/// backend:

CACHE_BACKEND = 'simple:///'

Dummy Backend

The dummy backend does not cache any data, but it enables the cache interface. The dummy backend should be used only in development or test websites.

To enable the dummy backend in the settings.py file, set the CACHE_BACKEND to the dummy:/// backend:

CACHE_BACKEND = 'dummy:///'

Did you Know?

The dummy backend is useful if you need to test or debug a website that has a heavy amount of caching. All you need to do is modify the CACHE_BACKEND setting for the test environment.


Memcached Backend

The fastest and most efficient backend available for Django is the Memcached backend. It runs as a daemon that stores and retrieves data into a memory cache.

The Memcached backend is not distributed with Django; you must obtain it from www.django.com/memcached/. Before you can enable Memcached, you must install it, along with the Memcached Python bindings. The Memcached Python bindings are in the Python module, memcache.py, which you can find at www.djangoproject.com/thirdparty/python-memcached/.

To enable the Memcached backend in the settings.py file, you should set the CACHE_BACKEND to the memcached:// backend and provide the IP address and port that the Memcached daemon is running on. For example, if the Memcached daemon is running on the local host (127.0.0.1) using port 12221, you would use the following setting:

CACHE_BACKEND = 'memcached://127.0.0.1:12221'

One of the best features of Memcached is that you can distribute the cache over multiple servers by running the Memcached daemon on multiple machines. Memcached treats the servers as a single cache.

Try It Yourself: Enable a Database Backend for Caching

In this section, you will create a table in the database and enable the database backend to use that table for caching in the iFriends project. Follow these steps to enable the database backend:

1.
Stop the development server.

2.
From the root of the iFriends project, use the following command to create a table named ifriends_cache in the database:

python manage.py createcachetable ifriends_cache

3.
Open the iFriends/settings.py file in an editor.

4.
Add the following line to enable the database backend using the ifriends_cache table you created in step 2. Specify a timeout of 2 minutes and the maximum number of entries as 200:

CACHE_BACKEND = 'db://ifriends_cache?timeout=120&max_entries=200'

5.
Save the iFriends/settings.py file.

6.
Start the development server.


Previous Page Next Page