13

I was trying to use the locmem cache for my web application but couldn't find any documentation on how to see the contents of the cache. I mean I want to check if my keys are being set correctly in the cache. How can I list all the keys in this cache or is that even possible?

I found the question "https://stackoverflow.com/questions/9048257/get-list-of-cache-keys-in-django?rq=1" but it's about memcache, not the locmem cache.

Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129
Mayank
  • 357
  • 3
  • 17

2 Answers2

24

The thing about locmem is that it really is just a local memory storage. Looking at the code, it's clear that the data is just being saved in a module-level variable, _caches, in that module. So you can just do

from django.core.cache.backends import locmem
print(locmem._caches)
Boris Verkhovskiy
  • 14,854
  • 11
  • 100
  • 103
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
0

With LocMemCache, 3 sets of code below can get the contents of the cache in Django. *My question explains what results 3 sets of code below can get with LocMemCache and my answer explains how to get the key's values with the keys and versions using LocMemCache:

from django.core.cache import cache

print(cache._cache) # Here
from django.core.cache import cache

print(cache._cache.items()) # Here
from django.core.cache.backends import locmem

print(locmem._caches) # Here
Super Kai - Kazuya Ito
  • 22,221
  • 10
  • 124
  • 129