Django provides a possibility to speed up the request-response cycle by caching the most expensive parts such as database queries or template rendering. The fastest and most reliable caching natively supported by Django is the memory-based cache server, Memcached. In this recipe, you will learn how to use Memcached to cache a view for our viral_videos
app that we created in the Using database query expressions recipe earlier in this chapter.
There are several things to do in order to prepare caching for your Django project:
Install Memcached server, as follows:
$ wget http://memcached.org/files/memcached-1.4.23.tar.gz $ tar -zxvf memcached-1.4.23.tar.gz $ cd memcached-1.4.23 $ ./configure && make && make test && sudo make install
Start Memcached server, as shown in the following:
$ memcached -d
Install Memcached Python bindings in your virtual environment, as follows:
(myproject_env)$ pip install python-memcached