Report all cache keys that were deleted

This will make it easier to add another test / feature to clear all
the cache keys. It's debatable which of "sum" and "max" is useful:

- "max" is a better (although still not accurate) indicator of the
number of "things" affected e.g. templates, services, etc.

- "sum" makes sense in places where "max" doesn't e.g. when we clear
the "organisations" group, which doesn't equate to individual orgs.

Using "sum() ... across" seems like a reasonable compromise and makes
it clear that we're iterating over different kinds of keys.

While the pluralisation is nice, I don't think it's worth the effort
to make it work for both "object(s)" and "format(s)".
This commit is contained in:
Ben Thorner
2022-02-21 10:37:51 +00:00
parent 73cc034676
commit 8396412ce1
2 changed files with 17 additions and 17 deletions

View File

@@ -423,14 +423,16 @@ def clear_cache():
form.model_type.choices = [(key, key.replace('_', ' ').title()) for key in CACHE_KEYS]
if form.validate_on_submit():
to_delete = form.model_type.data
group_key = form.model_type.data
group = CACHE_KEYS[group_key]
num_deleted = max(
num_deleted = sum(
redis_client.delete_cache_keys_by_pattern(pattern)
for pattern in CACHE_KEYS[to_delete]
for pattern in group
)
msg = 'Removed {} {} object{} from redis'
flash(msg.format(num_deleted, to_delete, 's' if num_deleted != 1 else ''), category='default')
msg = f'Removed {num_deleted} {group_key} objects across {len(group)} key formats'
flash(msg, category='default')
return render_template(
'views/platform-admin/clear-cache.html',