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',
}
}
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With