diff --git a/main/management/commands/clear_cache.py b/main/management/commands/clear_cache.py new file mode 100644 index 0000000000..a0416308b4 --- /dev/null +++ b/main/management/commands/clear_cache.py @@ -0,0 +1,15 @@ +"""Command to clear the cache""" + +from django.core.management.base import BaseCommand + +from main.utils import clear_search_cache + + +class Command(BaseCommand): + """Command to clear the cache""" + + help = "Command to clear the cache" + + def handle(self, *args, **options): # noqa: ARG002 + cache_items = clear_search_cache() + self.stdout.write(f"cleared {cache_items} items from cache") diff --git a/main/utils.py b/main/utils.py index d227c63f93..7c68eae647 100644 --- a/main/utils.py +++ b/main/utils.py @@ -357,8 +357,10 @@ def clean_data(data: str) -> str: def clear_search_cache(): cache = caches["redis"] + cleared = 0 if hasattr(cache, "keys"): search_keys = cache.keys("views.decorators.cache.cache_page.search.*") - cache.delete_many(search_keys) + cleared += cache.delete_many(search_keys) or 0 search_keys = cache.keys("views.decorators.cache.cache_header.search.*") - cache.delete_many(search_keys) + cleared += cache.delete_many(search_keys) or 0 + return cleared diff --git a/scripts/heroku-release-phase.sh b/scripts/heroku-release-phase.sh index 28e02c1caa..ba07b4711a 100755 --- a/scripts/heroku-release-phase.sh +++ b/scripts/heroku-release-phase.sh @@ -22,3 +22,6 @@ python $MANAGE_FILE prune_subscription_queries 2>&1 | indent echo "-----> Generating cache tables" python $MANAGE_FILE createcachetable 2>&1 | indent + +# clear cache entries +python $MANAGE_FILE clear_cache --noinput 2>&1 | indent