From fa85c8ba80a92e2d0a9210865dcb9b5655ae9b5a Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Tue, 4 Oct 2022 20:14:26 +0100 Subject: [PATCH] Cache fragments for longer, and clear when editing page --- .../templates/common/content-details.html | 2 +- .../common/templates/common/listing-item.html | 2 +- website/common/wagtail_hooks.py | 23 +++++++++++++++++++ website/utils/context_processors.py | 9 +++++++- 4 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 website/common/wagtail_hooks.py diff --git a/website/common/templates/common/content-details.html b/website/common/templates/common/content-details.html index 114c25c..b326719 100644 --- a/website/common/templates/common/content-details.html +++ b/website/common/templates/common/content-details.html @@ -1,6 +1,6 @@ {% load wagtailcore_tags humanize_tags cache util_tags %} -{% cache 600|jitter:60 "content-details" page.id request.is_preview %} +{% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "content-details" page.id request.is_preview %}
{% if page.date %} diff --git a/website/common/templates/common/listing-item.html b/website/common/templates/common/listing-item.html index c986dff..df0c2fa 100644 --- a/website/common/templates/common/listing-item.html +++ b/website/common/templates/common/listing-item.html @@ -13,7 +13,7 @@

{{ page.title }}

{% include "common/content-details.html" %} - {% cache 900|jitter:60 "summary" page.id request.is_preview %} + {% cache FRAGMENT_CACHE_TTL|jitter:FRAGMENT_CACHE_TTL_JITTER "summary" page.id request.is_preview %}

{{ page.summary }}

{% endcache %}
diff --git a/website/common/wagtail_hooks.py b/website/common/wagtail_hooks.py new file mode 100644 index 0000000..30df29e --- /dev/null +++ b/website/common/wagtail_hooks.py @@ -0,0 +1,23 @@ +from django.core.cache import cache +from django.core.cache.utils import make_template_fragment_key +from django.http.request import HttpRequest +from wagtail import hooks +from wagtail.models import Page + +from website.common.models import BasePage + +FRAGMENT_CACHES = {"summary", "content-details"} + + +@hooks.register("after_edit_page") +def clear_fragment_cache(request: HttpRequest, page: Page) -> None: + if not isinstance(page, BasePage): + return + + cache.delete_many( + [ + # Empty string is for the empty value of `request.is_preview` + make_template_fragment_key(cache_name, [page.id, False]) + for cache_name in FRAGMENT_CACHES + ] + ) diff --git a/website/utils/context_processors.py b/website/utils/context_processors.py index fc7fa77..6f6099e 100644 --- a/website/utils/context_processors.py +++ b/website/utils/context_processors.py @@ -3,4 +3,11 @@ from django.http.request import HttpRequest def global_vars(request: HttpRequest) -> dict: - return {"SEO_INDEX": settings.SEO_INDEX, "DEBUG": settings.DEBUG} + # noop caching in preview + fragment_cache_ttl = 0 if getattr(request, "is_preview", False) else 3600 + return { + "SEO_INDEX": settings.SEO_INDEX, + "DEBUG": settings.DEBUG, + "FRAGMENT_CACHE_TTL": fragment_cache_ttl, + "FRAGMENT_CACHE_TTL_JITTER": fragment_cache_ttl * 0.1, + }