diff --git a/CHANGELOG.rst b/CHANGELOG.rst index a7a0f97..8d28af0 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -1,6 +1,11 @@ What’s new in django-cachalot? ============================== +2.3.4 +----- + +- Fix bug with externally invalidated cache keys (#120) + 2.3.3 ----- diff --git a/cachalot/monkey_patch.py b/cachalot/monkey_patch.py index b77916f..c822877 100644 --- a/cachalot/monkey_patch.py +++ b/cachalot/monkey_patch.py @@ -34,20 +34,24 @@ def _unset_raw_connection(original): def _get_result_or_execute_query(execute_query_func, cache, cache_key, table_cache_keys): - data = cache.get_many(table_cache_keys + [cache_key]) + try: + data = cache.get_many(table_cache_keys + [cache_key]) + except KeyError: + data = None new_table_cache_keys = set(table_cache_keys) - new_table_cache_keys.difference_update(data) + if data: + new_table_cache_keys.difference_update(data) - if not new_table_cache_keys: - try: - timestamp, result = data.pop(cache_key) - if timestamp >= max(data.values()): - return result - except (KeyError, TypeError, ValueError): - # In case `cache_key` is not in `data` or contains bad data, - # we simply run the query and cache again the results. - pass + if not new_table_cache_keys: + try: + timestamp, result = data.pop(cache_key) + if timestamp >= max(data.values()): + return result + except (KeyError, TypeError, ValueError): + # In case `cache_key` is not in `data` or contains bad data, + # we simply run the query and cache again the results. + pass result = execute_query_func() if result.__class__ not in ITERABLES and isinstance(result, Iterable):