Cache page content for a little
This is mostly useful on listing pages
This commit is contained in:
parent
d46fab9bdc
commit
e6c44e9a8a
1 changed files with 21 additions and 1 deletions
|
@ -1,7 +1,9 @@
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from typing import Any, Optional
|
from typing import Any, Optional
|
||||||
|
|
||||||
|
from django.core.cache import cache
|
||||||
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.utils.functional import cached_property, classproperty
|
from django.utils.functional import cached_property, classproperty
|
||||||
from django.utils.text import slugify
|
from django.utils.text import slugify
|
||||||
|
@ -93,9 +95,21 @@ 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:
|
||||||
return str(self.body)
|
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
|
||||||
|
|
||||||
@cached_property
|
@cached_property
|
||||||
def content_html(self) -> str:
|
def content_html(self) -> str:
|
||||||
|
@ -137,6 +151,12 @@ 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