diff --git a/bookwyrm/models/shelf.py b/bookwyrm/models/shelf.py index 3291d5653..d955e8d07 100644 --- a/bookwyrm/models/shelf.py +++ b/bookwyrm/models/shelf.py @@ -103,12 +103,25 @@ class ShelfBook(CollectionItemMixin, BookWyrmModel): if not self.user: self.user = self.shelf.user if self.id and self.user.local: - cache.delete(f"book-on-shelf-{self.book.id}-{self.shelf.id}") + # remove all caches related to all editions of this book + cache.delete_many( + [ + f"book-on-shelf-{book.id}-{self.shelf.id}" + for book in self.book.parent_work.editions.all() + ] + ) super().save(*args, **kwargs) def delete(self, *args, **kwargs): if self.id and self.user.local: - cache.delete(f"book-on-shelf-{self.book.id}-{self.shelf.id}") + cache.delete_many( + [ + f"book-on-shelf-{book}-{self.shelf.id}" + for book in self.book.parent_work.editions.values_list( + "id", flat=True + ) + ] + ) super().delete(*args, **kwargs) class Meta: diff --git a/bookwyrm/templatetags/shelf_tags.py b/bookwyrm/templatetags/shelf_tags.py index 0eb1d8118..1fb799883 100644 --- a/bookwyrm/templatetags/shelf_tags.py +++ b/bookwyrm/templatetags/shelf_tags.py @@ -17,7 +17,7 @@ def get_is_book_on_shelf(book, shelf): lambda b, s: s.books.filter(id=b.id).exists(), book, shelf, - timeout=15552000, + timeout=60 * 60, # just cache this for an hour ) @@ -68,7 +68,7 @@ def active_shelf(context, book): ), user, book, - timeout=15552000, + timeout=60 * 60, ) or {"book": book} @@ -85,5 +85,5 @@ def latest_read_through(book, user): ), user, book, - timeout=15552000, + timeout=60 * 60, ) diff --git a/bookwyrm/views/reading.py b/bookwyrm/views/reading.py index c1e6e5955..eb43e4ea4 100644 --- a/bookwyrm/views/reading.py +++ b/bookwyrm/views/reading.py @@ -52,9 +52,6 @@ class ReadingStatus(View): logger.exception("Invalid reading status type: %s", status) return HttpResponseBadRequest() - # invalidate related caches - cache.delete(f"active_shelf-{request.user.id}-{book_id}") - desired_shelf = get_object_or_404( models.Shelf, identifier=identifier, user=request.user ) @@ -65,6 +62,14 @@ class ReadingStatus(View): .get(id=book_id) ) + # invalidate related caches + cache.delete_many( + [ + f"active_shelf-{request.user.id}-{ed}" + for ed in book.parent_work.editions.values_list("id", flat=True) + ] + ) + # gets the first shelf that indicates a reading status, or None shelves = [ s