16

Tools version:

  • Python 2.6.5
  • Django 1.3.1
  • memcached 1.4.10
  • python-memcached 1.48

Memcached is currently running:

$ ps -ef | grep memcache
nobody    2993     1  0 16:46 ?        00:00:00 /usr/bin/memcached -m 64 -p 11211 -u nobody -l 127.0.0.1

I'm using memcached and python memcached with my Django proj and I've set it like the following in settings.py:

CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:11211',
        'TIMEOUT': 86400,
    },
}

I've set the cache in the code:

from django.core.cache import cache
cache.set('countries', ['Canada', 'US'])

I then open a Django shell to inspect the content of the cache:

>>> from django.core.cache import cache
>>> 'countries' in cache
True
>>> import memcache
>>> mc = memcache.Client(['127.0.0.1:11211'], debug=1)
>>> mc.get('countries')
>>> 

When I use Django's cache, countries key exists. However, when I use Python's memcache, I don't get anything for countries. What am I doing wrong above?

Thierry Lam
  • 45,304
  • 42
  • 117
  • 144

3 Answers3

9

Django prefixes cache keys with a colon. You can inspect memcached like so if this doesn't help.

tback
  • 11,138
  • 7
  • 47
  • 71
  • For reference django no longer prefixes its cache keys with a colon by default. You can however still make this happen by using the KEY_PREFIX configuration option described here. http://bit.ly/1lzFFPi – Jeff Sheffield Feb 05 '14 at 05:42
  • Why would you shorten the [Django Cache Key Prefixing](https://docs.djangoproject.com/en/dev/topics/cache/#cache-key-prefixing) URL? – tback Feb 05 '14 at 07:41
  • 1
    I did it because I was thinking I was going to run out of comment space. ( I wont do it in the future ) – Jeff Sheffield Feb 05 '14 at 14:23
5

You can use memcached_stats from: https://github.com/dlrust/python-memcached-stats

Example: (I used pylibmc for the cache, but I think this should be the same is you use python-memcached)

import pylibmc

from memcached_stats import MemcachedStats
mem = MemcachedStats() # connecting to localhost at default memcached port

# print out all your keys
mem.keys()

# say for example key[0] is 'countries', then to get the value just do
key = mem.keys()[0]

import memcache
mc = memcache.Client(['127.0.0.1:11211'], debug=1)
value = mc.get (key)

There is also a command line interface to memcaced_stats: python -m memcached_stats

Have a look at the github repo as the README is very clear.

Omar Al-Ithawi
  • 4,988
  • 5
  • 36
  • 47
Philip Clarke
  • 727
  • 6
  • 13
  • there's a bug in your code where you do mc.get() .. what is mc...? I assume its from pylibmc. +1-nd tho. the memcached-stats lib is awesome. – Jeff Sheffield Feb 05 '14 at 06:01
  • see docs http://sendapatch.se/projects/pylibmc/ ...typical for client instantiation...mc = pylibmc.Client(["127.0.0.1"], binary=True,behaviors={"tcp_nodelay": True,"ketama": True}) – jpwagner Dec 29 '14 at 21:18
3

The following script dumps all the keys of a memcached server. It's tested with Ubuntu 12.04 and a localhost memcached, so your milage may vary.

#!/usr/bin/env bash

echo 'stats items'  \
| nc localhost 11211  \
| grep -oe ':[0-9]*:'  \
| grep -oe '[0-9]*'  \
| sort  \
| uniq  \
| xargs -L1 -I{} bash -c 'echo "stats cachedump {} 1000" | nc localhost 11211'

What it does, it goes through all the cache slabs and print 1000 keys of each.

Omar Al-Ithawi
  • 4,988
  • 5
  • 36
  • 47
  • It does not dump all the content of a memcached server. It just shows the existing keys. It's tested with Ubuntu 16.04. – F.Tamy Jul 30 '19 at 09:52