mirror of
https://github.com/Growstuff/growstuff.git
synced 2026-03-26 18:56:06 -04:00
and another try and making caching work
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user