From 5bcdd19425dfa53be3d78296158d478cad43ea0c Mon Sep 17 00:00:00 2001 From: tleish Date: Wed, 15 Jun 2022 17:22:09 -0600 Subject: [PATCH 1/3] Kredis::Migration#delete_all array of keys --- lib/kredis/migration.rb | 10 ++++++++-- test/migration_test.rb | 10 +++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lib/kredis/migration.rb b/lib/kredis/migration.rb index fa12e68..b698317 100644 --- a/lib/kredis/migration.rb +++ b/lib/kredis/migration.rb @@ -31,8 +31,14 @@ def migrate(from:, to:, pipeline: nil) end def delete_all(key_pattern) - each_key_batch_matching(key_pattern) do |keys, pipeline| - pipeline.del *keys + log_migration "DELETE ALL #{key_pattern.inspect}" do + if key_pattern.is_a? Array + @redis.del *key_pattern + else + each_key_batch_matching(key_pattern) do |keys, pipeline| + pipeline.del *keys + end + end end end diff --git a/test/migration_test.rb b/test/migration_test.rb index 032385a..9310a75 100644 --- a/test/migration_test.rb +++ b/test/migration_test.rb @@ -51,11 +51,19 @@ class MigrationTest < ActiveSupport::TestCase end end - test "delete_all" do + test "delete_all with pattern" do 3.times { |index| Kredis.proxy("mykey:#{index}").set "hello there #{index}" } Kredis::Migration.delete_all "mykey:*" 3.times { |index| assert_nil Kredis.proxy("mykey:#{index}").get } end + + test "delete_all with keys" do + 3.times { |index| Kredis.proxy("mykey:#{index}").set "hello there #{index}" } + + Kredis::Migration.delete_all 3.times.map { |index| "mykey:#{index}" } + + 3.times { |index| assert_nil Kredis.proxy("mykey:#{index}").get } + end end From f31f749e900c6b8126d2f3d45f72ab3b586b856a Mon Sep 17 00:00:00 2001 From: tleish Date: Wed, 15 Jun 2022 17:37:01 -0600 Subject: [PATCH 2/3] Kredis::Migration#delete_all array of keys --- lib/kredis/migration.rb | 18 +++++++++--------- test/migration_test.rb | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/kredis/migration.rb b/lib/kredis/migration.rb index b698317..db8a94d 100644 --- a/lib/kredis/migration.rb +++ b/lib/kredis/migration.rb @@ -9,8 +9,8 @@ def initialize(config = :shared) @copy_sha = @redis.script "load", "redis.call('SETNX', KEYS[2], redis.call('GET', KEYS[1])); return 1;" end - def migrate_all(key_pattern) - each_key_batch_matching(key_pattern) do |keys, pipeline| + def migrate_all(key_patterns) + each_key_batch_matching(key_patterns) do |keys, pipeline| keys.each do |key| ids = key.scan(/\d+/).map(&:to_i) migrate from: key, to: yield(key, *ids), pipeline: pipeline @@ -30,12 +30,12 @@ def migrate(from:, to:, pipeline: nil) end end - def delete_all(key_pattern) - log_migration "DELETE ALL #{key_pattern.inspect}" do - if key_pattern.is_a? Array - @redis.del *key_pattern + def delete_all(*key_patterns) + log_migration "DELETE ALL #{key_patterns.inspect}" do + if key_patterns.length > 1 + @redis.del *key_patterns else - each_key_batch_matching(key_pattern) do |keys, pipeline| + each_key_batch_matching(key_patterns.first) do |keys, pipeline| pipeline.del *keys end end @@ -45,10 +45,10 @@ def delete_all(key_pattern) private SCAN_BATCH_SIZE = 1_000 - def each_key_batch_matching(key_pattern, &block) + def each_key_batch_matching(key_patterns, &block) cursor = "0" begin - cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE) + cursor, keys = @redis.scan(cursor, match: key_patterns, count: SCAN_BATCH_SIZE) @redis.multi { |pipeline| yield keys, pipeline } end until cursor == "0" end diff --git a/test/migration_test.rb b/test/migration_test.rb index 9310a75..40b9c5a 100644 --- a/test/migration_test.rb +++ b/test/migration_test.rb @@ -62,7 +62,7 @@ class MigrationTest < ActiveSupport::TestCase test "delete_all with keys" do 3.times { |index| Kredis.proxy("mykey:#{index}").set "hello there #{index}" } - Kredis::Migration.delete_all 3.times.map { |index| "mykey:#{index}" } + Kredis::Migration.delete_all *3.times.map { |index| "mykey:#{index}" } 3.times { |index| assert_nil Kredis.proxy("mykey:#{index}").get } end From d61b0ea5752597dfb8dd72bcede0f5245716cb9b Mon Sep 17 00:00:00 2001 From: tleish Date: Wed, 15 Jun 2022 17:39:47 -0600 Subject: [PATCH 3/3] Kredis::Migration#delete_all array of keys --- lib/kredis/migration.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/kredis/migration.rb b/lib/kredis/migration.rb index db8a94d..9b24a30 100644 --- a/lib/kredis/migration.rb +++ b/lib/kredis/migration.rb @@ -9,8 +9,8 @@ def initialize(config = :shared) @copy_sha = @redis.script "load", "redis.call('SETNX', KEYS[2], redis.call('GET', KEYS[1])); return 1;" end - def migrate_all(key_patterns) - each_key_batch_matching(key_patterns) do |keys, pipeline| + def migrate_all(key_pattern) + each_key_batch_matching(key_pattern) do |keys, pipeline| keys.each do |key| ids = key.scan(/\d+/).map(&:to_i) migrate from: key, to: yield(key, *ids), pipeline: pipeline @@ -45,10 +45,10 @@ def delete_all(*key_patterns) private SCAN_BATCH_SIZE = 1_000 - def each_key_batch_matching(key_patterns, &block) + def each_key_batch_matching(key_pattern, &block) cursor = "0" begin - cursor, keys = @redis.scan(cursor, match: key_patterns, count: SCAN_BATCH_SIZE) + cursor, keys = @redis.scan(cursor, match: key_pattern, count: SCAN_BATCH_SIZE) @redis.multi { |pipeline| yield keys, pipeline } end until cursor == "0" end