Merge pull request #4160 from alphagov/redis-clear-all

Make it easy to clear cache for all key formats
This commit is contained in:
Ben Thorner
2022-02-21 15:18:32 +00:00
committed by GitHub
3 changed files with 44 additions and 28 deletions

View File

@@ -2445,10 +2445,14 @@ class TemplateAndFoldersSelectionForm(Form):
class ClearCacheForm(StripWhitespaceForm):
model_type = GovukRadiosField(
model_type = GovukCheckboxesField(
'What do you want to clear today',
)
def validate_model_type(self, field):
if not field.data:
raise ValidationError('Select at least one option')
class GoLiveNotesForm(StripWhitespaceForm):
request_to_go_live_notes = TextAreaField(

View File

@@ -420,17 +420,28 @@ 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():
to_delete = form.model_type.data
group_keys = form.model_type.data
groups = map(CACHE_KEYS.get, group_keys)
patterns = list(itertools.chain(*groups))
num_deleted = max(
num_deleted = sum(
redis_client.delete_cache_keys_by_pattern(pattern)
for pattern in CACHE_KEYS[to_delete]
for pattern in patterns
)
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} objects '
f'across {len(patterns)} key formats '
f'for {", ".join(group_keys)}'
)
flash(msg, category='default')
return render_template(
'views/platform-admin/clear-cache.html',