Get list of Cache Keys in Django

I'm trying to understand how Django is setting keys for my views. I'm wondering if there's a way to just get all the saved keys from Memcached. something like a cache.all() or something. I've been trying to find the key with cache.has_key('test') but still cant figure out how the view keys are being named.

UPDATE: The reason I need this is because I need to manually delete parts of the cache but dont know the key values Django is setting for my cache_view key

Answers


You can use http://www.darkcoding.net/software/memcached-list-all-keys/ as explained in How do I check the content of a Django cache with Python memcached?


You can use memcached_stats from: https://github.com/dlrust/python-memcached-stats. This package makes it possible to view the memcached keys from within the python environment.


As mentioned there is no way to get a list of all cache keys within django. If you're using an external cache (e.g. memcached, or database caching) you can inspect the external cache directly.

But if you want to know how to convert a django key to the one used in the backend system, django's make_key() function will do this.

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

>>> from django.core.cache import caches
>>> caches['default'].make_key('test-key')
u':1:test-key'

For RedisCache you can get all available keys with.

from django.core.cache import cache

cache.keys('*')

If this is not too out of date, I have had similar issue, due I have had to iterate over whole cache. I managed it, when I add something to my cache like in following pseudocode:

#create caches key list if not exists
if not my_cache.get("keys"):
    my_cache.set("keys", [])

#add to my cache
my_cache.set(key, value)

#add key to keys
if key not in my_cache.get("keys"):
    keys_list = my_cache.get("keys")
    keys_list.append(key)
    my_cache.set("keys", keys_list)

There are some weird workarounds you can do to get all keys from the command line, but there is no way to do this with memcached inside of Django. See this thread.


Need Your Help

What does 'Acme' stand for in Symfony2?

php symfony

The standard bundle in Symfony2 is 'Acme', but what does that stand for?

How to select the most recent set of dated records from a mysql table

mysql sql date greatest-n-per-group

I am storing the response to various rpc calls in a mysql table with the following fields: