djangomemcachedcelerydjango-cachepython-memcached

memcache on django is not working


I have a race condition in Celery. Inspired by this - http://ask.github.io/celery/cookbook/tasks.html#ensuring-a-task-is-only-executed-one-at-a-time I decided to use memcache to add locks to my tasks.

These are the changes I made:

python-memcached

# settings for memcache
CACHES = {
  'default': {
    'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
    'LOCATION': '127.0.0.1:11211',
   }
}

After this I login to my shell and do the following

>>> import os
>>> import django
>>> from django.core.cache import cache
>>> os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'proj.settings.base')
>>> cache
<django.core.cache.DefaultCacheProxy object at 0x101e4c860>
>>> cache.set('my_key', 'hello, world!', 30) #display nothing. No T/F
>>> cache.get('my_key') #Display nothing.
>>> from django.core.cache import caches
>>> caches['default']
<django.core.cache.backends.memcached.MemcachedCache object at 0x1048a5208>
>>> caches['default'].set('my_key', 'hello, world!', 30) #display nothing. No T/F
>>> caches['default'].get('my_key') #Display nothing.

also did pip install python-memcached

Using Python 3.6, Django==1.10.5

What am I doing wrong? Any help will be appreciated.


Solution

  • The problem was, memcached was killed for some reason and I was assumed it was still running. My bad. Now it works all perfectly.

    For anyone who is stuck on a similar problem you want to make sure you are still running memcached, try memcached -vv

    Keeping this here for reference.