From 25e49de08100a2119d879618073d6adb381fe790 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 31 Aug 2022 18:54:09 +0100 Subject: [PATCH] Don't cache page bodies They're still pretty fast to generate, and doing this breaks previews completely --- .../common/management/commands/clear_cache.py | 20 ----------------- .../commands/refresh_body_html_cache.py | 21 ------------------ website/common/models.py | 22 +------------------ 3 files changed, 1 insertion(+), 62 deletions(-) delete mode 100644 website/common/management/commands/clear_cache.py delete mode 100644 website/common/management/commands/refresh_body_html_cache.py diff --git a/website/common/management/commands/clear_cache.py b/website/common/management/commands/clear_cache.py deleted file mode 100644 index ed51df9..0000000 --- a/website/common/management/commands/clear_cache.py +++ /dev/null @@ -1,20 +0,0 @@ -from argparse import ArgumentParser - -from django.core.cache import DEFAULT_CACHE_ALIAS, caches -from django.core.management.base import BaseCommand - - -class Command(BaseCommand): - def add_arguments(self, parser: ArgumentParser) -> None: - parser.add_argument( - "cache", - type=str, - default=DEFAULT_CACHE_ALIAS, - choices=sorted(list(caches)), - help="Cache to clear", - ) - - def handle(self, *args: list, **options: dict) -> None: - cache_name: str = options["cache"] # type: ignore - caches[cache_name].clear() - self.stdout.write(f"Cleared cache '{cache_name}'.") diff --git a/website/common/management/commands/refresh_body_html_cache.py b/website/common/management/commands/refresh_body_html_cache.py deleted file mode 100644 index 8c08122..0000000 --- a/website/common/management/commands/refresh_body_html_cache.py +++ /dev/null @@ -1,21 +0,0 @@ -from django.core.cache import cache -from django.core.management.base import BaseCommand -from wagtail.models import Page - -from website.common.models import BaseContentPage -from website.utils.queue import enqueue_or_sync - - -def refresh_cache(page_id: int) -> None: - page = Page.objects.get(id=page_id).specific - cache.delete(page.body_html_cache_key) - page._body_html # Prime cache - - -class Command(BaseCommand): - def handle(self, *args: list, **options: dict) -> None: - for page in Page.objects.all().specific().only("id", "title").iterator(): - if not isinstance(page, BaseContentPage): - continue - - enqueue_or_sync(refresh_cache, args=[page.id]) diff --git a/website/common/models.py b/website/common/models.py index 3dfc5b4..00a0b5d 100644 --- a/website/common/models.py +++ b/website/common/models.py @@ -2,12 +2,10 @@ from datetime import timedelta from typing import Any, Optional, Type from django.contrib.syndication.views import Feed -from django.core.cache import cache from django.core.paginator import EmptyPage from django.core.paginator import Page as PaginatorPage from django.core.paginator import Paginator from django.db import models -from django.dispatch import receiver from django.http.request import HttpRequest from django.http.response import Http404, HttpResponse, HttpResponseBadRequest from django.utils.functional import cached_property, classproperty @@ -116,21 +114,9 @@ class BaseContentPage(BasePage, MetadataMixin): def body_html(self) -> str: return add_heading_anchors(self._body_html) - @cached_property - def body_html_cache_key(self) -> str: - return f"body_html_{self.id}" - @cached_property def _body_html(self) -> str: - body_html = cache.get(self.body_html_cache_key) - - if body_html is None: - body_html = str(self.body) - - # Cache for 1 day - cache.set(self.body_html_cache_key, body_html, 86400) - - return body_html + return str(self.body) @cached_property def content_html(self) -> str: @@ -172,12 +158,6 @@ class BaseContentPage(BasePage, MetadataMixin): return "" -@receiver(models.signals.post_save) -def clear_body_html_cache(sender: Any, instance: models.Model, **kwargs: dict) -> None: - if isinstance(instance, BaseContentPage): - cache.delete(instance.body_html_cache_key) - - class ContentPage(BaseContentPage): subpage_types: list[Any] = []