File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 22
33## x.y.z
44
5+ - Add ability to clear namespaces (#202 ). This operation can run for a very long time if the namespace contains
6+ 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.
7+
58## 1.9.0
69
710- Accept Proc as a namespace (#203 )
Original file line number Diff line number Diff line change @@ -338,6 +338,32 @@ def eval(*args)
338338 end
339339 ruby2_keywords ( :eval ) if respond_to? ( :ruby2_keywords , true )
340340
341+ # This operation can run for a very long time if the namespace contains lots of keys!
342+ # It should be used in tests, or when the namespace is small enough
343+ # and you are sure you know what you are doing.
344+ def clear
345+ if warning?
346+ warn ( "This operation can run for a very long time if the namespace contains lots of keys! " +
347+ "It should be used in tests, or when the namespace is small enough " +
348+ "and you are sure you know what you are doing." )
349+ end
350+
351+ batch_size = 1000
352+
353+ if supports_scan?
354+ cursor = "0"
355+ begin
356+ cursor , keys = scan ( cursor , count : batch_size )
357+ del ( *keys ) unless keys . empty?
358+ end until cursor == "0"
359+ else
360+ all_keys = keys ( "*" )
361+ all_keys . each_slice ( batch_size ) do |keys |
362+ del ( *keys )
363+ end
364+ end
365+ end
366+
341367 ADMINISTRATIVE_COMMANDS . keys . each do |command |
342368 define_method ( command ) do |*args , &block |
343369 raise NoMethodError if deprecations?
@@ -599,5 +625,10 @@ def create_enumerator(&block)
599625 Enumerator . new ( &block )
600626 end
601627 end
628+
629+ def supports_scan?
630+ redis_version = @redis . info [ "redis_version" ]
631+ Gem ::Version . new ( redis_version ) >= Gem ::Version . new ( "2.8.0" )
632+ end
602633 end
603634end
Original file line number Diff line number Diff line change 10531053 expect ( sub_sub_namespaced . full_namespace ) . to eql ( "ns:sub1:sub2" )
10541054 end
10551055 end
1056+
1057+ describe :clear do
1058+ it "warns with helpful output" do
1059+ expect { @namespaced . clear } . to output ( /can run for a very long time/ ) . to_stderr
1060+ end
1061+
1062+ it "should delete all the keys" do
1063+ @redis . set ( "foo" , "bar" )
1064+ @namespaced . mset ( "foo1" , "bar" , "foo2" , "bar" )
1065+ capture_stderr { @namespaced . clear }
1066+
1067+ expect ( @redis . keys ) . to eq [ "foo" ]
1068+ expect ( @namespaced . keys ) . to be_empty
1069+ end
1070+
1071+ it "should delete all the keys in older redis" do
1072+ allow ( @redis ) . to receive ( :info ) . and_return ( { "redis_version" => "2.7.0" } )
1073+
1074+ @redis . set ( "foo" , "bar" )
1075+ @namespaced . mset ( "foo1" , "bar" , "foo2" , "bar" )
1076+ capture_stderr { @namespaced . clear }
1077+
1078+ expect ( @redis . keys ) . to eq [ "foo" ]
1079+ expect ( @namespaced . keys ) . to be_empty
1080+ end
1081+ end
10561082end
You can’t perform that action at this time.
0 commit comments