Merge pull request #2327 from Br3nda/caching

Cache some heavy queries on models
This commit is contained in:
Brenda Wallace
2019-12-14 14:18:24 +13:00
committed by GitHub
2 changed files with 13 additions and 9 deletions

View File

@@ -8,7 +8,9 @@ module PhotoCapable
scope :has_photos, -> { includes(:photos).where.not(photos: { id: nil }) }
def default_photo
most_liked_photo
Rails.cache.fetch("#{cache_key_with_version}/default_photo", expires_in: 8.hours) do
most_liked_photo
end
end
def most_liked_photo

View File

@@ -42,14 +42,16 @@ module PredictPlanting
end
def percentage_grown
if finished?
100
elsif !planted?
0
elsif crop.perennial || finish_predicted_at.nil?
nil
else
calculate_percentage_grown
Rails.cache.fetch("#{cache_key_with_version}/percentage_grown", expires_in: 8.hours) do
if finished?
100
elsif !planted?
0
elsif crop.perennial || finish_predicted_at.nil?
nil
else
calculate_percentage_grown
end
end
end