diff --git a/cachebox/utils.py b/cachebox/utils.py index 072ff8b..1bc2564 100644 --- a/cachebox/utils.py +++ b/cachebox/utils.py @@ -247,14 +247,17 @@ def _wrapped(*args, **kwds): # try to get result from cache try: result = cache[key] + except KeyError: + pass + else: + # A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback` + # so don't wrap it with try except hits += 1 if callback is not None: callback(EVENT_HIT, key, result) return _copy_if_need(result, level=copy_level) - except KeyError: - pass with locks[key]: if exceptions.get(key, None) is not None: @@ -327,6 +330,11 @@ async def _wrapped(*args, **kwds): # try to get result from cache try: result = cache[key] + except KeyError: + pass + else: + # A NOTE FOR ME: we don't want to catch KeyError exceptions from `callback` + # so don't wrap it with try except hits += 1 if callback is not None: @@ -335,8 +343,6 @@ async def _wrapped(*args, **kwds): await awaitable return _copy_if_need(result, level=copy_level) - except KeyError: - pass async with locks[key]: if exceptions.get(key, None) is not None: