How do I check the content of a Django cache with Python memcached?

Question:

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?

Asked By: Thierry Lam

||

Answers:

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

Answered By: tback

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.

Answered By: Philip Clarke

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.

Answered By: Omar Al-Ithawi
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.