Skip to content

Commit

Permalink
Add ability to clear namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
fatkodima committed Nov 12, 2022
1 parent f7094ed commit f4b0c6b
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## x.y.z

- Add ability to clear namespaces (#202). This operation can run for a very long time if the namespace contains
lots of keys! It should be used in tests, or when the namespace is small enough and you are sure you know what you are doing.

## 1.9.0

- Accept Proc as a namespace (#203)
Expand Down
31 changes: 31 additions & 0 deletions lib/redis/namespace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,32 @@ def eval(*args)
end
ruby2_keywords(:eval) if respond_to?(:ruby2_keywords, true)

# This operation can run for a very long time if the namespace contains lots of keys!
# It should be used in tests, or when the namespace is small enough
# and you are sure you know what you are doing.
def clear
if warning?
warn("This operation can run for a very long time if the namespace contains lots of keys! " +
"It should be used in tests, or when the namespace is small enough " +
"and you are sure you know what you are doing.")
end

batch_size = 1000

if supports_scan?
cursor = "0"
begin
cursor, keys = scan(cursor, count: batch_size)
del(*keys) unless keys.empty?
end until cursor == "0"
else
all_keys = keys("*")
all_keys.each_slice(batch_size) do |keys|
del(*keys)
end
end
end

ADMINISTRATIVE_COMMANDS.keys.each do |command|
define_method(command) do |*args, &block|
raise NoMethodError if deprecations?
Expand Down Expand Up @@ -591,5 +617,10 @@ def create_enumerator(&block)
Enumerator.new(&block)
end
end

def supports_scan?
redis_version = @redis.info["redis_version"]
Gem::Version.new(redis_version) >= Gem::Version.new("2.8.0")
end
end
end
26 changes: 26 additions & 0 deletions spec/redis_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1048,4 +1048,30 @@
expect(sub_sub_namespaced.full_namespace).to eql("ns:sub1:sub2")
end
end

describe :clear do
it "warns with helpful output" do
expect { @namespaced.clear }.to output(/can run for a very long time/).to_stderr
end

it "should delete all the keys" do
@redis.set("foo", "bar")
@namespaced.mset("foo1", "bar", "foo2", "bar")
capture_stderr { @namespaced.clear }

expect(@redis.keys).to eq ["foo"]
expect(@namespaced.keys).to be_empty
end

it "should delete all the keys in older redis" do
allow(@redis).to receive(:info).and_return({ "redis_version" => "2.7.0" })

@redis.set("foo", "bar")
@namespaced.mset("foo1", "bar", "foo2", "bar")
capture_stderr { @namespaced.clear }

expect(@redis.keys).to eq ["foo"]
expect(@namespaced.keys).to be_empty
end
end
end

0 comments on commit f4b0c6b

Please sign in to comment.