How to use memcached in Django?


The fact that dynamic websites provide an interactive and adaptable website is their calculations, including database queries, template rendering, and business logic, to produce the page that your site's visitor sees. This is much more expensive in terms of processing overhead than the usual accessing a file from the system server setup. For medium to high traffic sites, it is essential to cut as much overhead as possible. This is where caching comes in. Saving the result of a time-consuming calculation so that you don't have to do it again is referred to as caching.

The caching mechanism requires very little setup. Specifically, you must tell it where your cached data should be stored, whether in a database, on the file system, or in memory. This is a critical decision which will affect the performance of the cache since some cache are faster than others. Caching is often faster than file system or database caching because it avoids the overhead of accessing the file system or database.

What is Memcached?

Memcached is a memory-based caching technology that was created at Live Journal to manage large loads and then open-sourced by Danga Interactive. Sites like Slashdot and Wikipedia utilize it to drastically improve site performance by reducing database access. At http://danga.com/memcached/, you can get Memcached for free. It runs as a daemon and is given a set amount of memory. Its main function is to provide a user interface for adding, retrieving, and deleting data from the cache. Because all data is saved in memory, there is no database or storage overhead.

After installing Memcached, you'll need to install the Python bindings for Memcached, which are not included with Django. These bindings are contained in the memcache.py Python module, which may be found at the following link http://www.tummy.com/Community/software/python-memcached/

How to set memcached in Django?

To set a cache preference, the data in CACHE_BACKEND setting in settings.py file can be changed. If the developer chooses to use caching and does not specify the CACHE_BACKEND, by default Django will use the :/// symbol.

Memcached can be used with Django by setting CACHE_BACKEND to memcached://ip:port/. Here ip is the IP address of the Memcached daemon and port is the port on which the Memcached is running.

The local host of the Django project runs on 127.0.0.1. In the following example, the memcached is running on the same IP address with port 1109

CACHE_BACKEND = 'memcached://127.0.0.1:1109/

CACHES = {
   'default': {
      'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
      'LOCATION': '127.0.0.1:1109',
   }
}

Advantage of Memecached

The main advantage of Memcached is, it can run cache on multiple servers. This means that memcached daemons run on multiple machines and all the programs running on multiple machines will assume they are working with a single cach.

This eliminates the need to duplicate cache values for every machine. This feature can be used in the following way.

CACHE_BACKEND = 'memcached://172.15.25.212:1108;172.16.24.209:1109/'

CACHES = {
   'default': {
      'BACKEND': 'django.core.cache.backends.memcached.PyMemcacheCache',
      'LOCATION': [
         '172.15.25.212:1108',
         172.16.24.209:1109
   }
}

Disadvantage of Memcached

The main disadvantage of memory based caching is that it is temporary. All the data stored in it will disappear if the server crashes. So, any and all caching based storages are only temporary and should not be your only storage solution.

Updated on: 05-Sep-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements