diff --git a/app/models/crop.rb b/app/models/crop.rb index ad2b440c6..aa742dcbc 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -81,23 +81,24 @@ class Crop < ActiveRecord::Base # Crop.interesting # returns a list of interesting crops, for use on the homepage etc def Crop.interesting - howmany = 12 # max number to find - interesting_crops = Array.new - min_plantings = 3 # needs this many plantings to be interesting - min_photos = 3 # needs this many photos to be interesting + return Rails.cache.fetch("interesting_crops", :expires_in => 1.day) do + howmany = 12 # max number to find + interesting_crops = Array.new + min_plantings = 3 # needs this many plantings to be interesting + min_photos = 3 # needs this many photos to be interesting - # it's inefficient to shuffle this up-front, but if we cache - # the crops on the homepage it won't be run too often, and there's - # not *that* many crops. - Crop.all.shuffle.each do |c| - break if interesting_crops.length == howmany - next unless c.photos.count >= min_photos - next unless c.plantings_count >= min_plantings - interesting_crops.push(c) + # it's inefficient to shuffle this up-front, but if we cache + # the crops on the homepage it won't be run too often, and there's + # not *that* many crops. + Crop.all.shuffle.each do |c| + break if interesting_crops.length == howmany + next unless c.photos.count >= min_photos + next unless c.plantings_count >= min_plantings + interesting_crops.push(c) + end + + interesting_crops end - - Rails.cache.fetch("interesting_crops", :expires_in => 1.day) { interesting_crops } - return interesting_crops end end diff --git a/app/models/member.rb b/app/models/member.rb index 4c198edca..65432388f 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -182,15 +182,16 @@ class Member < ActiveRecord::Base end def Member.interesting - howmany = 12 # max number to find - interesting_members = Array.new - Member.confirmed.located.recently_signed_in.each do |m| - break if interesting_members.length == howmany - next unless m.plantings.present? - interesting_members.push(m) + return Rails.cache.fetch('interesting_members', :expires_in => 1.day) do + howmany = 12 # max number to find + interesting_members = Array.new + Member.confirmed.located.recently_signed_in.each do |m| + break if interesting_members.length == howmany + next unless m.plantings.present? + interesting_members.push(m) + end + interesting_members end - Rails.cache.fetch('interesting_members', :expires_in => 1.day) { interesting_members } - return interesting_members end protected diff --git a/app/models/member_sweeper.rb b/app/models/member_sweeper.rb index 015c2dd11..211a9d833 100644 --- a/app/models/member_sweeper.rb +++ b/app/models/member_sweeper.rb @@ -6,6 +6,9 @@ class MemberSweeper < ActionController::Caching::Sweeper end def after_update(member) + if member.location.blank? + Rails.cache.delete('interesting_members') + end expire_fragment("member_thumbnail_#{member.id}") end diff --git a/app/models/planting.rb b/app/models/planting.rb index 050b89080..2bd94a843 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -75,20 +75,20 @@ class Planting < ActiveRecord::Base # we can't do this via a scope (as far as we know) so sadly we have to # do it this way. def Planting.interesting - howmany = 12 # max amount to collect - interesting_plantings = Array.new - seen_owners = Hash.new(false) # keep track of which owners we've seen already + return Rails.cache.fetch("interesting_plantings", :expires_in => 3.hours) do + howmany = 12 # max amount to collect + interesting_plantings = Array.new + seen_owners = Hash.new(false) # keep track of which owners we've seen already - Planting.all.each do |p| - break if interesting_plantings.count == howmany # got enough yet? - next unless p.interesting? # skip those that don't have photos - next if seen_owners[p.owner] # skip if we already have one from this owner + Planting.all.each do |p| + break if interesting_plantings.count == howmany # got enough yet? + next unless p.interesting? # skip those that don't have photos + next if seen_owners[p.owner] # skip if we already have one from this owner - seen_owners[p.owner] = true # we've seen this owner - interesting_plantings.push(p) + seen_owners[p.owner] = true # we've seen this owner + interesting_plantings.push(p) + end + interesting_plantings end - - Rails.cache.fetch("interesting_plantings", :expires_in => 3.hours) { interesting_plantings } - return interesting_plantings end end diff --git a/app/models/seed.rb b/app/models/seed.rb index 56b0568aa..18b2148dc 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -33,17 +33,19 @@ class Seed < ActiveRecord::Base # Seed.interesting # returns a list of interesting seeds, for use on the homepage etc def Seed.interesting - howmany = 12 # max number to find - interesting_seeds = Array.new + return Rails.cache.fetch('interesting_seeds', :expires_in => 6.hours) do - Seed.tradable.each do |s| - break if interesting_seeds.length == howmany - next if s.owner.location.blank? # don't want unspecified locations - interesting_seeds.push(s) + howmany = 12 # max number to find + interesting_seeds = Array.new + + Seed.tradable.each do |s| + break if interesting_seeds.length == howmany + next if s.owner.location.blank? # don't want unspecified locations + interesting_seeds.push(s) + end + interesting_seeds end - Rails.cache.fetch('interesting_seeds', :expires_in => 6.hours) { interesting_seeds } - return interesting_seeds end def seed_slug