Commit 5a07068e56f10e5951051ce3b083adc34100bece

Authored by Rodrigo Souto
1 parent f3850081

Making blocks and articles cache language sensitive

(ActionItem2336)
app/helpers/sweeper_helper.rb
... ... @@ -20,7 +20,7 @@ module SweeperHelper
20 20  
21 21 # friends blocks
22 22 blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)}
23   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  23 + BlockSweeper.expire_blocks(blocks)
24 24 end
25 25  
26 26 def expire_communities(profile)
... ... @@ -32,13 +32,13 @@ module SweeperHelper
32 32  
33 33 # communities block
34 34 blocks = profile.blocks.select{|b| b.kind_of?(CommunitiesBlock)}
35   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  35 + BlockSweeper.expire_blocks(blocks)
36 36 end
37 37  
38 38 def expire_enterprises(profile)
39 39 # enterprises and favorite enterprises blocks
40 40 blocks = profile.blocks.select {|b| [EnterprisesBlock, FavoriteEnterprisesBlock].any?{|klass| b.kind_of?(klass)} }
41   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  41 + BlockSweeper.expire_blocks(blocks)
42 42 end
43 43  
44 44 def expire_profile_index(profile)
... ...
app/models/article.rb
... ... @@ -496,8 +496,8 @@ class Article < ActiveRecord::Base
496 496 end
497 497  
498 498 alias :active_record_cache_key :cache_key
499   - def cache_key(params = {}, the_profile = nil)
500   - active_record_cache_key +
  499 + def cache_key(params = {}, the_profile = nil, language = 'en')
  500 + active_record_cache_key+'-'+language +
501 501 (allow_post_content?(the_profile) ? "-owner" : '') +
502 502 (params[:npage] ? "-npage-#{params[:npage]}" : '') +
503 503 (params[:year] ? "-year-#{params[:year]}" : '') +
... ...
app/models/block.rb
... ... @@ -127,6 +127,11 @@ class Block < ActiveRecord::Base
127 127 true
128 128 end
129 129  
  130 + alias :active_record_cache_key :cache_key
  131 + def cache_key(language='en')
  132 + active_record_cache_key+'-'+language
  133 + end
  134 +
130 135 def timeout
131 136 4.hours
132 137 end
... ...
app/sweepers/article_sweeper.rb
... ... @@ -13,15 +13,11 @@ class ArticleSweeper < ActiveRecord::Observer
13 13 protected
14 14  
15 15 def expire_caches(article)
16   - article.hierarchy.each do |a|
17   - if a != article
18   - a.update_attribute(:updated_at, Time.now)
19   - end
20   - end
  16 + article.hierarchy.each { |a| a.touch if a != article }
21 17 blocks = article.profile.blocks
22 18 blocks += article.profile.environment.blocks if article.profile.environment
23 19 blocks = blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}}
24   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  20 + BlockSweeper.expire_blocks(blocks)
25 21 env = article.profile.environment
26 22 if env && (env.portal_community == article.profile)
27 23 expire_fragment(env.portal_news_cache_key)
... ...
app/sweepers/block_sweeper.rb
1 1 class BlockSweeper < ActiveRecord::Observer
2 2  
3   - include SweeperHelper
4 3 observe :block
5 4  
  5 + class << self
  6 + include SweeperHelper
  7 +
  8 + def cache_key_regex(block)
  9 + regex = '-[a-z]*$'
  10 + clean_ck = block.cache_key.gsub(/#{regex}/,'')
  11 + %r{#{clean_ck+regex}}
  12 + end
  13 +
  14 + # Expire block's all languages cache
  15 + def expire_block(block)
  16 + expire_timeout_fragment(cache_key_regex(block))
  17 + end
  18 +
  19 + def expire_blocks(blocks)
  20 + blocks.each { |block| expire_block(block) }
  21 + end
  22 + end
  23 +
6 24 def after_save(block)
7   - expire_fragment(block.cache_key)
  25 + self.class.expire_block(block)
8 26 end
9 27  
10 28 end
... ...
app/sweepers/friendship_sweeper.rb
... ... @@ -35,7 +35,7 @@ protected
35 35 end
36 36  
37 37 blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)}
38   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  38 + BlockSweeper.expire_blocks(blocks)
39 39 end
40 40  
41 41 end
... ...
app/sweepers/profile_sweeper.rb
... ... @@ -31,7 +31,7 @@ protected
31 31  
32 32 def expire_statistics_block_cache(profile)
33 33 blocks = profile.environment.blocks.select { |b| b.kind_of?(EnvironmentStatisticsBlock) }
34   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  34 + BlockSweeper.expire_blocks(blocks)
35 35 end
36 36  
37 37 def expire_blogs(profile)
... ...
app/sweepers/role_assignment_sweeper.rb
... ... @@ -25,7 +25,7 @@ protected
25 25  
26 26 profile.blocks_to_expire_cache.each { |block|
27 27 blocks = profile.blocks.select{|b| b.kind_of?(block)}
28   - blocks.map(&:cache_key).each{|ck|expire_timeout_fragment(ck)}
  28 + BlockSweeper.expire_blocks(blocks)
29 29 }
30 30 end
31 31  
... ...
app/views/content_viewer/view_page.rhtml
... ... @@ -52,7 +52,7 @@
52 52 </div>
53 53 <% end %>
54 54  
55   -<% cache(@page.cache_key(params, user)) do %>
  55 +<% cache(@page.cache_key(params, user, language)) do %>
56 56 <div class="<%="article-body article-body-" + @page.css_class_name %>">
57 57 <% options = @page.image? ? {:gallery_view => true} : {} %>
58 58 <%= article_to_html(@page, options) %>
... ...
app/views/shared/block.rhtml
1 1 <% if block.cacheable? && use_cache %>
2   - <% cache_timeout(block.cache_key, block.timeout) do %>
  2 + <% cache_timeout(block.cache_key(language), block.timeout) do %>
3 3 <%= display_block_content(block, user, main_content) %>
4 4 <% end %>
5 5 <% else %>
... ...
features/caching.feature 0 → 100644
... ... @@ -0,0 +1,30 @@
  1 +Feature: caching
  2 + As a user
  3 + I want to see the contents according with my language
  4 + Even with the contents being cached
  5 +
  6 + Background:
  7 + Given the cache is turned on
  8 + And the following user
  9 + | login | name |
  10 + | mario | Mario |
  11 + And I am logged in as "mario"
  12 +
  13 + Scenario: blog view page
  14 + Given the following blogs
  15 + | owner | name | display_posts_in_current_language | visualization_format |
  16 + | mario | Sample Blog | false | short |
  17 + And the following articles
  18 + | owner | name | parent |
  19 + | mario | Post1 | Sample Blog |
  20 + | mario | Post2 | Sample Blog |
  21 + When I go to article "Sample Blog"
  22 + Then I should see "No comments yet"
  23 + When I follow "Português"
  24 + Then I should see "Sem comentários ainda"
  25 +
  26 + Scenario: blocks
  27 + Given I am on Mario's homepage
  28 + Then I should see "Recent content"
  29 + When I follow "Português"
  30 + Then I should see "Conteúdo recente"
... ...
features/step_definitions/noosfero_steps.rb
... ... @@ -572,3 +572,7 @@ When /^I edit my profile$/ do
572 572 visit "/myprofile/#{@current_user}"
573 573 click_link "Edit Profile"
574 574 end
  575 +
  576 +Given /^the cache is turned (on|off)$/ do |state|
  577 + ActionController::Base.perform_caching = (state == 'on')
  578 +end
... ...