Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Add a count parameter to lpop/rpop for redis >= 6.2.0 (redis/redis-py#1487)

Signed-off-by: Andrew-Chen-Wang <acwangpython@gmail.com>
  • Loading branch information
Andrew-Chen-Wang committed Oct 5, 2021
1 parent 293d90f commit 961da8c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 6 deletions.
30 changes: 24 additions & 6 deletions aioredis/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2318,9 +2318,18 @@ def llen(self, name: KeyT) -> Awaitable:
"""Return the length of the list ``name``"""
return self.execute_command("LLEN", name)

def lpop(self, name: KeyT) -> Awaitable:
"""Remove and return the first item of the list ``name``"""
return self.execute_command("LPOP", name)
def lpop(self, name: KeyT, count: Optional[int] = None) -> Awaitable:
"""
Removes and returns the first elements of the list ``name``.
By default, the command pops a single element from the beginning of
the list. When provided with the optional ``count`` argument, the reply
will consist of up to count elements, depending on the list's length.
"""
if count is not None:
return self.execute_command("LPOP", name, count)
else:
return self.execute_command("LPOP", name)

def lpush(self, name: KeyT, *values: EncodableT) -> Awaitable:
"""Push ``values`` onto the head of the list ``name``"""
Expand Down Expand Up @@ -2366,9 +2375,18 @@ def ltrim(self, name: KeyT, start: int, end: int) -> Awaitable:
"""
return self.execute_command("LTRIM", name, start, end)

def rpop(self, name: KeyT) -> Awaitable:
"""Remove and return the last item of the list ``name``"""
return self.execute_command("RPOP", name)
def rpop(self, name: KeyT, count: Optional[int] = None) -> Awaitable:
"""
Removes and returns the last elements of the list ``name``.
By default, the command pops a single element from the end of the list.
When provided with the optional ``count`` argument, the reply will
consist of up to count elements, depending on the list's length.
"""
if count is not None:
return self.execute_command("RPOP", name, count)
else:
return self.execute_command("RPOP", name)

def rpoplpush(self, src: KeyT, dst: KeyT) -> Awaitable:
"""
Expand Down
16 changes: 16 additions & 0 deletions tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,14 @@ async def test_lpop(self, r: aioredis.Redis):
assert await r.lpop("a") == b"3"
assert await r.lpop("a") is None

@skip_if_server_version_lt("6.2.0")
async def test_lpop_count(self, r: aioredis.Redis):
await r.rpush('a', '1', '2', '3')
assert await r.lpop('a', 2) == [b'1', b'2']
assert await r.lpop('a', 1) == [b'3']
assert await r.lpop('a') is None
assert await r.lpop('a', 3) is None

async def test_lpush(self, r: aioredis.Redis):
assert await r.lpush("a", "1") == 1
assert await r.lpush("a", "2") == 2
Expand Down Expand Up @@ -1239,6 +1247,14 @@ async def test_rpop(self, r: aioredis.Redis):
assert await r.rpop("a") == b"1"
assert await r.rpop("a") is None

@skip_if_server_version_lt('6.2.0')
async def test_rpop_count(self, r: aioredis.Redis):
await r.rpush('a', '1', '2', '3')
assert await r.rpop('a', 2) == [b'3', b'2']
assert await r.rpop('a', 1) == [b'1']
assert await r.rpop('a') is None
assert await r.rpop('a', 3) is None

async def test_rpoplpush(self, r: aioredis.Redis):
await r.rpush("a", "a1", "a2", "a3")
await r.rpush("b", "b1", "b2", "b3")
Expand Down

0 comments on commit 961da8c

Please sign in to comment.