Cache title author snippet

This commit is contained in:
Mouse Reeve 2022-01-05 14:33:10 -08:00
parent 4a43ad95ef
commit 0da0091237
3 changed files with 24 additions and 0 deletions

View file

@ -1,6 +1,8 @@
""" database schema for info about authors """
import re
from django.contrib.postgres.indexes import GinIndex
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import models
from bookwyrm import activitypub
@ -34,6 +36,17 @@ class Author(BookDataModel):
)
bio = fields.HtmlField(null=True, blank=True)
def save(self, *args, **kwargs):
"""clear related template caches"""
# clear template caches
cache_keys = [
make_template_fragment_key("titleby", [book])
for book in self.book_set.values_list("id", flat=True)
]
cache.delete_many(cache_keys)
return super().save(*args, **kwargs)
@property
def isni_link(self):
"""generate the url from the isni id"""

View file

@ -3,6 +3,8 @@ import re
from django.contrib.postgres.search import SearchVectorField
from django.contrib.postgres.indexes import GinIndex
from django.core.cache import cache
from django.core.cache.utils import make_template_fragment_key
from django.db import models, transaction
from django.db.models import Prefetch
from django.dispatch import receiver
@ -185,6 +187,11 @@ class Book(BookDataModel):
"""can't be abstract for query reasons, but you shouldn't USE it"""
if not isinstance(self, Edition) and not isinstance(self, Work):
raise ValueError("Books should be added as Editions or Works")
# clear template caches
cache_key = make_template_fragment_key("titleby", [self.id])
cache.delete(cache_key)
return super().save(*args, **kwargs)
def get_remote_id(self):

View file

@ -1,6 +1,8 @@
{% load i18n %}
{% load utilities %}
{% load cache %}
{% spaceless %}
{% cache None titleby book.id %}
{% if book.authors.exists %}
{% blocktrans trimmed with path=book.local_path title=book|book_title %}
@ -10,4 +12,6 @@
{% else %}
<a href="{{ book.local_path }}">{{ book|book_title }}</a>
{% endif %}
{% endcache %}
{% endspaceless %}