From 1e3938eea91c39858f5b47848f617c5a25312f97 Mon Sep 17 00:00:00 2001 From: Matchu Date: Thu, 9 Aug 2012 19:34:56 -0400 Subject: [PATCH] improve closet performance by caching item link --- app/models/item.rb | 5 ---- app/models/item_observer.rb | 17 ++++++++++++ app/models/item_sweeper.rb | 26 ------------------- app/views/closet_lists/_closet_list.html.haml | 8 +++--- app/views/items/_item.html.haml | 2 +- app/views/items/_item_link.html.haml | 10 +++---- config/application.rb | 2 +- 7 files changed, 28 insertions(+), 42 deletions(-) create mode 100644 app/models/item_observer.rb delete mode 100644 app/models/item_sweeper.rb diff --git a/app/models/item.rb b/app/models/item.rb index 1f7aa39f..353e4f4a 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -1,5 +1,3 @@ -# requires item sweeper at bottom - class Item < ActiveRecord::Base include PrettyParam @@ -836,6 +834,3 @@ class Item < ActiveRecord::Base class SearchError < ArgumentError;end end - -require 'item_sweeper' - diff --git a/app/models/item_observer.rb b/app/models/item_observer.rb new file mode 100644 index 00000000..3af52734 --- /dev/null +++ b/app/models/item_observer.rb @@ -0,0 +1,17 @@ +class ItemObserver < ActionController::Caching::Sweeper + def after_update(item) + Rails.logger.debug "Item #{item.id} was just updated" + expire_cache_for(item) + end + + def after_destroy(item) + Rails.logger.debug "Item #{item.id} was just destroyed" + expire_cache_for(item) + end + + private + + def expire_cache_for(item) + ActionController::Base.new.expire_fragment("items/#{item.id}#item_link_partial") + end +end diff --git a/app/models/item_sweeper.rb b/app/models/item_sweeper.rb deleted file mode 100644 index b5a10ea6..00000000 --- a/app/models/item_sweeper.rb +++ /dev/null @@ -1,26 +0,0 @@ -class ItemSweeper < ActionController::Caching::Sweeper - observe Item - - def after_update(item) - expire_cache_for(item) - end - - def after_destroy(item) - expire_cache_for(item) - end - - private - def expire_cache_for(item) - options = {:controller => 'items', :action => 'show', :id => item.id} - expire_action(options) - end - - def expire_action(options) - if @controller - super - elsif LocalImpressHost - @tmp_controller ||= SweeperController.new - @tmp_controller.expire_action_proxy(options.dup) - end - end -end diff --git a/app/views/closet_lists/_closet_list.html.haml b/app/views/closet_lists/_closet_list.html.haml index 40005b79..077d927a 100644 --- a/app/views/closet_lists/_closet_list.html.haml +++ b/app/views/closet_lists/_closet_list.html.haml @@ -1,13 +1,13 @@ -.closet-list{'data-id' => closet_list.id, 'data-hangers-count' => closet_list.hangers.count, :id => "closet-list-#{closet_list.id}"} +.closet-list{'data-id' => closet_list.id, 'data-hangers-count' => closet_list.hangers.size, :id => "closet-list-#{closet_list.id}"} %header - if show_controls - = form_for [closet_list.user, closet_list], :html => {:class => 'visibility-form'} do |f| + = form_for [current_user, closet_list], :html => {:class => 'visibility-form'} do |f| = f.select :visibility, closet_visibility_choices(:human_name) = f.submit "Save" = closet_visibility_descriptions('items in this list') .closet-list-controls - = link_to 'Edit', edit_user_closet_list_path(closet_list.user, closet_list) - = form_tag user_closet_list_path(closet_list.user, closet_list), :method => 'delete' do + = link_to 'Edit', edit_user_closet_list_path(closet_list.user_id, closet_list) + = form_tag user_closet_list_path(closet_list.user_id, closet_list), :method => 'delete' do = submit_tag 'Delete', :confirm => closet_list_delete_confirmation(closet_list) %h4= closet_list.name diff --git a/app/views/items/_item.html.haml b/app/views/items/_item.html.haml index 7b497011..5a3d63ad 100644 --- a/app/views/items/_item.html.haml +++ b/app/views/items/_item.html.haml @@ -1,3 +1,3 @@ .object = render :partial => 'items/item_link', :locals => {:item => item} - + = closeted_icons_for(item) diff --git a/app/views/items/_item_link.html.haml b/app/views/items/_item_link.html.haml index e9f95056..a4a68d83 100644 --- a/app/views/items/_item_link.html.haml +++ b/app/views/items/_item_link.html.haml @@ -1,6 +1,6 @@ -= link_to item_path(item, :q => @query) do - = image_tag item.thumbnail_url, :alt => item.description, :title => item.description - %span.name= item.name - = nc_icon_for(item) - = closeted_icons_for(item) +- cache "items/#{item.id}#item_link_partial" do + = link_to item_path(item, :q => @query) do + = image_tag item.thumbnail_url, :alt => item.description, :title => item.description + %span.name= item.name + = nc_icon_for(item) diff --git a/config/application.rb b/config/application.rb index e571d0d7..21b1fc5b 100644 --- a/config/application.rb +++ b/config/application.rb @@ -20,7 +20,7 @@ module OpenneoImpressItems # config.plugins = [ :exception_notification, :ssl_requirement, :all ] # Activate observers that should always be running - config.active_record.observers = :item_sweeper, :user_sweeper + config.active_record.observers = :item_observer, :user_sweeper # 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.