From ef9e77580d5ad74368785dc6df0b5a2f3700063e Mon Sep 17 00:00:00 2001 From: Moises Machado Date: Fri, 31 Jul 2009 17:36:42 -0300 Subject: [PATCH] ActionItem1126: expire relevant caches --- app/controllers/public/profile_controller.rb | 7 +------ app/helpers/sweeper_helper.rb | 35 +++++++++++++++++++++++++++++++++++ app/models/profile.rb | 5 +++++ app/sweepers/friendship_sweeper.rb | 4 ++-- app/sweepers/profile_sweeper.rb | 22 ++++++++++++++++++++++ app/sweepers/role_assignment_sweeper.rb | 3 ++- lib/noosfero/constants.rb | 1 + 7 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 app/sweepers/profile_sweeper.rb diff --git a/app/controllers/public/profile_controller.rb b/app/controllers/public/profile_controller.rb index 2ba8f7f..036b638 100644 --- a/app/controllers/public/profile_controller.rb +++ b/app/controllers/public/profile_controller.rb @@ -105,11 +105,6 @@ class ProfileController < PublicController end def per_page - self.class.per_page - end - class << self - def per_page - 10 - end + Noosfero::Constants::PROFILE_PER_PAGE end end diff --git a/app/helpers/sweeper_helper.rb b/app/helpers/sweeper_helper.rb index 155a897..6b4bd9e 100644 --- a/app/helpers/sweeper_helper.rb +++ b/app/helpers/sweeper_helper.rb @@ -8,4 +8,39 @@ module SweeperHelper ActionController::Base.new().expire_timeout_fragment(*args) end + def expire_friends(profile) + # public friends page + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1 + (1..pages).each do |i| + expire_timeout_fragment(profile.friends_cache_key(:npage => i.to_s)) + end + # manage friends page + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1 + (1..pages).each do |i| + expire_timeout_fragment(profile.manage_friends_cache_key(:npage => i.to_s)) + end + + # friends blocks + blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)} + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} + end + + def expire_communities(profile) + # public communities page + pages = profile.communities.count / Noosfero::Constants::PROFILE_PER_PAGE + 1 + (1..pages).each do |i| + expire_timeout_fragment(profile.communities_cache_key(:npage => i.to_s)) + end + + # communities block + blocks = profile.blocks.select{|b| b.kind_of?(CommunitiesBlock)} + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} + end + + def expire_enterprises(profile) + # enterprises and favorite enterprises blocks + blocks = profile.blocks.select {|b| [EnterprisesBlock, FavoriteEnterprisesBlock].any?{|klass| b.kind_of?(klass)} } + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} + end + end diff --git a/app/models/profile.rb b/app/models/profile.rb index cb5091b..bccab2e 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -633,4 +633,9 @@ class Profile < ActiveRecord::Base end end + # FIXME: horrible workaround to circular dependancy in environment.rb + after_update do |profile| + ProfileSweeper.new().after_update(profile) + end + end diff --git a/app/sweepers/friendship_sweeper.rb b/app/sweepers/friendship_sweeper.rb index 175cafe..c165da7 100644 --- a/app/sweepers/friendship_sweeper.rb +++ b/app/sweepers/friendship_sweeper.rb @@ -19,12 +19,12 @@ protected def expire_cache(profile) # public friends page - pages = profile.friends.count / ProfileController.per_page + 1 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1 (1..pages).each do |i| expire_timeout_fragment(profile.friends_cache_key(:npage => i.to_s)) end # manage friends page - pages = profile.friends.count / FriendsController.per_page + 1 + pages = profile.friends.count / Noosfero::Constants::PROFILE_PER_PAGE + 1 (1..pages).each do |i| expire_timeout_fragment(profile.manage_friends_cache_key(:npage => i.to_s)) end diff --git a/app/sweepers/profile_sweeper.rb b/app/sweepers/profile_sweeper.rb new file mode 100644 index 0000000..1e4ddbb --- /dev/null +++ b/app/sweepers/profile_sweeper.rb @@ -0,0 +1,22 @@ +# This is not a proper observer since is explicitly called in the profile model +class ProfileSweeper # < ActiveRecord::Observer +# observe :profile + include SweeperHelper + + def after_update(profile) + expire_caches(profile) + end + +protected + + def expire_caches(profile) + profile.members.each do |member| + expire_communities(member) if profile.community? + expire_enterprises(member) if profile.enterprise? + end + + profile.blocks.each do |block| + expire_timeout_fragment(block.cache_keys) + end + end +end diff --git a/app/sweepers/role_assignment_sweeper.rb b/app/sweepers/role_assignment_sweeper.rb index a4cea59..32b47e7 100644 --- a/app/sweepers/role_assignment_sweeper.rb +++ b/app/sweepers/role_assignment_sweeper.rb @@ -18,7 +18,8 @@ protected end def expire_cache(profile) - profile.cache_keys(:per_page => ProfileController.per_page).each { |ck| + per_page = Noosfero::Constants::PROFILE_PER_PAGE + profile.cache_keys(:per_page => per_page).each { |ck| expire_timeout_fragment(ck) } diff --git a/lib/noosfero/constants.rb b/lib/noosfero/constants.rb index fdb4b0a..b587fd4 100644 --- a/lib/noosfero/constants.rb +++ b/lib/noosfero/constants.rb @@ -1,4 +1,5 @@ module Noosfero::Constants EMAIL_FORMAT = /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i INTEGER_FORMAT = /\A\d*\Z/i + PROFILE_PER_PAGE = 10 end -- libgit2 0.21.2