AO3-4495 Cache the bookmark counts #2459
+27
−2
zz9pzza
commented
May 14, 2016
•
lib/bookmark_count_caching.rb
| @@ -0,0 +1,15 @@ | ||
| +module BookmarkCountCaching | ||
| + def key_for_public_bookmarks_count | ||
| + "/v1/public_bookmarks_count/#{self.id}" | ||
| + end | ||
| + | ||
| + def public_bookmarks_count | ||
| + Rails.cache.fetch(self.key_for_public_bookmarks_count) do | ||
| + self.bookmarks.is_public.count | ||
| + end | ||
| + end | ||
| + | ||
| + def invalidate_public_bookmarks_count | ||
| + Rails.cache.delete(self.key_for_public_bookmarks_count) |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
lib/bookmark_count_caching.rb
| @@ -0,0 +1,15 @@ | ||
| +module BookmarkCountCaching | ||
| + def key_for_public_bookmarks_count | ||
| + "/v1/public_bookmarks_count/#{self.id}" | ||
| + end | ||
| + | ||
| + def public_bookmarks_count | ||
| + Rails.cache.fetch(self.key_for_public_bookmarks_count) do | ||
| + self.bookmarks.is_public.count |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
lib/bookmark_count_caching.rb
| @@ -0,0 +1,15 @@ | ||
| +module BookmarkCountCaching | ||
| + def key_for_public_bookmarks_count | ||
| + "/v1/public_bookmarks_count/#{self.id}" |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
lib/bookmark_count_caching.rb
| @@ -0,0 +1,15 @@ | ||
| +module BookmarkCountCaching | ||
| + def key_for_public_bookmarks_count | ||
| + "/v1/public_bookmarks_count/#{self.id}" | ||
| + end | ||
| + | ||
| + def public_bookmarks_count | ||
| + Rails.cache.fetch(self.key_for_public_bookmarks_count) do |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
app/models/bookmark.rb
| @@ -86,6 +86,15 @@ class Bookmark < ActiveRecord::Base | ||
| # Use the current user to determine what works are visible | ||
| scope :visible, visible_to_user(User.current_user) | ||
| + before_destroy :invalidate_bookmark_count | ||
| + after_save :invalidate_bookmark_count | ||
| + | ||
| + def invalidate_bookmark_count | ||
| + if self.bookmarkable_type == 'Work' | ||
| + Work.find(self.bookmarkable_id).invalidate_public_bookmarks_count |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
app/models/bookmark.rb
| @@ -86,6 +86,15 @@ class Bookmark < ActiveRecord::Base | ||
| # Use the current user to determine what works are visible | ||
| scope :visible, visible_to_user(User.current_user) | ||
| + before_destroy :invalidate_bookmark_count | ||
| + after_save :invalidate_bookmark_count | ||
| + | ||
| + def invalidate_bookmark_count | ||
| + if self.bookmarkable_type == 'Work' |
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://otwarchive.atlassian.net/browse/AO3-4495
Reverts otwcode/otwarchive#2454