tag change notices: use cache instead of using redis directly.
This commit is contained in:
@@ -1,18 +1,13 @@
|
||||
module TagChangeNoticeService
|
||||
extend self
|
||||
|
||||
def redis_client
|
||||
::Redis.new(url: Danbooru.config.redis_url)
|
||||
end
|
||||
|
||||
def get_forum_topic_id(tag)
|
||||
redis_client.get("tcn:#{tag}")
|
||||
Cache.get("tcn:#{tag}")
|
||||
end
|
||||
|
||||
def update_cache(affected_tags, forum_topic_id)
|
||||
rc = redis_client
|
||||
affected_tags.each do |tag|
|
||||
rc.setex("tcn:#{tag}", 1.week, forum_topic_id)
|
||||
Cache.put("tcn:#{tag}", forum_topic_id, 1.week)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -37,28 +37,27 @@ class BulkUpdateRequestTest < ActiveSupport::TestCase
|
||||
|
||||
context "#update_notice" do
|
||||
setup do
|
||||
@mock_redis = MockRedis.new
|
||||
@forum_topic = FactoryBot.create(:forum_topic)
|
||||
TagChangeNoticeService.stubs(:redis_client).returns(@mock_redis)
|
||||
end
|
||||
|
||||
should "update redis" do
|
||||
should "update the cache" do
|
||||
@script = "create alias aaa -> 000\n" +
|
||||
"create implication bbb -> 111\n" +
|
||||
"remove alias ccc -> 222\n" +
|
||||
"remove implication ddd -> 333\n" +
|
||||
"mass update eee -> 444\n"
|
||||
FactoryBot.create(:bulk_update_request, script: @script, forum_topic: @forum_topic)
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:aaa"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:000"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:bbb"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:111"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:ccc"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:222"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:ddd"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:333"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:eee"))
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:444"))
|
||||
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:aaa"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:000"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:bbb"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:111"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:ccc"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:222"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:ddd"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:333"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:eee"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:444"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -71,14 +71,12 @@ class TagAliasTest < ActiveSupport::TestCase
|
||||
|
||||
context "#update_notice" do
|
||||
setup do
|
||||
@mock_redis = MockRedis.new
|
||||
@forum_topic = FactoryBot.create(:forum_topic)
|
||||
TagChangeNoticeService.stubs(:redis_client).returns(@mock_redis)
|
||||
end
|
||||
|
||||
should "update redis" do
|
||||
should "update the cache" do
|
||||
FactoryBot.create(:tag_alias, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: true, forum_topic: @forum_topic)
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:aaa"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:aaa"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -67,14 +67,12 @@ class TagImplicationTest < ActiveSupport::TestCase
|
||||
|
||||
context "#update_notice" do
|
||||
setup do
|
||||
@mock_redis = MockRedis.new
|
||||
@forum_topic = FactoryBot.create(:forum_topic)
|
||||
TagChangeNoticeService.stubs(:redis_client).returns(@mock_redis)
|
||||
end
|
||||
|
||||
should "update redis" do
|
||||
should "update the cache" do
|
||||
FactoryBot.create(:tag_implication, antecedent_name: "aaa", consequent_name: "bbb", skip_secondary_validations: true, forum_topic: @forum_topic)
|
||||
assert_equal(@forum_topic.id.to_s, @mock_redis.get("tcn:aaa"))
|
||||
assert_equal(@forum_topic.id, Cache.get("tcn:aaa"))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user