Commit ef9e77580d5ad74368785dc6df0b5a2f3700063e

Authored by Moises Machado
Committed by Antonio Terceiro
1 parent 76fd7b7c

ActionItem1126: expire relevant caches

created a profile observer that is called by a callback in the model as workaround to some dependencies cycles in environment.rb
  changed the per_page number to be a Noosfero constant instead of a ProfileController class method
app/controllers/public/profile_controller.rb
... ... @@ -105,11 +105,6 @@ class ProfileController < PublicController
105 105 end
106 106  
107 107 def per_page
108   - self.class.per_page
109   - end
110   - class << self
111   - def per_page
112   - 10
113   - end
  108 + Noosfero::Constants::PROFILE_PER_PAGE
114 109 end
115 110 end
... ...
app/helpers/sweeper_helper.rb
... ... @@ -8,4 +8,39 @@ module SweeperHelper
8 8 ActionController::Base.new().expire_timeout_fragment(*args)
9 9 end
10 10  
  11 + def expire_friends(profile)
  12 + # public friends page
  13 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1
  14 + (1..pages).each do |i|
  15 + expire_timeout_fragment(profile.friends_cache_key(:npage => i.to_s))
  16 + end
  17 + # manage friends page
  18 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1
  19 + (1..pages).each do |i|
  20 + expire_timeout_fragment(profile.manage_friends_cache_key(:npage => i.to_s))
  21 + end
  22 +
  23 + # friends blocks
  24 + blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)}
  25 + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
  26 + end
  27 +
  28 + def expire_communities(profile)
  29 + # public communities page
  30 + pages = profile.communities.count / Noosfero::Constants::PROFILE_PER_PAGE + 1
  31 + (1..pages).each do |i|
  32 + expire_timeout_fragment(profile.communities_cache_key(:npage => i.to_s))
  33 + end
  34 +
  35 + # communities block
  36 + blocks = profile.blocks.select{|b| b.kind_of?(CommunitiesBlock)}
  37 + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
  38 + end
  39 +
  40 + def expire_enterprises(profile)
  41 + # enterprises and favorite enterprises blocks
  42 + blocks = profile.blocks.select {|b| [EnterprisesBlock, FavoriteEnterprisesBlock].any?{|klass| b.kind_of?(klass)} }
  43 + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)}
  44 + end
  45 +
11 46 end
... ...
app/models/profile.rb
... ... @@ -633,4 +633,9 @@ class Profile &lt; ActiveRecord::Base
633 633 end
634 634 end
635 635  
  636 + # FIXME: horrible workaround to circular dependancy in environment.rb
  637 + after_update do |profile|
  638 + ProfileSweeper.new().after_update(profile)
  639 + end
  640 +
636 641 end
... ...
app/sweepers/friendship_sweeper.rb
... ... @@ -19,12 +19,12 @@ protected
19 19  
20 20 def expire_cache(profile)
21 21 # public friends page
22   - pages = profile.friends.count / ProfileController.per_page + 1
  22 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1
23 23 (1..pages).each do |i|
24 24 expire_timeout_fragment(profile.friends_cache_key(:npage => i.to_s))
25 25 end
26 26 # manage friends page
27   - pages = profile.friends.count / FriendsController.per_page + 1
  27 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1
28 28 (1..pages).each do |i|
29 29 expire_timeout_fragment(profile.manage_friends_cache_key(:npage => i.to_s))
30 30 end
... ...
app/sweepers/profile_sweeper.rb 0 → 100644
... ... @@ -0,0 +1,22 @@
  1 +# This is not a proper observer since is explicitly called in the profile model
  2 +class ProfileSweeper # < ActiveRecord::Observer
  3 +# observe :profile
  4 + include SweeperHelper
  5 +
  6 + def after_update(profile)
  7 + expire_caches(profile)
  8 + end
  9 +
  10 +protected
  11 +
  12 + def expire_caches(profile)
  13 + profile.members.each do |member|
  14 + expire_communities(member) if profile.community?
  15 + expire_enterprises(member) if profile.enterprise?
  16 + end
  17 +
  18 + profile.blocks.each do |block|
  19 + expire_timeout_fragment(block.cache_keys)
  20 + end
  21 + end
  22 +end
... ...
app/sweepers/role_assignment_sweeper.rb
... ... @@ -18,7 +18,8 @@ protected
18 18 end
19 19  
20 20 def expire_cache(profile)
21   - profile.cache_keys(:per_page => ProfileController.per_page).each { |ck|
  21 + per_page = Noosfero::Constants::PROFILE_PER_PAGE
  22 + profile.cache_keys(:per_page => per_page).each { |ck|
22 23 expire_timeout_fragment(ck)
23 24 }
24 25  
... ...
lib/noosfero/constants.rb
1 1 module Noosfero::Constants
2 2 EMAIL_FORMAT = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
3 3 INTEGER_FORMAT = /\A\d*\Z/i
  4 + PROFILE_PER_PAGE = 10
4 5 end
... ...