Commit 2abc4514919f3452241b259087e034c854f3f3e6

Authored by Leandro Nunes dos Santos
1 parent cf21bd07

merging with next colivre branch

app/helpers/sweeper_helper.rb
@@ -44,4 +44,30 @@ module SweeperHelper @@ -44,4 +44,30 @@ module SweeperHelper
44 def expire_profile_index(profile) 44 def expire_profile_index(profile)
45 expire_timeout_fragment(profile.relationships_cache_key) 45 expire_timeout_fragment(profile.relationships_cache_key)
46 end 46 end
  47 +
  48 + def expire_blocks_cache(context, causes)
  49 + if context.kind_of?(Profile)
  50 + profile = context
  51 + environment = profile.environment
  52 + else
  53 + environment = context
  54 + profile = nil
  55 + end
  56 +
  57 + blocks_to_expire = []
  58 + if profile
  59 + profile.blocks.each {|block|
  60 + conditions = block.class.expire_on
  61 + blocks_to_expire << block unless (conditions[:profile] & causes).empty?
  62 + }
  63 + end
  64 + environment.blocks.each {|block|
  65 + conditions = block.class.expire_on
  66 + blocks_to_expire << block unless (conditions[:environment] & causes).empty?
  67 + }
  68 +
  69 + blocks_to_expire.uniq!
  70 + BlockSweeper.expire_blocks(blocks_to_expire)
  71 + end
  72 +
47 end 73 end
app/models/article_block.rb
@@ -63,4 +63,9 @@ class ArticleBlock &lt; Block @@ -63,4 +63,9 @@ class ArticleBlock &lt; Block
63 end 63 end
64 64
65 settings_items :visualization_format, :type => :string, :default => 'short' 65 settings_items :visualization_format, :type => :string, :default => 'short'
  66 +
  67 + def self.expire_on
  68 + { :profile => [:article], :environment => [:article] }
  69 + end
  70 +
66 end 71 end
app/models/block.rb
@@ -133,7 +133,7 @@ class Block &lt; ActiveRecord::Base @@ -133,7 +133,7 @@ class Block &lt; ActiveRecord::Base
133 def cache_key(language='en') 133 def cache_key(language='en')
134 active_record_cache_key+'-'+language 134 active_record_cache_key+'-'+language
135 end 135 end
136 - 136 +
137 def timeout 137 def timeout
138 4.hours 138 4.hours
139 end 139 end
@@ -142,4 +142,15 @@ class Block &lt; ActiveRecord::Base @@ -142,4 +142,15 @@ class Block &lt; ActiveRecord::Base
142 false 142 false
143 end 143 end
144 144
  145 + # Override in your subclasses.
  146 + # Define which events and context should cause the block cache to expire
  147 + # Possible events are: :article, :profile, :friendship, :category
  148 + # Possible contexts are: :profile, :environment
  149 + def self.expire_on
  150 + {
  151 + :profile => [],
  152 + :environment => []
  153 + }
  154 + end
  155 +
145 end 156 end
app/models/blog_archives_block.rb
@@ -45,4 +45,7 @@ class BlogArchivesBlock &lt; Block @@ -45,4 +45,7 @@ class BlogArchivesBlock &lt; Block
45 content_tag('div', link_to(_('Subscribe RSS Feed'), owner_blog.feed.url), :class => 'subscribe-feed') 45 content_tag('div', link_to(_('Subscribe RSS Feed'), owner_blog.feed.url), :class => 'subscribe-feed')
46 end 46 end
47 47
  48 + def self.expire_on
  49 + { :profile => [:article], :environment => [:article] }
  50 + end
48 end 51 end
app/models/categories_block.rb
@@ -35,4 +35,7 @@ class CategoriesBlock &lt; Block @@ -35,4 +35,7 @@ class CategoriesBlock &lt; Block
35 end 35 end
36 end 36 end
37 37
  38 + def self.expire_on
  39 + { :profile => [], :environment => [:category] }
  40 + end
38 end 41 end
app/models/recent_documents_block.rb
@@ -30,4 +30,7 @@ class RecentDocumentsBlock &lt; Block @@ -30,4 +30,7 @@ class RecentDocumentsBlock &lt; Block
30 end 30 end
31 end 31 end
32 32
  33 + def self.expire_on
  34 + { :profile => [:article], :environment => [:article] }
  35 + end
33 end 36 end
app/models/tags_block.rb
@@ -59,4 +59,8 @@ class TagsBlock &lt; Block @@ -59,4 +59,8 @@ class TagsBlock &lt; Block
59 15.minutes 59 15.minutes
60 end 60 end
61 61
  62 + def self.expire_on
  63 + { :profile => [:article], :environment => [:article] }
  64 + end
  65 +
62 end 66 end
app/sweepers/article_sweeper.rb
@@ -15,16 +15,16 @@ class ArticleSweeper &lt; ActiveRecord::Observer @@ -15,16 +15,16 @@ class ArticleSweeper &lt; ActiveRecord::Observer
15 Article.find(article.parent_id_was).touch if article.parent_id_was 15 Article.find(article.parent_id_was).touch if article.parent_id_was
16 end 16 end
17 end 17 end
18 - 18 +
  19 +
19 protected 20 protected
20 21
21 def expire_caches(article) 22 def expire_caches(article)
  23 + expire_blocks_cache(article.profile, [:article])
  24 +
22 return if !article.environment 25 return if !article.environment
  26 +
23 article.hierarchy(true).each { |a| a.touch if a != article } 27 article.hierarchy(true).each { |a| a.touch if a != article }
24 - blocks = article.profile.blocks  
25 - blocks += article.profile.environment.blocks if article.profile.environment  
26 - blocks = blocks.select{|b|[RecentDocumentsBlock, BlogArchivesBlock].any?{|c| b.kind_of?(c)}}  
27 - BlockSweeper.expire_blocks(blocks)  
28 env = article.profile.environment 28 env = article.profile.environment
29 if env && (env.portal_community == article.profile) 29 if env && (env.portal_community == article.profile)
30 article.environment.locales.keys.each do |locale| 30 article.environment.locales.keys.each do |locale|
app/sweepers/category_sweeper.rb
@@ -3,7 +3,11 @@ class CategorySweeper &lt; ActiveRecord::Observer @@ -3,7 +3,11 @@ class CategorySweeper &lt; ActiveRecord::Observer
3 include SweeperHelper 3 include SweeperHelper
4 4
5 def after_save(category) 5 def after_save(category)
6 - expire_fragment(category.environment.id.to_s + "_categories_menu") 6 + # expire_fragment(category.environment.id.to_s + "_categories_menu")
  7 + expire_blocks_cache(category.environment, [:category])
7 end 8 end
8 9
  10 + def after_destroy(category)
  11 + expire_blocks_cache(category.environment, [:category])
  12 + end
9 end 13 end
plugins/display_content/lib/display_content_block.rb
@@ -83,4 +83,8 @@ class DisplayContentBlock &lt; Block @@ -83,4 +83,8 @@ class DisplayContentBlock &lt; Block
83 return parents 83 return parents
84 end 84 end
85 85
  86 + def self.expire_on
  87 + { :profile => [:article], :environment => [:article] }
  88 + end
  89 +
86 end 90 end
test/unit/block_test.rb
@@ -156,4 +156,13 @@ class BlockTest &lt; ActiveSupport::TestCase @@ -156,4 +156,13 @@ class BlockTest &lt; ActiveSupport::TestCase
156 assert_equal box.environment, block.environment 156 assert_equal box.environment, block.environment
157 end 157 end
158 158
  159 + should 'inform conditions for expiration on profile context' do
  160 + conditions = Block.expire_on
  161 + assert conditions[:profile].kind_of?(Array)
  162 + end
  163 +
  164 + should 'inform conditions for expiration on environment context' do
  165 + conditions = Block.expire_on
  166 + assert conditions[:environment].kind_of?(Array)
  167 + end
159 end 168 end