Don't cache page bodies
They're still pretty fast to generate, and doing this breaks previews completely
This commit is contained in:
parent
27dc383d17
commit
25e49de081
3 changed files with 1 additions and 62 deletions
|
@ -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}'.")
|
|
|
@ -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])
|
|
|
@ -2,12 +2,10 @@ from datetime import timedelta
|
||||||
from typing import Any, Optional, Type
|
from typing import Any, Optional, Type
|
||||||
|
|
||||||
from django.contrib.syndication.views import Feed
|
from django.contrib.syndication.views import Feed
|
||||||
from django.core.cache import cache
|
|
||||||
from django.core.paginator import EmptyPage
|
from django.core.paginator import EmptyPage
|
||||||
from django.core.paginator import Page as PaginatorPage
|
from django.core.paginator import Page as PaginatorPage
|
||||||
from django.core.paginator import Paginator
|
from django.core.paginator import Paginator
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.dispatch import receiver
|
|
||||||
from django.http.request import HttpRequest
|
from django.http.request import HttpRequest
|
||||||
from django.http.response import Http404, HttpResponse, HttpResponseBadRequest
|
from django.http.response import Http404, HttpResponse, HttpResponseBadRequest
|
||||||
from django.utils.functional import cached_property, classproperty
|
from django.utils.functional import cached_property, classproperty
|
||||||
|
@ -116,21 +114,9 @@ class BaseContentPage(BasePage, MetadataMixin):
|
||||||
def body_html(self) -> str:
|
def body_html(self) -> str:
|
||||||
return add_heading_anchors(self._body_html)
|
return add_heading_anchors(self._body_html)
|
||||||
|
|
||||||
@cached_property
|
|
||||||
def body_html_cache_key(self) -> str:
|
|
||||||
return f"body_html_{self.id}"
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def _body_html(self) -> str:
|
def _body_html(self) -> str:
|
||||||
body_html = cache.get(self.body_html_cache_key)
|
return str(self.body)
|
||||||
|
|
||||||
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
|
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def content_html(self) -> str:
|
def content_html(self) -> str:
|
||||||
|
@ -172,12 +158,6 @@ class BaseContentPage(BasePage, MetadataMixin):
|
||||||
return ""
|
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):
|
class ContentPage(BaseContentPage):
|
||||||
subpage_types: list[Any] = []
|
subpage_types: list[Any] = []
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue