Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 984
suprita shankar Avatar asked May 09 '17 22:05

suprita shankar


People also ask

How do you cache in Django?

To use cache in Django, first thing to do is to set up where the cache will stay. The cache framework offers different possibilities - cache can be saved in database, on file system or directly in memory. Setting is done in the settings.py file of your project.

Does Django have caching?

Django comes with a robust cache system that lets you save dynamic pages so they don't have to be calculated for each request.


1 Answers

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.

like image 151
suprita shankar Avatar answered Oct 16 '22 15:10

suprita shankar