diff --git a/app/helpers/sweeper_helper.rb b/app/helpers/sweeper_helper.rb index 5aa4943..fac400a 100644 --- a/app/helpers/sweeper_helper.rb +++ b/app/helpers/sweeper_helper.rb @@ -44,4 +44,30 @@ module SweeperHelper def expire_profile_index(profile) expire_timeout_fragment(profile.relationships_cache_key) end + + def expire_blocks_cache(context, causes) + if context.kind_of?(Profile) + profile = context + environment = profile.environment + else + environment = context + profile = nil + end + + blocks_to_expire = [] + if profile + profile.blocks.each {|block| + conditions = block.class.expire_on + blocks_to_expire << block unless (conditions[:profile] & causes).empty? + } + end + environment.blocks.each {|block| + conditions = block.class.expire_on + blocks_to_expire << block unless (conditions[:environment] & causes).empty? + } + + blocks_to_expire.uniq! + BlockSweeper.expire_blocks(blocks_to_expire) + end + end diff --git a/app/models/article_block.rb b/app/models/article_block.rb index a369d9e..c878f0d 100644 --- a/app/models/article_block.rb +++ b/app/models/article_block.rb @@ -63,4 +63,9 @@ class ArticleBlock < Block end settings_items :visualization_format, :type => :string, :default => 'short' + + def self.expire_on + { :profile => [:article], :environment => [:article] } + end + end diff --git a/app/models/block.rb b/app/models/block.rb index 5231153..819c11d 100644 --- a/app/models/block.rb +++ b/app/models/block.rb @@ -133,7 +133,7 @@ class Block < ActiveRecord::Base def cache_key(language='en') active_record_cache_key+'-'+language end - + def timeout 4.hours end @@ -142,4 +142,15 @@ class Block < ActiveRecord::Base false end + # Override in your subclasses. + # Define which events and context should cause the block cache to expire + # Possible events are: :article, :profile, :friendship, :category + # Possible contexts are: :profile, :environment + def self.expire_on + { + :profile => [], + :environment => [] + } + end + end diff --git a/app/models/blog_archives_block.rb b/app/models/blog_archives_block.rb index a557af5..92c4ae7 100644 --- a/app/models/blog_archives_block.rb +++ b/app/models/blog_archives_block.rb @@ -45,4 +45,7 @@ class BlogArchivesBlock < Block content_tag('div', link_to(_('Subscribe RSS Feed'), owner_blog.feed.url), :class => 'subscribe-feed') end + def self.expire_on + { :profile => [:article], :environment => [:article] } + end end diff --git a/app/models/categories_block.rb b/app/models/categories_block.rb index 9de3b39..ee372eb 100644 --- a/app/models/categories_block.rb +++ b/app/models/categories_block.rb @@ -35,4 +35,7 @@ class CategoriesBlock < Block end end + def self.expire_on + { :profile => [], :environment => [:category] } + end end diff --git a/app/models/recent_documents_block.rb b/app/models/recent_documents_block.rb index 42a99a8..46535a9 100644 --- a/app/models/recent_documents_block.rb +++ b/app/models/recent_documents_block.rb @@ -30,4 +30,7 @@ class RecentDocumentsBlock < Block end end + def self.expire_on + { :profile => [:article], :environment => [:article] } + end end diff --git a/app/models/tags_block.rb b/app/models/tags_block.rb index 8446b11..79a4665 100644 --- a/app/models/tags_block.rb +++ b/app/models/tags_block.rb @@ -59,4 +59,8 @@ class TagsBlock < Block 15.minutes end + def self.expire_on + { :profile => [:article], :environment => [:article] } + end + end diff --git a/app/sweepers/article_sweeper.rb b/app/sweepers/article_sweeper.rb index 5e11e82..924ba7b 100644 --- a/app/sweepers/article_sweeper.rb +++ b/app/sweepers/article_sweeper.rb @@ -15,16 +15,16 @@ class ArticleSweeper < ActiveRecord::Observer Article.find(article.parent_id_was).touch if article.parent_id_was end end - + + protected def expire_caches(article) + expire_blocks_cache(article.profile, [:article]) + return if !article.environment + article.hierarchy(true).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)}} - BlockSweeper.expire_blocks(blocks) env = article.profile.environment if env && (env.portal_community == article.profile) article.environment.locales.keys.each do |locale| diff --git a/app/sweepers/category_sweeper.rb b/app/sweepers/category_sweeper.rb index 4a69b76..8c06b8a 100644 --- a/app/sweepers/category_sweeper.rb +++ b/app/sweepers/category_sweeper.rb @@ -3,7 +3,11 @@ class CategorySweeper < ActiveRecord::Observer include SweeperHelper def after_save(category) - expire_fragment(category.environment.id.to_s + "_categories_menu") + # expire_fragment(category.environment.id.to_s + "_categories_menu") + expire_blocks_cache(category.environment, [:category]) end + def after_destroy(category) + expire_blocks_cache(category.environment, [:category]) + end end diff --git a/plugins/display_content/lib/display_content_block.rb b/plugins/display_content/lib/display_content_block.rb index 2f66d50..0983c69 100644 --- a/plugins/display_content/lib/display_content_block.rb +++ b/plugins/display_content/lib/display_content_block.rb @@ -83,4 +83,8 @@ class DisplayContentBlock < Block return parents end + def self.expire_on + { :profile => [:article], :environment => [:article] } + end + end diff --git a/test/unit/block_test.rb b/test/unit/block_test.rb index 5406049..24a2b3f 100644 --- a/test/unit/block_test.rb +++ b/test/unit/block_test.rb @@ -156,4 +156,13 @@ class BlockTest < ActiveSupport::TestCase assert_equal box.environment, block.environment end + should 'inform conditions for expiration on profile context' do + conditions = Block.expire_on + assert conditions[:profile].kind_of?(Array) + end + + should 'inform conditions for expiration on environment context' do + conditions = Block.expire_on + assert conditions[:environment].kind_of?(Array) + end end -- libgit2 0.21.2