There are several things to do in order to prepare caching for your Django project. First, let's examine how this would be done for a virtual environment project:
- Install the memcached server, as follows:
$ wget http://memcached.org/files/memcached-1.5.7.tar.gz $ tar -zxvf memcached-1.4.23.tar.gz $ cd memcached-1.4.23 $ ./configure && make && make test && sudo make install
- Start the memcached server, as shown in the following:
$ memcached -d
- Install Memcached Python bindings in your virtual environment, as follows:
(myproject_env)$ pip3 install python-memcached~=1.59.0
If using a Docker environment, follow these steps instead:
- Update your docker-compose.yml file to associate a Memcached container with your app, as in the following:
# docker-compose.yml
version: '3'
services:
# ...
memcached:
image: 'memcached:1.5'
app:
# ...
environment:
# ...
- "CACHE_LOCATION=memcached:11211"
- Include the Memcached Python bindings in the requirements for your app container, like so:
# requirements.txt or requirements/base.txt
# ...
python-memcached~=1.59.0
- Stop, build, and restart your containers.