home page caching, reset top contributors only when necessary

This commit is contained in:
Emi Matchu 2010-12-11 09:37:39 -05:00
parent 8d20a68a53
commit ca155314fa
7 changed files with 128 additions and 100 deletions

View file

@ -29,9 +29,13 @@ class OutfitsController < ApplicationController
end end
def new def new
unless fragment_exist?(:action_suffix => 'start_from_scratch_form_content')
@colors = Color.all_ordered_by_name @colors = Color.all_ordered_by_name
@species = Species.all_ordered_by_name @species = Species.all_ordered_by_name
@top_contributors = User.top_contributors.limit(3) end
unless fragment_exist?(:action_suffix => 'top_contributors')
@top_contributors = User.top_contributors.limit(User::PreviewTopContributorsCount)
end
end end
def show def show

View file

@ -1,6 +1,8 @@
class PetsController < ApplicationController class PetsController < ApplicationController
rescue_from Pet::PetNotFound, :with => :pet_not_found rescue_from Pet::PetNotFound, :with => :pet_not_found
cache_sweeper :user_sweeper
DESTINATIONS = { DESTINATIONS = {
'needed_items' => '?', 'needed_items' => '?',
'root' => '#', 'root' => '#',

View file

@ -1,5 +1,3 @@
puts 'Hey! Item sweeper!'
class ItemSweeper < ActionController::Caching::Sweeper class ItemSweeper < ActionController::Caching::Sweeper
observe Item observe Item

View file

@ -1,5 +1,6 @@
class User < ActiveRecord::Base class User < ActiveRecord::Base
DefaultAuthServerId = 1 DefaultAuthServerId = 1
PreviewTopContributorsCount = 3
has_many :contributions has_many :contributions
has_many :outfits has_many :outfits
@ -37,4 +38,8 @@ class User < ActiveRecord::Base
end end
user user
end end
def self.points_required_to_pass_top_contributor(offset)
User.top_contributors.select(:points).limit(1).offset(offset).first.points
end
end end

View file

@ -0,0 +1,12 @@
class UserSweeper < ActionController::Caching::Sweeper
observe User
def before_save(user)
if user.points_changed?
points_to_beat = User.points_required_to_pass_top_contributor(User::PreviewTopContributorsCount - 1)
if user.points >= points_to_beat
expire_fragment(:controller => 'outfits', :action => 'new', :action_suffix => 'top_contributors')
end
end
end
end

View file

@ -1,11 +1,13 @@
- hide_home_link - hide_home_link
#outfit-forms #outfit-forms
- cache :action_suffix => 'outfit_forms_intro' do
#pet-preview #pet-preview
= image_tag 'default_preview.png', :alt => '' = image_tag 'default_preview.png', :alt => ''
%span %span
%h1 Dress to Impress %h1 Dress to Impress
%h2 Neopets wearables made easy! %h2 Neopets wearables made easy!
= form_tag load_pet_path, :id => 'load-pet-to-wardrobe' do = form_tag load_pet_path, :id => 'load-pet-to-wardrobe' do
- cache :action_suffix => 'main_load_pet_form_content' do
= origin_tag root_path = origin_tag root_path
= destination_tag 'wardrobe' = destination_tag 'wardrobe'
%fieldset %fieldset
@ -14,11 +16,13 @@
%button{:type => "submit"} %button{:type => "submit"}
Plan my outfit! Plan my outfit!
= form_tag wardrobe_path, :method => 'get', :id => 'start-from-scratch' do = form_tag wardrobe_path, :method => 'get', :id => 'start-from-scratch' do
- cache :action_suffix => 'start_from_scratch_form_content' do
%fieldset %fieldset
%legend Or start from scratch %legend Or start from scratch
= pet_attribute_select 'color', @colors, 8 = pet_attribute_select 'color', @colors, 8
= pet_attribute_select 'species', @species = pet_attribute_select 'species', @species
%input{:type => "submit", :value => "Go"} %input{:type => "submit", :value => "Go"}
- cache :action_suffix => 'sections_and_description' do
%ul#sections %ul#sections
%li %li
%a{:href => "http://forum.openneo.net"} %a{:href => "http://forum.openneo.net"}
@ -69,6 +73,7 @@
and award and award
= link_to 'points', top_contributors_path = link_to 'points', top_contributors_path
so you can show off just how dedicated you really are! so you can show off just how dedicated you really are!
- cache :action_suffix => 'top_contributors' do
#top-contributors #top-contributors
%h3 Top Contributors %h3 Top Contributors
%ol %ol
@ -79,6 +84,7 @@
= user.points = user.points
%a{:href => top_contributors_path} see more %a{:href => top_contributors_path} see more
= form_tag load_pet_path, :id => 'how-can-i-help' do = form_tag load_pet_path, :id => 'how-can-i-help' do
- cache :action_suffix => 'how_can_i_help_form_content' do
= destination_tag 'needed_items' = destination_tag 'needed_items'
= origin_tag root_path = origin_tag root_path
%h2 How can I help? %h2 How can I help?
@ -89,6 +95,7 @@
%button{:type => "submit"} %button{:type => "submit"}
Let's model! Let's model!
= form_tag load_pet_path, :id => 'i-found-something' do = form_tag load_pet_path, :id => 'i-found-something' do
- cache :action_suffix => 'i_found_something_form_content' do
= origin_tag root_path = origin_tag root_path
%a{:href => bulk_pets_path} %a{:href => bulk_pets_path}
add many pets add many pets

View file

@ -20,7 +20,7 @@ module OpenneoImpressItems
# config.plugins = [ :exception_notification, :ssl_requirement, :all ] # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
# Activate observers that should always be running # Activate observers that should always be running
config.active_record.observers = :item_sweeper config.active_record.observers = :item_sweeper, :user_sweeper
# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.