From cca4427a7551d9f8c7c66e6d1812cd31969544ff Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 7 Feb 2025 01:58:29 -0800 Subject: [PATCH] Add cache to is_favorited(user) --- app/models/party.rb | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/party.rb b/app/models/party.rb index 31b1b7b..1a949c1 100644 --- a/app/models/party.rb +++ b/app/models/party.rb @@ -120,9 +120,6 @@ class Party < ApplicationRecord PREVIEW_EXPIRY = 30.days MAX_RETRY_ATTEMPTS = 3 - def is_favorited(user) - user.favorite_parties.include? self if user - end after_commit :schedule_preview_generation, if: :should_generate_preview? def is_remix @@ -149,6 +146,14 @@ class Party < ApplicationRecord visibility == 3 end + def is_favorited(user) + return false unless user + + Rails.cache.fetch("party_#{id}_favorited_by_#{user.id}", expires_in: 1.hour) do + user.favorite_parties.include?(self) + end + end + def ready_for_preview? return false if weapons_count < 1 # At least 1 weapon return false if characters_count < 1 # At least 1 character