Don't cache page bodies

They're still pretty fast to generate, and doing this breaks previews
completely
This commit is contained in:
Jake Howard 2022-08-31 18:54:09 +01:00
parent 27dc383d17
commit 25e49de081
Signed by: jake
GPG key ID: 57AFB45680EDD477
3 changed files with 1 additions and 62 deletions

View file

@ -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}'.")

View file

@ -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])

View file

@ -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] = []