Deleting a key from the cache

Hey, there!

So I just faced the case in which I tried to delete a key from a cache definition which wasn’t actually there (given the way I tested the code, I had been always trying to delete things that had been previously created by me… yes, silly me). And it turns out I got an error instead of… nothing.

The code would be:

# Invalidate the cache
cache_key = 'id:%s' % id_
cache = self.cache.get_cache('builtin', 'logins')

I have checked the docs but I have not found about a potential parameter which would let me instruct the cache API to fail silently, i.e. “it’s okay if you cannot delete it (because it doesn’t exist)”.

Of course I could check whether it’s there before trying to delete it, but it would require two I/O operations.

What do you think, @dsuch? Worth adding an extra parameter there, in case it’s not already there?


I have just changed it to return None instead of raising a KeyError.

Redis does not return an error either in similar situations so it looks like a common expectation. It also makes it work more in line with delete_by_* operations that do not raise KeyError if there are no matching keys.


It’s working fine. Cheers! :+1: