From bfc3f8e0d6748aafd71f8e8155a517308fa2bbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Matja=C5=BE=20Horvat?= Date: Tue, 16 Apr 2024 21:01:43 +0200 Subject: [PATCH] Make sure cache data is refreshed by deleting it before making a request which will populate it again (#3176) --- .../base/management/commands/warmup_cache.py | 33 +++++++++++++++---- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/pontoon/base/management/commands/warmup_cache.py b/pontoon/base/management/commands/warmup_cache.py index f2023a3635..4b7f7a6faa 100644 --- a/pontoon/base/management/commands/warmup_cache.py +++ b/pontoon/base/management/commands/warmup_cache.py @@ -2,6 +2,7 @@ from urllib.parse import urljoin +from django.core.cache import cache from django.core.management.base import BaseCommand from django.urls import reverse @@ -27,8 +28,13 @@ def handle(self, *args, **options): self.warmup_contributors_cache() self.warmup_insights_cache() - def warmup_url(self, url, is_ajax=False): + def warmup_url(self, url, keys=[], is_ajax=False): try: + # Make sure cache data is refreshed by deleting it + # before making a request which will populate it again. + for key in keys: + cache.delete(key) + headers = {"x-requested-with": "XMLHttpRequest"} if is_ajax else None requests.get(url, headers=headers) except requests.exceptions.RequestException as e: @@ -38,7 +44,8 @@ def warmup_contributors_cache(self): self.stdout.write("Warm up Contributors page.") path = reverse("pontoon.contributors") url = urljoin(SITE_URL, path) - self.warmup_url(url) + key = "pontoon.contributors.views.(AND:).None" + self.warmup_url(url, keys=[key]) self.stdout.write("Contributors page warmed up.") self.stdout.write("Warm up Project Contributors tabs.") @@ -47,7 +54,10 @@ def warmup_contributors_cache(self): "pontoon.projects.ajax.contributors", kwargs={"slug": project.slug} ) url = urljoin(SITE_URL, path) - self.warmup_url(url) + key = f"pontoon.contributors.views.(AND:('entity__resource__project',)).None".replace( + " ", "" + ) + self.warmup_url(url, keys=[key]) self.stdout.write("Project Contributors tabs warmed up.") self.stdout.write("Warm up Team Contributors tabs.") @@ -56,7 +66,10 @@ def warmup_contributors_cache(self): "pontoon.teams.ajax.contributors", kwargs={"locale": locale.code} ) url = urljoin(SITE_URL, path) - self.warmup_url(url) + key = f"pontoon.contributors.views.(AND:('locale',)).None".replace( + " ", "" + ) + self.warmup_url(url, keys=[key]) self.stdout.write("Team Contributors tabs warmed up.") # We do not warm up ProjectLocale pages, because there are too many of them and @@ -66,7 +79,11 @@ def warmup_insights_cache(self): self.stdout.write("Warm up Insights page.") path = reverse("pontoon.insights") url = urljoin(SITE_URL, path) - self.warmup_url(url) + keys = [ + f"/pontoon.insights.views/team_pretranslation_quality", + f"/pontoon.insights.views/project_pretranslation_quality", + ] + self.warmup_url(url, keys=keys) self.stdout.write("Insights page warmed up.") self.stdout.write("Warm up Project Insights tabs.") @@ -75,7 +92,8 @@ def warmup_insights_cache(self): "pontoon.projects.ajax.insights", kwargs={"slug": project.slug} ) url = urljoin(SITE_URL, path) - self.warmup_url(url, is_ajax=True) + key = f"/pontoon.projects.views/{project.slug}/insights" + self.warmup_url(url, keys=[key], is_ajax=True) self.stdout.write("Project Insights tabs warmed up.") self.stdout.write("Warm up Team Insights tabs.") @@ -84,7 +102,8 @@ def warmup_insights_cache(self): "pontoon.teams.ajax.insights", kwargs={"locale": locale.code} ) url = urljoin(SITE_URL, path) - self.warmup_url(url, is_ajax=True) + key = f"/pontoon.teams.views/{locale.code}/insights" + self.warmup_url(url, keys=[key], is_ajax=True) self.stdout.write("Team Insights tabs warmed up.") # We do not warm up ProjectLocale pages, because there are too many of them and