I need to iterate through my server's cache, which is a LocMemCache
object, and remove every key in the cache that begins with the string 'rl:'
. From what I understand, the only functions that the caching API django provides are get, set, and delete. Here is a rough example of what I am trying to do:
def clear_ratelimit_cache():
if any('rl:' in s for s in cache.get(s)):
log.info(
'FOUND SOMETHING')
cache.delete(*something else here*)
Trying to do this, however, gives me a NameError
, stating that global name 's' is not defined
. Also it must be noted that the cache is not iterable. Has anyone worked with the cache in a similar manner, and has a suggestion?
One option would be to have a separate, named cache in your configuration just for this data type, and then call its
clear()
method.Otherwise, Django
LocMemCache
stores items in a simpledict
, in the instance's_cache
attribute. Since they don't give you an API for this, you can simply remove the items directly:Usual disclaimer, this is an implementation detail that won't work with other cache types.