cache newest items on homepage and items#index

This commit is contained in:
Emi Matchu 2012-08-09 22:35:30 -04:00
parent 1e3938eea9
commit f6d34841ec
5 changed files with 57 additions and 35 deletions

View file

@ -34,8 +34,9 @@ class ItemsController < ApplicationController
else
respond_to do |format|
format.html {
unless fragment_exist?('items#index newest_items')
@newest_items = Item.newest.limit(18)
current_user.assign_closeted_to_items!(@newest_items) if user_signed_in?
end
}
format.js { render :json => {:error => '$q required'}}
end

View file

@ -57,7 +57,10 @@ class OutfitsController < ApplicationController
@top_contributors = User.top_contributors.limit(User::PreviewTopContributorsCount)
end
unless fragment_exist?('outfits#new newest_items')
@newest_items = Item.newest.select([:id, :name, :thumbnail_url]).limit(9)
end
@latest_contribution = Contribution.recent.first
Contribution.preload_contributeds_and_parents([@latest_contribution])
end

View file

@ -1,4 +1,9 @@
class ItemObserver < ActionController::Caching::Sweeper
def after_create(item)
Rails.logger.debug "Item #{item.id} was just created"
expire_newest_items
end
def after_update(item)
Rails.logger.debug "Item #{item.id} was just updated"
expire_cache_for(item)
@ -11,7 +16,16 @@ class ItemObserver < ActionController::Caching::Sweeper
private
def controller
@controller ||= ActionController::Base.new
end
def expire_cache_for(item)
ActionController::Base.new.expire_fragment("items/#{item.id}#item_link_partial")
controller.expire_fragment("items/#{item.id}#item_link_partial")
end
def expire_newest_items
controller.expire_fragment('outfits#new newest_items')
controller.expire_fragment('items#index newest_items')
end
end

View file

@ -14,6 +14,7 @@
= will_paginate @items
- else
#search-info
- cache :action_suffix => 'search_help' do
#search-help
%h2 Find what you're looking for
%dl
@ -35,10 +36,12 @@
%dt type:hat
%dd returns any item that fills a "hat" zone
- cache 'items#index newest_items' do
#search-new-items
%h2 Newest items
= render @newest_items
- cache :action_suffix => 'species_search_links' do
#species-search-links
%h2 Can't decide? Search by species
= standard_species_search_links

View file

@ -94,6 +94,7 @@
%div
%a#blog-preview-linkback{:href => 'http://blog.openneo.net/'} OpenNeo Blog
- cache 'outfits#new newest_items' do
#newest-items
%h2 New Items
%ul