Merge pull request #1837 from bookwyrm-social/author-page-caching

Author page caching
This commit is contained in:
Mouse Reeve
2022-01-18 11:02:49 -08:00
committed by GitHub
9 changed files with 75 additions and 18 deletions

View File

@ -12,6 +12,7 @@ from bookwyrm import forms, models
from bookwyrm.activitypub import ActivitypubResponse
from bookwyrm.connectors import connector_manager
from bookwyrm.settings import PAGE_LENGTH
from bookwyrm.utils import cache
from bookwyrm.views.helpers import is_api_request
@ -30,14 +31,24 @@ class Author(View):
parent_work=OuterRef("parent_work")
).order_by("-edition_rank")
books = (
models.Edition.viewer_aware_objects(request.user)
.filter(Q(authors=author) | Q(parent_work__authors=author))
book_ids = cache.get_or_set(
f"author-books-{author.id}",
lambda a: models.Edition.objects.filter(
Q(authors=a) | Q(parent_work__authors=a)
)
.annotate(default_id=Subquery(default_editions.values("id")[:1]))
.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")
).distinct()
)
paginated = Paginator(books, PAGE_LENGTH)
page = paginated.get_page(request.GET.get("page"))

View File

@ -46,9 +46,7 @@ class ReadingStatus(View):
return HttpResponseBadRequest()
# invalidate related caches
cache.delete(
f"active_shelf-{request.user.id}-{book_id}",
)
cache.delete(f"active_shelf-{request.user.id}-{book_id}")
desired_shelf = get_object_or_404(
models.Shelf, identifier=identifier, user=request.user