Commit 2abc4514919f3452241b259087e034c854f3f3e6
1 parent
cf21bd07
Exists in
staging
and in
42 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 | 44 | def expire_profile_index(profile) |
| 45 | 45 | expire_timeout_fragment(profile.relationships_cache_key) |
| 46 | 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 | 73 | end | ... | ... |
app/models/article_block.rb
app/models/block.rb
| ... | ... | @@ -133,7 +133,7 @@ class Block < ActiveRecord::Base |
| 133 | 133 | def cache_key(language='en') |
| 134 | 134 | active_record_cache_key+'-'+language |
| 135 | 135 | end |
| 136 | - | |
| 136 | + | |
| 137 | 137 | def timeout |
| 138 | 138 | 4.hours |
| 139 | 139 | end |
| ... | ... | @@ -142,4 +142,15 @@ class Block < ActiveRecord::Base |
| 142 | 142 | false |
| 143 | 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 | 156 | end | ... | ... |
app/models/blog_archives_block.rb
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 | 15 | Article.find(article.parent_id_was).touch if article.parent_id_was |
| 16 | 16 | end |
| 17 | 17 | end |
| 18 | - | |
| 18 | + | |
| 19 | + | |
| 19 | 20 | protected |
| 20 | 21 | |
| 21 | 22 | def expire_caches(article) |
| 23 | + expire_blocks_cache(article.profile, [:article]) | |
| 24 | + | |
| 22 | 25 | return if !article.environment |
| 26 | + | |
| 23 | 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 | 28 | env = article.profile.environment |
| 29 | 29 | if env && (env.portal_community == article.profile) |
| 30 | 30 | article.environment.locales.keys.each do |locale| | ... | ... |
app/sweepers/category_sweeper.rb
| ... | ... | @@ -3,7 +3,11 @@ class CategorySweeper < ActiveRecord::Observer |
| 3 | 3 | include SweeperHelper |
| 4 | 4 | |
| 5 | 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 | 8 | end |
| 8 | 9 | |
| 10 | + def after_destroy(category) | |
| 11 | + expire_blocks_cache(category.environment, [:category]) | |
| 12 | + end | |
| 9 | 13 | end | ... | ... |
plugins/display_content/lib/display_content_block.rb
test/unit/block_test.rb
| ... | ... | @@ -156,4 +156,13 @@ class BlockTest < ActiveSupport::TestCase |
| 156 | 156 | assert_equal box.environment, block.environment |
| 157 | 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 | 168 | end | ... | ... |