From e841b2868beaba7aa9954127a27060090f3ee670 Mon Sep 17 00:00:00 2001 From: "Sofian A. Thibaut" Date: Tue, 17 Sep 2024 13:15:15 +0200 Subject: [PATCH] Distinct cache for fragment and full template --- utils/views_mixins.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/utils/views_mixins.py b/utils/views_mixins.py index 6fa6bf6e6..21200e112 100644 --- a/utils/views_mixins.py +++ b/utils/views_mixins.py @@ -55,13 +55,21 @@ class CacheMixin: cache_timeout = 60 * 15 # cache pour 15 minutes def should_cache(self, *args, **kwargs): - """Override to disable cache conditionnally""" + """Override to disable cache conditionally""" return True - def prefixer(request): + def prefixer(self, request): if request.method != "GET" or request.GET.get("no-cache"): return None - return request.get_full_path() + # Distinction pour les requêtes AJAX + if request.headers.get("X-Requested-With") == "XMLHttpRequest": + return f"partial_{request.get_full_path()}" + return f"full_{request.get_full_path()}" + + def cached_dispatch(self, request, *args, **kwargs): + # Utiliser la méthode de classe pour le préfixe + key_prefix = self.prefixer(request) + return cache_page(self.cache_timeout, key_prefix=key_prefix)(super().dispatch)(request, *args, **kwargs) @method_decorator( cache_control( @@ -71,10 +79,6 @@ def prefixer(request): must_revalidate=True, ) ) - @method_decorator(cache_page(cache_timeout, key_prefix=prefixer)) - def cached_dispatch(self, request, *args, **kwargs): - return super().dispatch(request, *args, **kwargs) - def dispatch(self, request, *args, **kwargs): if self.should_cache(): return self.cached_dispatch(request, *args, **kwargs)