1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix NoMethodError on ActiveSupport::Cache::RedisCacheStore#clear with Redis::Distributed

This commit is contained in:
Akihito Tsukamoto 2020-01-10 15:25:38 +09:00
parent 7abcba6909
commit fd2bc5cee6
2 changed files with 19 additions and 4 deletions

View file

@ -246,10 +246,14 @@ module ActiveSupport
pattern = namespace_key(matcher, options)
cursor = "0"
# Fetch keys in batches using SCAN to avoid blocking the Redis server.
begin
cursor, keys = c.scan(cursor, match: pattern, count: SCAN_BATCH_SIZE)
c.del(*keys) unless keys.empty?
end until cursor == "0"
nodes = c.respond_to?(:nodes) ? c.nodes : [c]
nodes.each do |node|
begin
cursor, keys = node.scan(cursor, match: pattern, count: SCAN_BATCH_SIZE)
node.del(*keys) unless keys.empty?
end until cursor == "0"
end
end
end
end

View file

@ -301,5 +301,16 @@ module ActiveSupport::Cache::RedisCacheStoreTests
assert_not @cache.exist?("foo")
assert @cache.redis.exists("fu")
end
test "clear all cache key with Redis::Distributed" do
cache = ActiveSupport::Cache::RedisCacheStore.new(
url: %w[redis://localhost:6379/0, redis://localhost:6379/1],
timeout: 0.1, namespace: @namespace, expires_in: 60, driver: DRIVER)
cache.write("foo", "bar")
cache.write("fu", "baz")
cache.clear
assert_not cache.exist?("foo")
assert_not cache.exist?("fu")
end
end
end