From 2fe0960623a1705f6c6254798182db15376625ff Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Sat, 20 Aug 2022 18:51:01 +0100 Subject: [PATCH] Add management command to clear cache --- .../common/management/commands/clear_cache.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 website/common/management/commands/clear_cache.py diff --git a/website/common/management/commands/clear_cache.py b/website/common/management/commands/clear_cache.py new file mode 100644 index 0000000..ed51df9 --- /dev/null +++ b/website/common/management/commands/clear_cache.py @@ -0,0 +1,20 @@ +from argparse import ArgumentParser + +from django.core.cache import DEFAULT_CACHE_ALIAS, caches +from django.core.management.base import BaseCommand + + +class Command(BaseCommand): + def add_arguments(self, parser: ArgumentParser) -> None: + parser.add_argument( + "cache", + type=str, + default=DEFAULT_CACHE_ALIAS, + choices=sorted(list(caches)), + help="Cache to clear", + ) + + def handle(self, *args: list, **options: dict) -> None: + cache_name: str = options["cache"] # type: ignore + caches[cache_name].clear() + self.stdout.write(f"Cleared cache '{cache_name}'.")