Using memcached for Django Cache Framework

The fastest, most efficient type of cache supported natively by Django, Memcached is an entirely memory-based cache server, used to reduce database access or store pre-computed stuff that will be used in the near future. It is used by sites such as Facebook and Wikipedia and have dramatically improved their site performance.

The first step to this process is setting memcached server. Here is how you can set up memcached server in RedHat Linux:

  1. Install memcached
  2. sudo yum install memcached
  3. Start the service
  4. sudo service memcached start
  5. Configuration of memcached
  6. memcached -h lists all the commands that are available from the bash. The default configuration for the memcached server is saved in a file at /etc/init.d/memcached. In most cases, you may have to modify the file to your needs – for example you may have to change the server host, port number, cache size etc. Feel free make the change and restart the server sudo service memcached restart. At the beginning of the config file, you will see the following code:

     if [ -f /etc/sysconfig/memcached ];then
       . /etc/sysconfig/memcached
     fi
    

    meaning it reads the variables listed in the file /etc/sysconfig/memcached if it exists. In the function named “start”, you will see the following line:

      daemon --pidfile ${pidfile} memcached -d -l $HOST 
                                               -p $PORT 
                                               -u $USER  
                                               -m $CACHESIZE 
                                               -c $MAXCONN 
                                               -P ${pidfile} 
                                               $OPTIONS
    

    These are the variables it is looking for when the server is started. You can either provide the variables from the file /etc/sysconfig/memcached as the following:

      HOST="0.0.0.0"
      PORT="11211"
      USER="memcached"
      MAXCONN="1024"
      CACHESIZE="64"
      OPTIONS=""
    

    or provide the values editing the config file directly. Just note that it will first look for values in the /etc/sysconfig/memcached file.

Once you are done setting up memcached, you can check its status by:
sudo service memcached status

Now, it is time to hook up memcached server with Django. First off, you need to have have a Python binding that will talk to the server. Two bindings are available – python-memcached and pylibmc. I used python-memcached as it was a little more straight-forward to install than that of pylibmc. For python-memcached, I downloaded the tar file from here, extracted the file and ran “python setup.py install” from the extracted folder. It was as simple as that.

To set up your django project to use memcached as the cache backend, following need to be added to the settings.py file:

CACHES = {'default': {
'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
'LOCATION': '0.0.0.0:11211'}
}