Skip to content

Commit 589a4e7

Browse files
authored
Merge pull request #982 from rhymes/rhymes/redis-62-lpop-rpop-count-argument
[Redis 6.2] Add count argument to lpop and rpop
2 parents fe62c45 + 5eee83f commit 589a4e7

File tree

3 files changed

+42
-14
lines changed

3 files changed

+42
-14
lines changed

lib/redis.rb

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1172,23 +1172,29 @@ def rpushx(key, value)
11721172
end
11731173
end
11741174

1175-
# Remove and get the first element in a list.
1175+
# Remove and get the first elements in a list.
11761176
#
11771177
# @param [String] key
1178-
# @return [String]
1179-
def lpop(key)
1178+
# @param [Integer] count number of elements to remove
1179+
# @return [String, Array<String>] the values of the first elements
1180+
def lpop(key, count = nil)
11801181
synchronize do |client|
1181-
client.call([:lpop, key])
1182+
command = [:lpop, key]
1183+
command << count if count
1184+
client.call(command)
11821185
end
11831186
end
11841187

1185-
# Remove and get the last element in a list.
1188+
# Remove and get the last elements in a list.
11861189
#
11871190
# @param [String] key
1188-
# @return [String]
1189-
def rpop(key)
1191+
# @param [Integer] count number of elements to remove
1192+
# @return [String, Array<String>] the values of the last elements
1193+
def rpop(key, count = nil)
11901194
synchronize do |client|
1191-
client.call([:rpop, key])
1195+
command = [:rpop, key]
1196+
command << count if count
1197+
client.call(command)
11921198
end
11931199
end
11941200

lib/redis/distributed.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,14 +413,14 @@ def rpushx(key, value)
413413
node_for(key).rpushx(key, value)
414414
end
415415

416-
# Remove and get the first element in a list.
417-
def lpop(key)
418-
node_for(key).lpop(key)
416+
# Remove and get the first elements in a list.
417+
def lpop(key, count = nil)
418+
node_for(key).lpop(key, count)
419419
end
420420

421-
# Remove and get the last element in a list.
422-
def rpop(key)
423-
node_for(key).rpop(key)
421+
# Remove and get the last elements in a list.
422+
def rpop(key, count = nil)
423+
node_for(key).rpop(key, count)
424424
end
425425

426426
# Remove the last element in a list, append it to another list and return

test/lint/lists.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,17 @@ def test_lpop
119119
assert_equal 1, r.llen("foo")
120120
end
121121

122+
def test_lpop_count
123+
target_version("6.2") do
124+
r.rpush "foo", "s1"
125+
r.rpush "foo", "s2"
126+
127+
assert_equal 2, r.llen("foo")
128+
assert_equal ["s1", "s2"], r.lpop("foo", 2)
129+
assert_equal 0, r.llen("foo")
130+
end
131+
end
132+
122133
def test_rpop
123134
r.rpush "foo", "s1"
124135
r.rpush "foo", "s2"
@@ -128,6 +139,17 @@ def test_rpop
128139
assert_equal 1, r.llen("foo")
129140
end
130141

142+
def test_rpop_count
143+
target_version("6.2") do
144+
r.rpush "foo", "s1"
145+
r.rpush "foo", "s2"
146+
147+
assert_equal 2, r.llen("foo")
148+
assert_equal ["s2", "s1"], r.rpop("foo", 2)
149+
assert_equal 0, r.llen("foo")
150+
end
151+
end
152+
131153
def test_linsert
132154
r.rpush "foo", "s1"
133155
r.rpush "foo", "s3"

0 commit comments

Comments
 (0)