--- layout: post status: publish published: true title: Django and Memcached author: display_name: sipp11 login: sipp11 email: sipp11@gmail.com url: '' author_login: sipp11 author_email: sipp11@gmail.com wordpress_id: 889 wordpress_url: http://blog.10ninox.com/?p=889 date: '2014-01-23 02:20:24 +0700' date_gmt: '2014-01-22 19:20:24 +0700' categories: - coding tags: - python - django - memcached ---

Apparently it seems that both Django and Memcached work together flawlessly, but it fails silently most of the time which is hard to debug. One thing I've learned is never cache any queryset with Memcached since it wouldn't complain anything but doesn't do anything either. If you like to cache queryset, then using LoMemCache, FileBasedCache, DummyCache or whatever which is not Memcached. Everything will be all good.

Otherwise, you can separate cache into couple things then push queryset to one which is not Memcached.

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211'
    },
    'queryset': {
        'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'
    },
}

Then you can using get_cache to pick whatever you want

from django.core.cache import get_cache, cache
mycache = get_cache('queryset')

While cache will still point to 'default' or Memcached one.