Skip to content

Commit fa5c501

Browse files
committed
Add async mode for flushdb/flushall
1 parent 8694faa commit fa5c501

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

lib/redis.rb

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,19 +226,31 @@ def debug(*args)
226226

227227
# Remove all keys from all databases.
228228
#
229+
# @param [Hash] options
230+
# - `:async => Boolean`: async flush (default: false)
229231
# @return [String] `OK`
230-
def flushall
232+
def flushall(options = nil)
231233
synchronize do |client|
232-
client.call([:flushall])
234+
if options && options[:async]
235+
client.call([:flushall, :async])
236+
else
237+
client.call([:flushall])
238+
end
233239
end
234240
end
235241

236242
# Remove all keys from the current database.
237243
#
244+
# @param [Hash] options
245+
# - `:async => Boolean`: async flush (default: false)
238246
# @return [String] `OK`
239-
def flushdb
247+
def flushdb(options = nil)
240248
synchronize do |client|
241-
client.call([:flushdb])
249+
if options && options[:async]
250+
client.call([:flushdb, :async])
251+
else
252+
client.call([:flushdb])
253+
end
242254
end
243255
end
244256

test/commands_on_value_types_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ def test_dbsize
8181
end
8282

8383
def test_flushdb
84+
# Test defaults
8485
r.set("foo", "s1")
8586
r.set("bar", "s2")
8687

@@ -89,12 +90,51 @@ def test_flushdb
8990
r.flushdb
9091

9192
assert_equal 0, r.dbsize
93+
94+
# Test sync
95+
r.set("foo", "s1")
96+
r.set("bar", "s2")
97+
98+
assert_equal 2, r.dbsize
99+
100+
r.flushdb(:async => false)
101+
102+
assert_equal 0, r.dbsize
103+
104+
# Test async
105+
target_version "3.9.101" do
106+
r.set("foo", "s1")
107+
r.set("bar", "s2")
108+
109+
assert_equal 2, r.dbsize
110+
111+
r.flushdb(:async => true)
112+
113+
assert_equal 0, r.dbsize
114+
115+
redis_mock(:flushdb => lambda { |args| "+FLUSHDB #{args.upcase}" }) do |redis|
116+
assert_equal "FLUSHDB ASYNC", redis.flushdb(:async => true)
117+
end
118+
end
92119
end
93120

94121
def test_flushall
122+
# Test defaults
95123
redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
96124
assert_equal "FLUSHALL", redis.flushall
97125
end
126+
127+
# Test sync
128+
redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
129+
assert_equal "FLUSHALL", redis.flushall(:async => false)
130+
end
131+
132+
# Test async
133+
target_version "3.9.101" do
134+
redis_mock(:flushall => lambda { |args| "+FLUSHALL #{args.upcase}" }) do |redis|
135+
assert_equal "FLUSHALL ASYNC", redis.flushall(:async => true)
136+
end
137+
end
98138
end
99139

100140
def test_migrate

0 commit comments

Comments
 (0)