From 3c914f5ecb7bf8e6152b28a7b3e8dc79cd98618b Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Fri, 20 Mar 2009 16:02:54 -0300 Subject: [PATCH] ActionItem955: added cache to blocks and friends listing --- app/controllers/box_organizer_controller.rb | 2 ++ app/controllers/my_profile/cms_controller.rb | 9 +++++++++ app/controllers/my_profile/friends_controller.rb | 1 + app/controllers/public/home_controller.rb | 1 - app/helpers/boxes_helper.rb | 15 ++++++++++++--- app/models/article.rb | 8 ++++++-- app/models/block.rb | 12 ++++++++++++ app/models/login_block.rb | 4 ++++ app/models/main_block.rb | 4 ++++ app/models/my_network_block.rb | 4 ++++ app/models/profile.rb | 2 +- app/models/profile_image_block.rb | 4 ++++ app/models/profile_info_block.rb | 4 ++++ app/models/recent_documents_block.rb | 4 ++++ app/models/tags_block.rb | 4 ++++ app/views/content_viewer/view_page.rhtml | 10 ++++++---- app/views/profile/friends.rhtml | 2 ++ app/views/shared/block.rhtml | 7 +++++++ test/unit/block_test.rb | 13 +++++++++++++ vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb | 8 ++++---- 20 files changed, 103 insertions(+), 15 deletions(-) create mode 100644 app/views/shared/block.rhtml diff --git a/app/controllers/box_organizer_controller.rb b/app/controllers/box_organizer_controller.rb index 43eeaa9..70ae138 100644 --- a/app/controllers/box_organizer_controller.rb +++ b/app/controllers/box_organizer_controller.rb @@ -80,6 +80,7 @@ class BoxOrganizerController < ApplicationController def save @block = boxes_holder.blocks.find(params[:id]) @block.update_attributes(params[:block]) + expire_timeout_fragment(@block.cache_keys) redirect_to :action => 'index' end @@ -90,6 +91,7 @@ class BoxOrganizerController < ApplicationController def remove @block = Block.find(params[:id]) if @block.destroy + expire_timeout_fragment(@block.cache_keys) redirect_to :action => 'index' else flash[:notice] = _('Failed to remove block') diff --git a/app/controllers/my_profile/cms_controller.rb b/app/controllers/my_profile/cms_controller.rb index 96f032d..83d5561 100644 --- a/app/controllers/my_profile/cms_controller.rb +++ b/app/controllers/my_profile/cms_controller.rb @@ -67,6 +67,7 @@ class CmsController < MyProfileController if request.post? @article.last_changed_by = user if @article.update_attributes(params[:article]) + expire_caches(@article) redirect_back return end @@ -113,6 +114,7 @@ class CmsController < MyProfileController @article.last_changed_by = user if request.post? if @article.save + expire_caches(@article) redirect_back return end @@ -158,6 +160,7 @@ class CmsController < MyProfileController def destroy @article = profile.articles.find(params[:id]) if request.post? + expire_caches(@article) @article.destroy redirect_to :action => (@article.parent ? 'view' : 'index'), :id => @article.parent end @@ -249,5 +252,11 @@ class CmsController < MyProfileController end end + def expire_caches(article) + article.hierarchy.each {|a| expire_fragment(/#{a.cache_key}/) } + blocks = article.profile.blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} + end + end diff --git a/app/controllers/my_profile/friends_controller.rb b/app/controllers/my_profile/friends_controller.rb index 2bb30cc..82d4a80 100644 --- a/app/controllers/my_profile/friends_controller.rb +++ b/app/controllers/my_profile/friends_controller.rb @@ -23,6 +23,7 @@ class FriendsController < MyProfileController def remove @friend = profile.friends.find(params[:id]) if request.post? && params[:confirmation] + expire_fragment(:action => 'friends', :controller => 'profile', :profile => profile.identifier) profile.remove_friend(@friend) redirect_to :action => 'index' end diff --git a/app/controllers/public/home_controller.rb b/app/controllers/public/home_controller.rb index 69a56e6..3636638 100644 --- a/app/controllers/public/home_controller.rb +++ b/app/controllers/public/home_controller.rb @@ -1,7 +1,6 @@ class HomeController < PublicController def index - @articles = environment.recent_documents(10) end end diff --git a/app/helpers/boxes_helper.rb b/app/helpers/boxes_helper.rb index 8c5f149..85ab9a9 100644 --- a/app/helpers/boxes_helper.rb +++ b/app/helpers/boxes_helper.rb @@ -55,6 +55,14 @@ module BoxesHelper end def display_block(block, main_content = nil) + render :file => 'shared/block', :locals => {:block => block, :main_content => main_content, :use_cache => use_cache? } + end + + def use_cache? + box_decorator == DontMoveBlocks + end + + def display_block_content(block, main_content = nil) content = block.main? ? main_content : block.content result = extract_block_content(content) footer_content = extract_block_content(block.footer) @@ -189,12 +197,13 @@ module BoxesHelper end def current_blocks - @controller.boxes_holder.boxes.map(&:blocks).flatten + @controller.boxes_holder.boxes.map(&:blocks).inject([]){|ac, a| ac + a} end def import_blocks_stylesheets - stylesheet_import( current_blocks.map{|b|'blocks/' + b.css_class_name}.uniq ) + "\n" + - stylesheet_import( current_blocks.map{|b|'blocks/' + b.css_class_name}.uniq, :themed_source => true ) + blocks_css_files = current_blocks.map{|b|'blocks/' + b.css_class_name}.uniq + stylesheet_import(blocks_css_files) + "\n" + + stylesheet_import(blocks_css_files, :themed_source => true ) end end diff --git a/app/models/article.rb b/app/models/article.rb index 604c5c1..21e2fb0 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -171,11 +171,11 @@ class Article < ActiveRecord::Base end def url - self.profile.url.merge(:page => path.split('/')) + @url ||= self.profile.url.merge(:page => path.split('/')) end def view_url - image? ? url.merge(:view => true) : url + @view_url ||= image? ? url.merge(:view => true) : url end def allow_children? @@ -254,6 +254,10 @@ class Article < ActiveRecord::Base profile end + def cache_key + "article-id-#{id}" + end + private def sanitize_tag_list diff --git a/app/models/block.rb b/app/models/block.rb index f9ba6d4..491adcd 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -86,4 +86,16 @@ class Block < ActiveRecord::Base end end + def cacheable? + true + end + + def cache_keys + "block-id-#{id}" + end + + def timeout + 4.hours + end + end diff --git a/app/models/login_block.rb b/app/models/login_block.rb index ef81e28..4a07832 100644 --- a/app/models/login_block.rb +++ b/app/models/login_block.rb @@ -18,4 +18,8 @@ class LoginBlock < Block end end + def cacheable? + false + end + end diff --git a/app/models/main_block.rb b/app/models/main_block.rb index 7d92682..ec0e179 100644 --- a/app/models/main_block.rb +++ b/app/models/main_block.rb @@ -20,4 +20,8 @@ class MainBlock < Block false end + def cacheable? + false + end + end diff --git a/app/models/my_network_block.rb b/app/models/my_network_block.rb index 1c37987..fd5794c 100644 --- a/app/models/my_network_block.rb +++ b/app/models/my_network_block.rb @@ -24,4 +24,8 @@ class MyNetworkBlock < Block end end + def cacheable? + false + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index 6fb9fc6..20bf3e1 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -326,7 +326,7 @@ class Profile < ActiveRecord::Base end def url - if self.domains.empty? + @url ||= if self.domains.empty? generate_url(:controller => 'content_viewer', :action => 'view_page', :page => []) else Noosfero.url_options.merge({ :host => self.domains.first.name, :controller => 'content_viewer', :action => 'view_page', :page => []}) diff --git a/app/models/profile_image_block.rb b/app/models/profile_image_block.rb index c529499..4c1f717 100644 --- a/app/models/profile_image_block.rb +++ b/app/models/profile_image_block.rb @@ -19,4 +19,8 @@ class ProfileImageBlock < Block false end + def cacheable? + false + end + end diff --git a/app/models/profile_info_block.rb b/app/models/profile_info_block.rb index ddfa776..43c4ad5 100644 --- a/app/models/profile_info_block.rb +++ b/app/models/profile_info_block.rb @@ -19,4 +19,8 @@ class ProfileInfoBlock < Block false end + def cacheable? + false + end + end diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 402675f..0ea4888 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -32,4 +32,8 @@ class RecentDocumentsBlock < Block end end + def timeout + 2.months + end + end diff --git a/app/models/tags_block.rb b/app/models/tags_block.rb index 7dd27dc..f03a4a4 100644 --- a/app/models/tags_block.rb +++ b/app/models/tags_block.rb @@ -29,4 +29,8 @@ class TagsBlock < Block "\n\n"; end + def timeout + 15.minutes + end + end diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index 93bc494..a5f1c12 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -83,10 +83,12 @@ <% end %> -
"> - <%= article_to_html(@page) %> -
-
+<% cache(@page.cache_key) do %> +
"> + <%= article_to_html(@page) %> +
+
+<% end %> <% if ! @page.categories.empty? %>
diff --git a/app/views/profile/friends.rhtml b/app/views/profile/friends.rhtml index ab10b05..9d4719b 100644 --- a/app/views/profile/friends.rhtml +++ b/app/views/profile/friends.rhtml @@ -3,11 +3,13 @@

<%= __("%s's friends") % profile.name %>

+<% cache do%> +<% end %> <% button_bar do %> <% if user == profile %> diff --git a/app/views/shared/block.rhtml b/app/views/shared/block.rhtml new file mode 100644 index 0000000..7ef6fc1 --- /dev/null +++ b/app/views/shared/block.rhtml @@ -0,0 +1,7 @@ +<% if block.cacheable? && use_cache %> + <% cache_timeout(block.cache_keys, block.timeout.from_now) do %> + <%= display_block_content(block, main_content) %> + <% end %> +<% else %> + <%= display_block_content(block, main_content) %> +<% end %> diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index eae12a2..042f090 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -54,4 +54,17 @@ class BlockTest < Test::Unit::TestCase assert !b.visible? end + should 'be cacheable' do + b = Block.new + assert b.cacheable? + end + + should 'provide chache keys' do + p = create_user('test_user').person + box = p.boxes[0] + b = Block.create!(:box => box) + + assert_equal( "block-id-#{b.id}", b.cache_keys) + end + end diff --git a/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb b/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb index 5850c54..1d04d1f 100644 --- a/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb +++ b/vendor/plugins/timed_cached_fragment/lib/timed_cache_fragment.rb @@ -17,12 +17,12 @@ module ActionController #handles the expiration of timeout fragment def expire_timeout_fragment(key) @@cache_timeout_values[key] = nil - expire_fragment(key) + expire_fragment(/#{key}/) end #checks to see if a cache has fully expired def is_cache_expired?(name, is_key = false) - key = is_key ? fragment_cache_key(name) : name - return (!@@cache_timeout_values.has_key?(key)) || (@@cache_timeout_values[key] < Time.now) + key = is_key ? name : fragment_cache_key(name) + return (!@@cache_timeout_values[key]) || (@@cache_timeout_values[key] < Time.now) end end end @@ -46,4 +46,4 @@ end #add to the respective controllers ActionView::Base.send(:include, ActionView::Helpers::TimedCacheHelper) -ActionController::Base.send(:include, ActionController::Cache::TimedCache) \ No newline at end of file +ActionController::Base.send(:include, ActionController::Cache::TimedCache) -- libgit2 0.21.2