Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Cache cache.set Not storing data

Tags:

When I run python manage.py shell and then:

from django.core.cache import cache
cache.set("stack","overflow",3000)
print cache.get("stack")

(output: ) None

I tried restarting memcache, and here is what's in my settings:

CACHES = { 
  'default' : { 
     'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 
     'LOCATION' : '127.0.0.1:11211',
  }
}
like image 313
Zac Connelly Avatar asked Sep 10 '12 18:09

Zac Connelly


People also ask

How does Django store cache data?

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.

How do I know if Django cache is working?

If cache. get() returns the set value it means that cache is working as it should. Otherwise it will return None . An other option is to start memcached with $ memcached -vv , since it will log all the cache accesses to the terminal.

What is default caching mechanism in Django?

Django has a default caching system in the form of local-memory caching. It is very powerful and robust. This system can handle multi-threaded processes and is efficient. It is best for those projects which cannot use Memcached framework.

Is Django cache thread-safe?

Django relies on the cache backend to be thread-safe, and a single instance of a memcache. Client is not thread-safe. The issue is with Django only creating a single instance that is shared between all threads (django. core.


1 Answers

Make sure it's using the correct cache. Try from django.core.cache import caches, and then see the contents of caches.all(). It should just have one instance of django.core.cache.backends.memcached.MemcachedCache.
If it is, try accessing that directly, e.g.

from django.core.cache import caches  
m_cache = caches.all()[0]
m_cache.set("stack","overflow",3000)
m_cache.get("stack")

This might not solve your problem, but will at least get you closer to debugging Memcached instead of Django's cache proxy or your configuration.

like image 155
Jake Kreider Avatar answered Sep 19 '22 15:09

Jake Kreider