and another try and making caching work

This commit is contained in:
Skud
2013-08-14 13:51:54 +10:00
parent a04c93f843
commit 9409fc6507
5 changed files with 50 additions and 43 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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