Thierry Lam
Thierry Lam

Reputation: 46294

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

Tools version:

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?

Upvotes: 16

Views: 23380

Answers (3)

Omar Al-Ithawi
Omar Al-Ithawi

Reputation: 5170

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.

Upvotes: 3

Philip Clarke
Philip Clarke

Reputation: 735

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.

Upvotes: 5

tback
tback

Reputation: 11571

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

Upvotes: 9

Related Questions