From 5a07068e56f10e5951051ce3b083adc34100bece Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 9 Apr 2012 16:43:25 -0300 Subject: [PATCH] Making blocks and articles cache language sensitive --- app/helpers/sweeper_helper.rb | 6 +++--- app/models/article.rb | 4 ++-- app/models/block.rb | 5 +++++ app/sweepers/article_sweeper.rb | 8 ++------ app/sweepers/block_sweeper.rb | 22 ++++++++++++++++++++-- app/sweepers/friendship_sweeper.rb | 2 +- app/sweepers/profile_sweeper.rb | 2 +- app/sweepers/role_assignment_sweeper.rb | 2 +- app/views/content_viewer/view_page.rhtml | 2 +- app/views/shared/block.rhtml | 2 +- features/caching.feature | 30 ++++++++++++++++++++++++++++++ features/step_definitions/noosfero_steps.rb | 4 ++++ 12 files changed, 71 insertions(+), 18 deletions(-) create mode 100644 features/caching.feature diff --git a/app/helpers/sweeper_helper.rb b/app/helpers/sweeper_helper.rb index 8540c66..5aa4943 100644 --- a/app/helpers/sweeper_helper.rb +++ b/app/helpers/sweeper_helper.rb @@ -20,7 +20,7 @@ module SweeperHelper # friends blocks blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)} - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) end def expire_communities(profile) @@ -32,13 +32,13 @@ module SweeperHelper # communities block blocks = profile.blocks.select{|b| b.kind_of?(CommunitiesBlock)} - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) end def expire_enterprises(profile) # enterprises and favorite enterprises blocks blocks = profile.blocks.select {|b| [EnterprisesBlock, FavoriteEnterprisesBlock].any?{|klass| b.kind_of?(klass)} } - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) end def expire_profile_index(profile) diff --git a/app/models/article.rb b/app/models/article.rb index 6c3cb74..59e9e6d 100644 --- a/app/models/article.rb +++ b/app/models/article.rb @@ -496,8 +496,8 @@ class Article < ActiveRecord::Base end alias :active_record_cache_key :cache_key - def cache_key(params = {}, the_profile = nil) - active_record_cache_key + + def cache_key(params = {}, the_profile = nil, language = 'en') + active_record_cache_key+'-'+language + (allow_post_content?(the_profile) ? "-owner" : '') + (params[:npage] ? "-npage-#{params[:npage]}" : '') + (params[:year] ? "-year-#{params[:year]}" : '') + diff --git a/app/models/block.rb b/app/models/block.rb index cd4378b..80da92a 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -127,6 +127,11 @@ class Block < ActiveRecord::Base true end + alias :active_record_cache_key :cache_key + def cache_key(language='en') + active_record_cache_key+'-'+language + end + def timeout 4.hours end diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb index b3ba392..f879ece 100644 --- a/app/sweepers/article_sweeper.rb +++ b/app/sweepers/article_sweeper.rb @@ -13,15 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer protected def expire_caches(article) - article.hierarchy.each do |a| - if a != article - a.update_attribute(:updated_at, Time.now) - end - end + article.hierarchy.each { |a| a.touch if a != article } blocks = article.profile.blocks blocks += article.profile.environment.blocks if article.profile.environment blocks = blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}} - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) env = article.profile.environment if env && (env.portal_community == article.profile) expire_fragment(env.portal_news_cache_key) diff --git a/app/sweepers/block_sweeper.rb b/app/sweepers/block_sweeper.rb index 05a0bc2..fc8f0c0 100644 --- a/app/sweepers/block_sweeper.rb +++ b/app/sweepers/block_sweeper.rb @@ -1,10 +1,28 @@ class BlockSweeper < ActiveRecord::Observer - include SweeperHelper observe :block + class << self + include SweeperHelper + + def cache_key_regex(block) + regex = '-[a-z]*$' + clean_ck = block.cache_key.gsub(/#{regex}/,'') + %r{#{clean_ck+regex}} + end + + # Expire block's all languages cache + def expire_block(block) + expire_timeout_fragment(cache_key_regex(block)) + end + + def expire_blocks(blocks) + blocks.each { |block| expire_block(block) } + end + end + def after_save(block) - expire_fragment(block.cache_key) + self.class.expire_block(block) end end diff --git a/app/sweepers/friendship_sweeper.rb b/app/sweepers/friendship_sweeper.rb index 5896261..400dc4e 100644 --- a/app/sweepers/friendship_sweeper.rb +++ b/app/sweepers/friendship_sweeper.rb @@ -35,7 +35,7 @@ protected end blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)} - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) end end diff --git a/app/sweepers/profile_sweeper.rb b/app/sweepers/profile_sweeper.rb index 926d36a..bca6563 100644 --- a/app/sweepers/profile_sweeper.rb +++ b/app/sweepers/profile_sweeper.rb @@ -31,7 +31,7 @@ protected def expire_statistics_block_cache(profile) blocks = profile.environment.blocks.select { |b| b.kind_of?(EnvironmentStatisticsBlock) } - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) end def expire_blogs(profile) diff --git a/app/sweepers/role_assignment_sweeper.rb b/app/sweepers/role_assignment_sweeper.rb index 4c700f3..0b58b96 100644 --- a/app/sweepers/role_assignment_sweeper.rb +++ b/app/sweepers/role_assignment_sweeper.rb @@ -25,7 +25,7 @@ protected profile.blocks_to_expire_cache.each { |block| blocks = profile.blocks.select{|b| b.kind_of?(block)} - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)} + BlockSweeper.expire_blocks(blocks) } end diff --git a/app/views/content_viewer/view_page.rhtml b/app/views/content_viewer/view_page.rhtml index d771379..7ba980a 100644 --- a/app/views/content_viewer/view_page.rhtml +++ b/app/views/content_viewer/view_page.rhtml @@ -52,7 +52,7 @@ <% end %> -<% cache(@page.cache_key(params, user)) do %> +<% cache(@page.cache_key(params, user, language)) do %>
"> <% options = @page.image? ? {:gallery_view => true} : {} %> <%= article_to_html(@page, options) %> diff --git a/app/views/shared/block.rhtml b/app/views/shared/block.rhtml index bef5f03..1964b48 100644 --- a/app/views/shared/block.rhtml +++ b/app/views/shared/block.rhtml @@ -1,5 +1,5 @@ <% if block.cacheable? && use_cache %> - <% cache_timeout(block.cache_key, block.timeout) do %> + <% cache_timeout(block.cache_key(language), block.timeout) do %> <%= display_block_content(block, user, main_content) %> <% end %> <% else %> diff --git a/features/caching.feature b/features/caching.feature new file mode 100644 index 0000000..255d9d6 --- /dev/null +++ b/features/caching.feature @@ -0,0 +1,30 @@ +Feature: caching + As a user + I want to see the contents according with my language + Even with the contents being cached + + Background: + Given the cache is turned on + And the following user + | login | name | + | mario | Mario | + And I am logged in as "mario" + + Scenario: blog view page + Given the following blogs + | owner | name | display_posts_in_current_language | visualization_format | + | mario | Sample Blog | false | short | + And the following articles + | owner | name | parent | + | mario | Post1 | Sample Blog | + | mario | Post2 | Sample Blog | + When I go to article "Sample Blog" + Then I should see "No comments yet" + When I follow "Português" + Then I should see "Sem comentários ainda" + + Scenario: blocks + Given I am on Mario's homepage + Then I should see "Recent content" + When I follow "Português" + Then I should see "Conteúdo recente" diff --git a/features/step_definitions/noosfero_steps.rb b/features/step_definitions/noosfero_steps.rb index 06754da..d77d1f0 100644 --- a/features/step_definitions/noosfero_steps.rb +++ b/features/step_definitions/noosfero_steps.rb @@ -572,3 +572,7 @@ When /^I edit my profile$/ do visit "/myprofile/#{@current_user}" click_link "Edit Profile" end + +Given /^the cache is turned (on|off)$/ do |state| + ActionController::Base.perform_caching = (state == 'on') +end -- libgit2 0.21.2