20

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',
  }
}
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Zac Connelly
  • 321
  • 1
  • 2
  • 5
  • 4
    Do you have one of python-memcached or pylibmc installed? – James R Sep 10 '12 at 19:59
  • Yeah, I have python-memcached installed – Zac Connelly Sep 11 '12 at 14:45
  • Have you verified that the port 11211 is open? – James R Sep 11 '12 at 15:32
  • 1
    also you should try running memcached with the -vv flag to get "very verbose" output to see if there any issues – James R Sep 11 '12 at 15:34
  • Do you have other cache specific settings in settings.py? – Sergey Lyapustin Sep 08 '14 at 13:41
  • Isn't that you are serving it on a virtualenv? – Chemical Programmer Sep 22 '14 at 01:05
  • 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'). – johndavidback Nov 04 '14 at 20:11
  • 8
    Are you sure your memcached is up? Did you try to set and get a cache key [using telnet](http://www.lzone.de/cheat-sheet/memcached)? – Renan Ivo Jan 05 '15 at 16:12
  • 1
    I would recommend installing [django-debug-toolbar](https://github.com/django-debug-toolbar/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. – Cheng Apr 30 '15 at 06:54
  • try to use a `'django.core.cache.backends.locmem.LocMemCache` as default cache then if the code works try to change the cache backend with memcache. – Karim N Gorjux Jun 10 '15 at 18:39
  • Worked for me in a virtualenv with a brand new memcached system install. The only settings I am using beside what you listed are CACHE_MIDDLEWARE_ALIAS = "default", CACHE_MIDDLEWARE_SECONDS = 7200, CACHE_MIDDLEWARE_KEY_PREFIX = "". – Sectio Aurea Jun 14 '15 at 17:09
  • Do you set DEBUG = True ? (Maybe you have this in your settings.py and memcached is looking for) – Borish Aug 11 '17 at 07:03

3 Answers3

3

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.

Jake Kreider
  • 950
  • 7
  • 12
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

quickinsights
  • 1,067
  • 12
  • 18
0

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")
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129