Skip to content

Commit 1428f0f

Browse files
committed
Make unlink consistent with del when an empty keyset passed
`del` returns zero immideately if no keys were passed, I think it makes sense to do the same for `unlink`? It simplifies and makes safer migration to unlinking
1 parent c205a8c commit 1428f0f

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/redis/commands/keys.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ def del(*keys)
261261
# @param [String, Array<String>] keys
262262
# @return [Integer] number of keys that were unlinked
263263
def unlink(*keys)
264+
keys.flatten!(1)
265+
return 0 if keys.empty?
266+
264267
send_command([:unlink] + keys)
265268
end
266269

test/redis/commands_on_value_types_test.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def test_unlink
4949

5050
assert_equal ["bar", "baz", "foo"], r.keys("*").sort
5151

52+
assert_equal 0, r.unlink("")
53+
54+
assert_equal ["bar", "baz", "foo"], r.keys("*").sort
55+
5256
assert_equal 1, r.unlink("foo")
5357

5458
assert_equal ["bar", "baz"], r.keys("*").sort
@@ -62,15 +66,24 @@ def test_unlink_with_array_argument
6266
r.set "foo", "s1"
6367
r.set "bar", "s2"
6468
r.set "baz", "s3"
69+
r.set "bad", "s4"
6570

66-
assert_equal ["bar", "baz", "foo"], r.keys("*").sort
71+
assert_equal ["bad", "bar", "baz", "foo"], r.keys("*").sort
72+
73+
assert_equal 0, r.unlink([])
74+
75+
assert_equal ["bad", "bar", "baz", "foo"], r.keys("*").sort
6776

6877
assert_equal 1, r.unlink(["foo"])
6978

70-
assert_equal ["bar", "baz"], r.keys("*").sort
79+
assert_equal ["bad", "bar", "baz"], r.keys("*").sort
7180

7281
assert_equal 2, r.unlink(["bar", "baz"])
7382

83+
assert_equal ["bad"], r.keys("*").sort
84+
85+
assert_equal 1, r.unlink([["bad"]])
86+
7487
assert_equal [], r.keys("*").sort
7588
end
7689

0 commit comments

Comments
 (0)