21

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',
  }
}
12
  • 4
    Do you have one of python-memcached or pylibmc installed? Commented Sep 10, 2012 at 19:59
  • 1
    also you should try running memcached with the -vv flag to get "very verbose" output to see if there any issues Commented Sep 11, 2012 at 15:34
  • 6
    This is a silly question, but are you doing cache.get('stack') quickly enough? Try just cache.set('stack', 'overflow') and then using cache.get('stack'). Commented Nov 4, 2014 at 20:11
  • 8
    Are you sure your memcached is up? Did you try to set and get a cache key using telnet? Commented Jan 5, 2015 at 16:12
  • 1
    I would recommend installing django-debug-toolbar. Once it is installed and added to your DJANGO_APPS. you will see a sidebar on your page. Open the sidebar and check the cache option to see if the cache has really been set. Commented Apr 30, 2015 at 6:54

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

0

I believe django augments the key with a version. For example,

django_memcache.set('my_key', 'django', 1000)

will set the key :1:my_key in memcache:

<36 set :1:my_key 0 1000 6
>36 STORED

However, if you set the key through telnet or python-memcached module, it will store the raw key as expected:

<38 set my_key 0 1000 13 
>38 STORED

So, perhaps you are not querying the correct key?

See https://docs.djangoproject.com/en/1.10/topics/cache/#cache-key-transformation

Comments

-1

With set(), set_many(), get(), get_many() and get_or_set(), you can set and get cache values with LocMemCache which is used by default as shown below. *set() can set a timeout and a version and set_many() can set multiple cache values with a timeout and get() can get the key's cache value matched with the version and get a default value if the key doesn't exist and get_many() can get multiple cache values and get_or_set() can get a key's cache value if the key exists or set and get a key's cache value if the key doesn't exist and my answer explains how to delete cache values with LocMemCache and the answer of my question explains the default version of a cache value with LocMemCache:

from django.http import HttpResponse
from django.core.cache import cache

def test(request):
    cache.set("first_name", "John")
    cache.set("first_name", "David", 30, 2)
    cache.set("last_name", "Smith")
    cache.set("last_name", "Miller", version=2)
    cache.set_many({"age": 36, "gender": "Male"}, 50)

    print(cache.get("first_name")) # John
    print(cache.get("first_name", version=2)) # David
    print(cache.get("first_name", "Doesn't exist", version=3)) # Doesn't exist
    print(cache.get_many(["first_name", "last_name", "age", "gender"]))
    # {'first_name': 'John', 'last_name': 'Smith', 'age': 36, 'gender': 'Male'}
    print(cache.get_many(["first_name", "last_name", "age", "gender"], 2))
    # {'first_name': 'David', 'last_name': 'Miller'}
    print(cache.get_or_set("first_name", "Doesn't exist")) # John
    print(cache.get_or_set("email", "Doesn't exist")) # Doesn't exist
    return HttpResponse("Test")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.