summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlibi <aliby.bbb@gmail.com>2022-08-21 15:18:02 +0600
committerGitHub <noreply@github.com>2022-08-21 12:18:02 +0300
commit031b2087b57713d29b98140fb1f0b9f7b4fc7a21 (patch)
tree9426e49702885165479c2616b2cbce7b8207f521
parente95b05a8df4d45d0c8186ac37f57d9edd9235820 (diff)
downloadredis-py-031b2087b57713d29b98140fb1f0b9f7b4fc7a21.tar.gz
Add BITFIELD_RO (#2340)
-rw-r--r--redis/commands/core.py23
-rw-r--r--tests/test_asyncio/test_commands.py13
-rw-r--r--tests/test_commands.py13
3 files changed, 49 insertions, 0 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py
index 6cf0457..51f30d2 100644
--- a/redis/commands/core.py
+++ b/redis/commands/core.py
@@ -1504,6 +1504,29 @@ class BasicKeyCommands(CommandsProtocol):
"""
return BitFieldOperation(self, key, default_overflow=default_overflow)
+ def bitfield_ro(
+ self: Union["Redis", "AsyncRedis"],
+ key: KeyT,
+ encoding: str,
+ offset: BitfieldOffsetT,
+ items: Optional[list] = None,
+ ) -> ResponseT:
+ """
+ Return an array of the specified bitfield values
+ where the first value is found using ``encoding`` and ``offset``
+ parameters and remaining values are result of corresponding
+ encoding/offset pairs in optional list ``items``
+ Read-only variant of the BITFIELD command.
+
+ For more information see https://redis.io/commands/bitfield_ro
+ """
+ params = [key, "GET", encoding, offset]
+
+ items = items or []
+ for encoding, offset in items:
+ params.extend(["GET", encoding, offset])
+ return self.execute_command("BITFIELD_RO", *params)
+
def bitop(self, operation: str, dest: KeyT, *keys: KeyT) -> ResponseT:
"""
Perform a bitwise operation using ``operation`` between ``keys`` and
diff --git a/tests/test_asyncio/test_commands.py b/tests/test_asyncio/test_commands.py
index 4c25277..422328a 100644
--- a/tests/test_asyncio/test_commands.py
+++ b/tests/test_asyncio/test_commands.py
@@ -2953,6 +2953,19 @@ class TestRedisCommands:
)
assert resp == [0, None, 255]
+ @skip_if_server_version_lt("6.0.0")
+ async def test_bitfield_ro(self, r: redis.Redis):
+ bf = r.bitfield("a")
+ resp = await bf.set("u8", 8, 255).execute()
+ assert resp == [0]
+
+ resp = await r.bitfield_ro("a", "u8", 0)
+ assert resp == [0]
+
+ items = [("u4", 8), ("u4", 12), ("u4", 13)]
+ resp = await r.bitfield_ro("a", "u8", 0, items)
+ assert resp == [0, 15, 15, 14]
+
@skip_if_server_version_lt("4.0.0")
async def test_memory_stats(self, r: redis.Redis):
# put a key into the current db to make sure that "db.<current-db>"
diff --git a/tests/test_commands.py b/tests/test_commands.py
index f9134d8..de8020c 100644
--- a/tests/test_commands.py
+++ b/tests/test_commands.py
@@ -4438,6 +4438,19 @@ class TestRedisCommands:
)
assert resp == [0, None, 255]
+ @skip_if_server_version_lt("6.0.0")
+ def test_bitfield_ro(self, r: redis.Redis):
+ bf = r.bitfield("a")
+ resp = bf.set("u8", 8, 255).execute()
+ assert resp == [0]
+
+ resp = r.bitfield_ro("a", "u8", 0)
+ assert resp == [0]
+
+ items = [("u4", 8), ("u4", 12), ("u4", 13)]
+ resp = r.bitfield_ro("a", "u8", 0, items)
+ assert resp == [0, 15, 15, 14]
+
@skip_if_server_version_lt("4.0.0")
def test_memory_help(self, r):
with pytest.raises(NotImplementedError):