Memcached and Django

Here a quickie, since I’ve been silent for so long.

I find myself with a REST service taking way too long to process one of its queries, it’s not bad per se, but it will get bad when this query gets hit more often.

Memcached to the rescue.

Installing memcached

> apt-get install memcached

As easy as it looks. I’m sure it is also available in other systems with different install commands pip, pacman, brew.

In Debian/Ubuntu case, apt already sets up the system, so you can see if memcached is running:
> sudo service memcached status
and if not, simply start it
> sudo service memcached start

Django setup

Django is ready to work with memcached, and has a very good documentation as usual.

You’ll need to install either python-memcached or pylibmc, I’ve not checked the difference between them, so you pick whatever you feel like :-D.

> pip install python-memcached

And finally, configure Django. Open your settings.py file and set up a cache server.

CACHES = {
  'default': {
      'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
      'LOCATION': '127.0.0.1:11211', #Or your memcached server IP
  }
}

Caching things

Django has caching facilities and decorators, but for this case I use the so called “low level api”. What this means is that you actually write what you need to cache and when to retrieve it.

Very straightforward, check the following example:

(...)

from django.core.cache import cache

def an_example_view(request):
    cacheid = 'someID'
    cached_data = cache.get(cacheid)

    if cached_data:
        return Response(cached_data)

    # Perform your view thing
    # result ends ends up in newdata
    cache_seconds = 60*5
    cache.set(cacheid, newdata, cache_seconds)
    return Response(newdata)

(...)

It is just a matter of checking if a cached value exists and then return it. If it does not exist, follow the default path.

Conclusion

Quick and easy, vastly improving the response time of that endpoint (From a horrible 13 seconds, to a not so horrible in retrospective 1 second)

There’s still a long way to go to make it even faster, but that’s a huge whale of endpoint, so we’ll see.

Advertisement

Leave a comment

Filed under code, tools

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.