From 70e0d2fac20f5952dcf3d38f6a181c404674b06a Mon Sep 17 00:00:00 2001 From: evazion Date: Thu, 22 Aug 2019 23:24:17 -0500 Subject: [PATCH] tag change notices: use cache instead of using redis directly. --- app/logical/tag_change_notice_service.rb | 9 ++------- test/unit/bulk_update_request_test.rb | 25 ++++++++++++------------ test/unit/tag_alias_test.rb | 6 ++---- test/unit/tag_implication_test.rb | 6 ++---- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/app/logical/tag_change_notice_service.rb b/app/logical/tag_change_notice_service.rb index cb43b59ee..c7c5876b1 100644 --- a/app/logical/tag_change_notice_service.rb +++ b/app/logical/tag_change_notice_service.rb @@ -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 diff --git a/test/unit/bulk_update_request_test.rb b/test/unit/bulk_update_request_test.rb index ef69a7fb2..73ca62eec 100644 --- a/test/unit/bulk_update_request_test.rb +++ b/test/unit/bulk_update_request_test.rb @@ -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 diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index 5e245067d..36d600d80 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -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 diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index 1493e4793..31f41c7d6 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -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