Do not throw exception on externally invalidated cache keys (#169)

Closes #120
This commit is contained in:
Dominik George 2021-03-22 00:27:16 +01:00 committed by GitHub
parent d0b5213014
commit ac814e5cd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 11 deletions

View file

@ -1,6 +1,11 @@
Whats new in django-cachalot?
==============================
2.3.4
-----
- Fix bug with externally invalidated cache keys (#120)
2.3.3
-----

View file

@ -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):