Commit 27667670d71957d09dbfd7a4247cfee510796bac
Committed by
Antonio Terceiro
1 parent
c1ffddd6
Exists in
master
and in
29 other branches
ActionItem990: adding friendship_sweeper and role_assignment_sweeper
Showing
2 changed files
with
74 additions
and
0 deletions
Show diff stats
... | ... | @@ -0,0 +1,36 @@ |
1 | +class FriendshipSweeper < ActiveRecord::Observer | |
2 | + observe :friendship | |
3 | + | |
4 | + def after_create(friendship) | |
5 | + expire_caches(friendship) | |
6 | + end | |
7 | + | |
8 | + def after_destroy(friendship) | |
9 | + expire_cache(friendship.person) | |
10 | + end | |
11 | + | |
12 | +protected | |
13 | + | |
14 | + def expire_caches(friendship) | |
15 | + expire_cache(friendship.person) | |
16 | + expire_cache(friendship.friend) | |
17 | + end | |
18 | + | |
19 | + def expire_cache(profile) | |
20 | + [profile.friends_cache_key, profile.manage_friends_cache_key].each { |ck| | |
21 | + cache_key = ck.gsub(/(.)-\d.*$/, '\1') | |
22 | + expire_fragment(/#{cache_key}/) | |
23 | + } | |
24 | + | |
25 | + blocks = profile.blocks.select{|b| b.kind_of?(FriendsBlock)} | |
26 | + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} | |
27 | + end | |
28 | + | |
29 | + def expire_fragment(*args) | |
30 | + ActionController::Base.new().expire_fragment(*args) | |
31 | + end | |
32 | + | |
33 | + def expire_timeout_fragment(*args) | |
34 | + ActionController::Base.new().expire_timeout_fragment(*args) | |
35 | + end | |
36 | +end | ... | ... |
... | ... | @@ -0,0 +1,38 @@ |
1 | +class RoleAssignmentSweeper < ActiveRecord::Observer | |
2 | + observe :role_assignment | |
3 | + | |
4 | + def after_create(role_assignment) | |
5 | + expire_caches(role_assignment) | |
6 | + end | |
7 | + | |
8 | + def after_destroy(role_assignment) | |
9 | + expire_caches(role_assignment) | |
10 | + end | |
11 | + | |
12 | +protected | |
13 | + | |
14 | + def expire_caches(role_assignment) | |
15 | + expire_cache(role_assignment.accessor) | |
16 | + expire_cache(role_assignment.resource) if role_assignment.resource.respond_to?(:cache_keys) | |
17 | + end | |
18 | + | |
19 | + def expire_cache(profile) | |
20 | + profile.cache_keys.each { |ck| | |
21 | + cache_key = ck.gsub(/(.)-\d.*$/, '\1') | |
22 | + expire_fragment(/#{cache_key}/) | |
23 | + } | |
24 | + | |
25 | + profile.blocks_to_expire_cache.each { |block| | |
26 | + blocks = profile.blocks.select{|b| b.kind_of?(block)} | |
27 | + blocks.map(&:cache_keys).each{|ck|expire_timeout_fragment(ck)} | |
28 | + } | |
29 | + end | |
30 | + | |
31 | + def expire_fragment(*args) | |
32 | + ActionController::Base.new().expire_fragment(*args) | |
33 | + end | |
34 | + | |
35 | + def expire_timeout_fragment(*args) | |
36 | + ActionController::Base.new().expire_timeout_fragment(*args) | |
37 | + end | |
38 | +end | ... | ... |