Skip to content

Commit

Permalink
Distinct cache for fragment and full template
Browse files Browse the repository at this point in the history
  • Loading branch information
smdsgn committed Sep 17, 2024
1 parent b191eca commit e841b28
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions utils/views_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit e841b28

Please sign in to comment.