Only delete key from redis in-memory cache if present (#35126)

Fixes #35120 : the redis cache plugin keeps key/value
entries in an in-memory cache to avoid hitting the
redis database each time.

The problem is that a cache entry is only set when
a value is get or set but it is always deleted when
trying to delete a value.

When the --flush-cache ansible-playbook option is used,
the redis cache plugin is first asked to remove every
entry corresponding to every hostname present in the inventory.
As no value as been set/get so far, it then tries to delete
an unexisting value from the cache and hence crashes with
a KeyError exception.

(cherry picked from commit ee3dfef016)
This commit is contained in:
Yann Rouillard 2018-10-03 23:06:44 +08:00 committed by Toshio Kuratomi
parent b6276fe4a4
commit ac56230d5b
2 changed files with 4 additions and 1 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Only delete host key from redis in-memory cache if present.

View file

@ -116,6 +116,7 @@ class CacheModule(BaseCacheModule):
return (self._db.zrank(self._keys_set, key) is not None)
def delete(self, key):
if key in self._cache:
del self._cache[key]
self._db.delete(self._make_key(key))
self._db.zrem(self._keys_set, key)