Cache query for author's books
This commit is contained in:
@ -342,6 +342,11 @@ class Edition(Book):
|
|||||||
# set rank
|
# set rank
|
||||||
self.edition_rank = self.get_rank()
|
self.edition_rank = self.get_rank()
|
||||||
|
|
||||||
|
# clear author cache
|
||||||
|
if self.id:
|
||||||
|
for author_id in self.authors.values_list("id", flat=True):
|
||||||
|
cache.delete(f"author-books-{author_id}")
|
||||||
|
|
||||||
return super().save(*args, **kwargs)
|
return super().save(*args, **kwargs)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
|
@ -12,6 +12,7 @@ from bookwyrm import forms, models
|
|||||||
from bookwyrm.activitypub import ActivitypubResponse
|
from bookwyrm.activitypub import ActivitypubResponse
|
||||||
from bookwyrm.connectors import connector_manager
|
from bookwyrm.connectors import connector_manager
|
||||||
from bookwyrm.settings import PAGE_LENGTH
|
from bookwyrm.settings import PAGE_LENGTH
|
||||||
|
from bookwyrm.utils import cache
|
||||||
from bookwyrm.views.helpers import is_api_request
|
from bookwyrm.views.helpers import is_api_request
|
||||||
|
|
||||||
|
|
||||||
@ -30,14 +31,24 @@ class Author(View):
|
|||||||
parent_work=OuterRef("parent_work")
|
parent_work=OuterRef("parent_work")
|
||||||
).order_by("-edition_rank")
|
).order_by("-edition_rank")
|
||||||
|
|
||||||
books = (
|
book_ids = cache.get_or_set(
|
||||||
models.Edition.viewer_aware_objects(request.user)
|
f"author-books-{author.id}",
|
||||||
.filter(Q(authors=author) | Q(parent_work__authors=author))
|
lambda a: models.Edition.objects.filter(
|
||||||
|
Q(authors=a) | Q(parent_work__authors=a)
|
||||||
|
)
|
||||||
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
|
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
|
||||||
.filter(default_id=F("id"))
|
.filter(default_id=F("id"))
|
||||||
.order_by("-first_published_date", "-published_date", "-created_date")
|
.distinct()
|
||||||
|
.values_list("id", flat=True),
|
||||||
|
author,
|
||||||
|
timeout=15552000,
|
||||||
|
)
|
||||||
|
|
||||||
|
books = (
|
||||||
|
models.Edition.objects.filter(id__in=book_ids)
|
||||||
|
.order_by("-published_date", "-first_published_date", "-created_date")
|
||||||
.prefetch_related("authors")
|
.prefetch_related("authors")
|
||||||
).distinct()
|
)
|
||||||
|
|
||||||
paginated = Paginator(books, PAGE_LENGTH)
|
paginated = Paginator(books, PAGE_LENGTH)
|
||||||
page = paginated.get_page(request.GET.get("page"))
|
page = paginated.get_page(request.GET.get("page"))
|
||||||
|
Reference in New Issue
Block a user