From ccef242efb296b3eaa4720d2311b695c39a651ee Mon Sep 17 00:00:00 2001 From: shankar ambady Date: Wed, 9 Oct 2024 09:42:48 -0400 Subject: [PATCH 1/3] adding initial cache clear command --- main/management/commands/clear_cache.py | 15 +++++++++++++++ main/utils.py | 6 ++++-- 2 files changed, 19 insertions(+), 2 deletions(-) create mode 100644 main/management/commands/clear_cache.py diff --git a/main/management/commands/clear_cache.py b/main/management/commands/clear_cache.py new file mode 100644 index 0000000000..ac5085d8d3 --- /dev/null +++ b/main/management/commands/clear_cache.py @@ -0,0 +1,15 @@ +"""Command to generate the channel avatar SVGs""" + +from django.core.management.base import BaseCommand + +from main.utils import clear_search_cache + + +class Command(BaseCommand): + """Command to generate the channel avatar SVGs""" + + 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 From 744498ca64690c77c2211f299b03655ee415bfcd Mon Sep 17 00:00:00 2001 From: shankar ambady Date: Wed, 9 Oct 2024 09:53:23 -0400 Subject: [PATCH 2/3] adding cache clear command to heroku release script --- scripts/heroku-release-phase.sh | 3 +++ 1 file changed, 3 insertions(+) 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 From f716e4fdd1cadb6d3d8e6a2c3d87223727742b42 Mon Sep 17 00:00:00 2001 From: shankar ambady Date: Wed, 9 Oct 2024 10:00:18 -0400 Subject: [PATCH 3/3] fix docstrings --- main/management/commands/clear_cache.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main/management/commands/clear_cache.py b/main/management/commands/clear_cache.py index ac5085d8d3..a0416308b4 100644 --- a/main/management/commands/clear_cache.py +++ b/main/management/commands/clear_cache.py @@ -1,4 +1,4 @@ -"""Command to generate the channel avatar SVGs""" +"""Command to clear the cache""" from django.core.management.base import BaseCommand @@ -6,7 +6,7 @@ class Command(BaseCommand): - """Command to generate the channel avatar SVGs""" + """Command to clear the cache""" help = "Command to clear the cache"