Skip to content

Commit 55f2578

Browse files
authored
Merge pull request #706 from Napolskih/3.3
Add async mode for flushdb/flushall
2 parents 2bbd367 + fa5c501 commit 55f2578

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
@@ -237,19 +237,31 @@ def debug(*args)
237237

238238
# Remove all keys from all databases.
239239
#
240+
# @param [Hash] options
241+
# - `:async => Boolean`: async flush (default: false)
240242
# @return [String] `OK`
241-
def flushall
243+
def flushall(options = nil)
242244
synchronize do |client|
243-
client.call([:flushall])
245+
if options && options[:async]
246+
client.call([:flushall, :async])
247+
else
248+
client.call([:flushall])
249+
end
244250
end
245251
end
246252

247253
# Remove all keys from the current database.
248254
#
255+
# @param [Hash] options
256+
# - `:async => Boolean`: async flush (default: false)
249257
# @return [String] `OK`
250-
def flushdb
258+
def flushdb(options = nil)
251259
synchronize do |client|
252-
client.call([:flushdb])
260+
if options && options[:async]
261+
client.call([:flushdb, :async])
262+
else
263+
client.call([:flushdb])
264+
end
253265
end
254266
end
255267

test/commands_on_value_types_test.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ def test_dbsize
7979
end
8080

8181
def test_flushdb
82+
# Test defaults
8283
r.set("foo", "s1")
8384
r.set("bar", "s2")
8485

@@ -87,12 +88,51 @@ def test_flushdb
8788
r.flushdb
8889

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

92119
def test_flushall
120+
# Test defaults
93121
redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
94122
assert_equal "FLUSHALL", redis.flushall
95123
end
124+
125+
# Test sync
126+
redis_mock(:flushall => lambda { "+FLUSHALL" }) do |redis|
127+
assert_equal "FLUSHALL", redis.flushall(:async => false)
128+
end
129+
130+
# Test async
131+
target_version "3.9.101" do
132+
redis_mock(:flushall => lambda { |args| "+FLUSHALL #{args.upcase}" }) do |redis|
133+
assert_equal "FLUSHALL ASYNC", redis.flushall(:async => true)
134+
end
135+
end
96136
end
97137

98138
def test_migrate

0 commit comments

Comments
 (0)