mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-09-25 07:24:55 -04:00
Merge pull request #4794 from Growstuff/improve-cache-memory-usage-9918781528129420138
Optimize caching memory usage in PhotoCapable and CropsHelper
This commit is contained in:
1 parent
2c2f48ba32
commit
07f07e4c3d
4 files changed
+38
-10
No files matched your search
@@ -77,7 +77,7 @@ module CropsHelper
|
||||
}
|
||||
end
|
||||
|
||||
crop.posts.each do |post|
|
||||
crop.posts.limit(50).each do |post|
|
||||
subject_of_entities << {
|
||||
'@type': "SocialMediaPosting",
|
||||
url: post_url(post),
|
||||
@@ -89,7 +89,7 @@ module CropsHelper
|
||||
}
|
||||
end
|
||||
|
||||
crop.photos.each do |photo|
|
||||
crop.photos.limit(50).each do |photo|
|
||||
images << photo.fullsize_url
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,18 +10,18 @@ module PhotoCapable
|
||||
scope :has_photos, -> { includes(:photos).where.not(photos: { id: nil }) }
|
||||
|
||||
def default_photo
|
||||
Rails.cache.fetch("#{cache_key_with_version}/default_photo", expires_in: 8.hours) do
|
||||
most_liked_photo
|
||||
end
|
||||
most_liked_photo
|
||||
end
|
||||
|
||||
def thumbnail_url
|
||||
df = default_photo
|
||||
Rails.cache.fetch("#{cache_key_with_version}/thumbnail_url", expires_in: 8.hours) do
|
||||
df = default_photo
|
||||
|
||||
if df
|
||||
df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url
|
||||
elsif respond_to?(:crop) && crop.present?
|
||||
crop.thumbnail_url
|
||||
if df
|
||||
df.source == 'flickr' ? df.fullsize_url : df.thumbnail_url
|
||||
elsif respond_to?(:crop) && crop.present?
|
||||
crop.thumbnail_url
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -42,4 +42,25 @@ describe CropsHelper do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#crop_jsonld_data' do
|
||||
let(:crop) { create(:crop, name: 'Tomato') }
|
||||
|
||||
it 'returns schema.org BioChemEntity hash structure' do
|
||||
data = helper.crop_jsonld_data(crop)
|
||||
expect(data['@context']).to eq('https://schema.org')
|
||||
expect(data['@type']).to eq('BioChemEntity')
|
||||
expect(data[:name]).to eq('Tomato')
|
||||
end
|
||||
|
||||
it 'caps posts and photos at 50' do
|
||||
create_list(:post, 60, crops: [crop])
|
||||
photos = create_list(:photo, 60)
|
||||
photos.each { |p| crop.photo_associations.create!(photo: p) }
|
||||
|
||||
data = helper.crop_jsonld_data(crop, full_attributes: true)
|
||||
expect(data[:subjectOf].size).to eq(50)
|
||||
expect(data[:image].size).to eq(50)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -154,6 +154,13 @@ describe Crop do
|
||||
|
||||
it { expect(crop.default_photo).to eq photo }
|
||||
|
||||
it 'caches thumbnail_url string in Rails.cache' do
|
||||
expected_url = photo.source == 'flickr' ? photo.fullsize_url : photo.thumbnail_url
|
||||
expect(crop.thumbnail_url).to eq expected_url
|
||||
cached_value = Rails.cache.read("#{crop.cache_key_with_version}/thumbnail_url")
|
||||
expect(cached_value).to eq expected_url
|
||||
end
|
||||
|
||||
include_examples 'has default photo'
|
||||
end
|
||||
|
||||
|
||||
Reference in new issue
Block a user