Commit 2abc4514919f3452241b259087e034c854f3f3e6
1 parent
cf21bd07
Exists in
master
and in
22 other branches
merging with next colivre branch
Showing
11 changed files
with
79 additions
and
7 deletions
Show diff stats
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 < Block | @@ -63,4 +63,9 @@ class ArticleBlock < 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 < ActiveRecord::Base | @@ -133,7 +133,7 @@ class Block < 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 < ActiveRecord::Base | @@ -142,4 +142,15 @@ class Block < 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 < Block | @@ -45,4 +45,7 @@ class BlogArchivesBlock < 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
app/models/recent_documents_block.rb
app/models/tags_block.rb
app/sweepers/article_sweeper.rb
| @@ -15,16 +15,16 @@ class ArticleSweeper < ActiveRecord::Observer | @@ -15,16 +15,16 @@ class ArticleSweeper < 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 < ActiveRecord::Observer | @@ -3,7 +3,11 @@ class CategorySweeper < 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
test/unit/block_test.rb
| @@ -156,4 +156,13 @@ class BlockTest < ActiveSupport::TestCase | @@ -156,4 +156,13 @@ class BlockTest < 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 |