Fix #4129: Remove tag alias caching.
This commit is contained in:
@@ -13,19 +13,12 @@ class TagAliasCorrection
|
||||
|
||||
def statistics_hash
|
||||
@statistics_hash ||= {
|
||||
"antecedent_cache" => Cache.get("ta:" + Cache.hash(tag_alias.antecedent_name)),
|
||||
"consequent_cache" => Cache.get("ta:" + Cache.hash(tag_alias.consequent_name)),
|
||||
"antecedent_count" => Tag.find_by_name(tag_alias.antecedent_name).try(:post_count),
|
||||
"consequent_count" => Tag.find_by_name(tag_alias.consequent_name).try(:post_count)
|
||||
}
|
||||
end
|
||||
|
||||
def clear_cache
|
||||
tag_alias.clear_all_cache
|
||||
end
|
||||
|
||||
def fix!
|
||||
clear_cache
|
||||
tag_alias.delay(:queue => "default").update_posts
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,28 +1,10 @@
|
||||
class TagAlias < TagRelationship
|
||||
after_save :clear_all_cache
|
||||
after_destroy :clear_all_cache
|
||||
after_save :clear_all_cache, if: ->(rec) {rec.is_retired?}
|
||||
after_save :create_mod_action
|
||||
validates_uniqueness_of :antecedent_name, scope: :status, conditions: -> { active }
|
||||
validate :absence_of_transitive_relation
|
||||
validate :wiki_pages_present, on: :create, unless: :skip_secondary_validations
|
||||
validate :mininum_antecedent_count, on: :create, unless: :skip_secondary_validations
|
||||
|
||||
module CacheMethods
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
module ClassMethods
|
||||
def clear_cache_for(name)
|
||||
Cache.delete("ta:#{Cache.hash(name)}")
|
||||
end
|
||||
end
|
||||
|
||||
def clear_all_cache
|
||||
TagAlias.clear_cache_for(antecedent_name)
|
||||
TagAlias.clear_cache_for(consequent_name)
|
||||
end
|
||||
end
|
||||
|
||||
module ApprovalMethods
|
||||
def approve!(update_topic: true, approver: CurrentUser.user)
|
||||
CurrentUser.scoped(approver) do
|
||||
@@ -50,7 +32,6 @@ class TagAlias < TagRelationship
|
||||
end
|
||||
end
|
||||
|
||||
include CacheMethods
|
||||
include ApprovalMethods
|
||||
include ForumMethods
|
||||
|
||||
@@ -63,9 +44,9 @@ class TagAlias < TagRelationship
|
||||
end
|
||||
|
||||
def self.to_aliased(names)
|
||||
Cache.get_multi(Array(names), "ta") do |tag|
|
||||
ActiveRecord::Base.select_value_sql("select consequent_name from tag_aliases where status in ('active', 'processing') and antecedent_name = ?", tag) || tag.to_s
|
||||
end.values
|
||||
names = Array(names)
|
||||
aliases = active.where(antecedent_name: names).map { |ta| [ta.antecedent_name, ta.consequent_name] }.to_h
|
||||
names.map { |name| aliases[name] || name }
|
||||
end
|
||||
|
||||
def process!(update_topic: true)
|
||||
@@ -80,7 +61,6 @@ class TagAlias < TagRelationship
|
||||
update(status: "processing")
|
||||
move_aliases_and_implications
|
||||
move_saved_searches
|
||||
clear_all_cache
|
||||
ensure_category_consistency
|
||||
update_posts
|
||||
forum_updater.update(approval_message(approver), "APPROVED") if update_topic
|
||||
@@ -197,7 +177,6 @@ class TagAlias < TagRelationship
|
||||
|
||||
def reject!(update_topic: true)
|
||||
update(status: "deleted")
|
||||
clear_all_cache
|
||||
forum_updater.update(reject_message(CurrentUser.user), "REJECTED") if update_topic
|
||||
end
|
||||
|
||||
|
||||
@@ -16,17 +16,13 @@ class TagAliasCorrectionTest < ActiveSupport::TestCase
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
|
||||
context "with a bad cache and post counts" do
|
||||
context "with a bad post count" do
|
||||
setup do
|
||||
Cache.delete("ta:#{Cache.hash('bbb')}")
|
||||
Cache.put("ta:#{Cache.hash('aaa')}", "zzz")
|
||||
Tag.where(:name => "aaa").update_all("post_count = -3")
|
||||
@correction = TagAliasCorrection.new(@tag_alias.id)
|
||||
end
|
||||
|
||||
should "have the correct statistics hash" do
|
||||
assert_equal("zzz", @correction.statistics_hash["antecedent_cache"])
|
||||
assert_nil(@correction.statistics_hash["consequent_cache"])
|
||||
assert_equal(-3, @correction.statistics_hash["antecedent_count"])
|
||||
assert_equal(1, @correction.statistics_hash["consequent_count"])
|
||||
end
|
||||
@@ -44,11 +40,6 @@ class TagAliasCorrectionTest < ActiveSupport::TestCase
|
||||
context "that is fixed" do
|
||||
setup do
|
||||
@correction.fix!
|
||||
TagAlias.to_aliased(["aaa"])
|
||||
end
|
||||
|
||||
should "now have the correct cache" do
|
||||
assert_equal("bbb", Cache.get("ta:#{Cache.hash('aaa')}"))
|
||||
end
|
||||
|
||||
should "now have the correct count" do
|
||||
|
||||
@@ -106,22 +106,14 @@ class TagAliasTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
should "convert a tag to its normalized version" do
|
||||
tag1 = FactoryBot.create(:tag, :name => "aaa")
|
||||
tag2 = FactoryBot.create(:tag, :name => "bbb")
|
||||
ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
|
||||
normalized_tags = TagAlias.to_aliased(["aaa", "ccc"])
|
||||
assert_equal(["bbb", "ccc"], normalized_tags.sort)
|
||||
end
|
||||
tag1 = create(:tag, name: "aaa")
|
||||
tag2 = create(:tag, name: "bbb")
|
||||
ta = create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb")
|
||||
|
||||
should "update the cache" do
|
||||
tag1 = FactoryBot.create(:tag, :name => "aaa")
|
||||
tag2 = FactoryBot.create(:tag, :name => "bbb")
|
||||
ta = FactoryBot.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "bbb")
|
||||
assert_nil(Cache.get("ta:#{Cache.hash("aaa")}"))
|
||||
TagAlias.to_aliased(["aaa"])
|
||||
assert_equal("bbb", Cache.get("ta:#{Cache.hash("aaa")}"))
|
||||
ta.destroy
|
||||
assert_nil(Cache.get("ta:#{Cache.hash("aaa")}"))
|
||||
assert_equal(["bbb"], TagAlias.to_aliased("aaa"))
|
||||
assert_equal(["bbb", "ccc"], TagAlias.to_aliased(["aaa", "ccc"]))
|
||||
assert_equal(["ccc", "bbb"], TagAlias.to_aliased(["ccc", "bbb"]))
|
||||
assert_equal(["bbb", "bbb"], TagAlias.to_aliased(["aaa", "aaa"]))
|
||||
end
|
||||
|
||||
context "saved searches" do
|
||||
|
||||
Reference in New Issue
Block a user