Python Memcached Implementation for Django project

A fundamental trade-off in dynamic websites is they’re dynamic. Every time a user requests a page, the Web server fetches data from database then applies logic and renders in templates.  This will affect page load time due to time taken by server to fetch data and apply the business logic,

That’s where caching comes in.

To cache something is to save the result of an expensive calculation so that you don’t have to perform the calculation next time. In This Blog Post, Lets see how to use Memcached for server-side application caching.  Memcached is an in-memory key-value pair store, that helps in caching dynamic websites. Django uses python-memcached binding for communication between our web application and Memcached Server. 

apt-get install memcached         #to install memcached-server
pip install python-memcached

Add the following settings to settings.py or django settings

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': 'unix:/tmp/memcached.sock',
    }
}

Now you're all set use caching.

from django.core.cache import cache
cache.set(cache_key, result, cache_time)          # to cache a value
cache.set_many({'a': 1, 'b': 2, 'c': 3})          # to cache many keys
cache.get(cache_key)                              # to retive a key from cache
cache.get_many(['a', 'b', 'c'])                   # to retrive many keys from cache

To use as Decorator:

from django.views.decorators.cache import cache_page

@cache_page(60 * 10)
def my_view(request):
    ....

the above code will cache the page for 10 mins.

For template level caching:

{% load cache %}
 ... non cached content here ...
{% cache  cache_key %}
cached content here
​{% endcache %}

If you want to delete a specific key from cache or clear entire cache.

cache.delete('a')     # where a is key
cache.delete_many(['a', 'b', 'c'])
cache.clear()

These are various forms of caching implementations. For more information visit Django Official SIte

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Get count, average, min, max values from model field using Django Aggregate Ravi Kumar Gadila

To retrieve maximum, minimum or average values from group of rows we can use django Aggregation. For example to retrieve max price or avg price ...

Continue Reading...
How to Create your own e-commerce shop using Django-Oscar. Shirisha Gaddi

Oscar is an open-source ecommerce framework for Django. Django Oscar provides a base platform to build an online shop. Oscar is built as a highly ...

Continue Reading...
How to create Periodic Tasks in Celery? Nikhila Mergu

Celery provides asynchronous job queues, which allows you to run Python functions in the background. Celery is on the Python Package Index (PyPi), and can ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties