Make it easy to clear cache for all key formats

Having to submit the form for each choice separately slowed us down
during an incident where Redis was unavailable and came back with
stale data, which we had to clear manually.

Note: we don't want to use the "flush" feature in case there are other
keys in Redis, which may not be safe to remove.
This commit is contained in:
Ben Thorner
2022-02-21 11:28:15 +00:00
parent 16a14cd642
commit ebbfd20472
3 changed files with 25 additions and 14 deletions

View File

@@ -420,18 +420,27 @@ def clear_cache():
])
form = ClearCacheForm()
form.model_type.choices = [(key, key.replace('_', ' ').title()) for key in CACHE_KEYS]
form.model_type.choices = [
(key, key.replace('_', ' ').title()) for key in CACHE_KEYS
]
if form.validate_on_submit():
group_key = form.model_type.data
group = CACHE_KEYS[group_key]
group_keys = form.model_type.data
groups = map(CACHE_KEYS.get, group_keys)
patterns = list(itertools.chain(*groups))
num_deleted = sum(
redis_client.delete_cache_keys_by_pattern(pattern)
for pattern in group
for pattern in patterns
)
msg = (
f'Removed {num_deleted} objects '
f'across {len(patterns)} key formats '
f'for {", ".join(group_keys)}'
)
msg = f'Removed {num_deleted} {group_key} objects across {len(group)} key formats'
flash(msg, category='default')
return render_template(