fix post count cache expiration across servers

This commit is contained in:
albert
2013-03-11 18:50:23 -04:00
parent 978e6ccaea
commit a6ed8bcaf1
2 changed files with 19 additions and 12 deletions

View File

@@ -317,13 +317,9 @@ class Post < ActiveRecord::Base
increment_tags = tag_array - tag_array_was increment_tags = tag_array - tag_array_was
execute_sql("UPDATE tags SET post_count = post_count - 1 WHERE name IN (?)", decrement_tags) if decrement_tags.any? execute_sql("UPDATE tags SET post_count = post_count - 1 WHERE name IN (?)", decrement_tags) if decrement_tags.any?
execute_sql("UPDATE tags SET post_count = post_count + 1 WHERE name IN (?)", increment_tags) if increment_tags.any? execute_sql("UPDATE tags SET post_count = post_count + 1 WHERE name IN (?)", increment_tags) if increment_tags.any?
decrement_tags.each do |tag| Post.expire_cache_for_all(decrement_tags)
expire_cache(tag) Post.expire_cache_for_all(increment_tags)
end Post.expire_cache_for_all([""])
increment_tags.each do |tag|
expire_cache(tag)
end
expire_cache("")
end end
def set_tag_counts def set_tag_counts
@@ -651,11 +647,20 @@ class Post < ActiveRecord::Base
end end
module CacheMethods module CacheMethods
def expire_cache(tag_name) def expire_cache_for_all(tag_names)
Danbooru.config.all_server_hosts.each do |host|
delay(:queue => host).expire_cache(tag_names)
end
end
def expire_cache(tag_names)
tag_names.each do |tag_name|
Cache.delete(Post.count_cache_key(tag_name))
end
if Post.fast_count("").to_i < 1000 if Post.fast_count("").to_i < 1000
Cache.delete(Post.count_cache_key("")) Cache.delete(Post.count_cache_key(""))
end end
Cache.delete(Post.count_cache_key(tag_name))
end end
end end
@@ -765,7 +770,7 @@ class Post < ActiveRecord::Base
update_parent_on_destroy update_parent_on_destroy
decrement_tag_post_counts decrement_tag_post_counts
update_column(:parent_id, nil) update_column(:parent_id, nil)
tag_array.each {|x| expire_cache(x)} Post.expire_cache_for_all(tag_array)
unless options[:without_mod_action] unless options[:without_mod_action]
ModAction.create(:description => "deleted post ##{id}") ModAction.create(:description => "deleted post ##{id}")
@@ -782,7 +787,7 @@ class Post < ActiveRecord::Base
self.is_deleted = false self.is_deleted = false
self.approver_id = CurrentUser.id self.approver_id = CurrentUser.id
save save
tag_array.each {|x| expire_cache(x)} Post.expire_cache_for_all(tag_array)
update_parent_on_save update_parent_on_save
ModAction.create(:description => "undeleted post ##{id}") ModAction.create(:description => "undeleted post ##{id}")
end end
@@ -1005,7 +1010,7 @@ class Post < ActiveRecord::Base
include PoolMethods include PoolMethods
include VoteMethods include VoteMethods
extend CountMethods extend CountMethods
include CacheMethods extend CacheMethods
include ParentMethods include ParentMethods
include DeletionMethods include DeletionMethods
include VersionMethods include VersionMethods

View File

@@ -6,6 +6,7 @@ class PostTest < ActiveSupport::TestCase
CurrentUser.user = @user CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1" CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all MEMCACHE.flush_all
Delayed::Worker.delay_jobs = false
end end
teardown do teardown do
@@ -360,6 +361,7 @@ class PostTest < ActiveSupport::TestCase
@post.update_attribute(:tag_string, "art:abc") @post.update_attribute(:tag_string, "art:abc")
@post = Post.find(@post.id) @post = Post.find(@post.id)
@post.update_attribute(:tag_string, "copy:abc") @post.update_attribute(:tag_string, "copy:abc")
@post.reload
end end
teardown do teardown do