summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorshacharPash <shachar.pashchur@redis.com>2023-05-15 17:07:46 +0300
committershacharPash <shachar.pashchur@redis.com>2023-05-15 17:07:46 +0300
commitd690e179fe8f7684ca0c19faf5585d7bd44a3b83 (patch)
tree8f67c0c5dbb13ef60948a461879e7222ff5033ca
parent5dc0be3040e6ffe9a0505c9f9d1bb721f152a85c (diff)
downloadredis-py-d690e179fe8f7684ca0c19faf5585d7bd44a3b83.tar.gz
Support JSON.MSET command
-rw-r--r--redis/commands/json/__init__.py1
-rw-r--r--redis/commands/json/commands.py19
-rw-r--r--tests/test_json.py9
3 files changed, 29 insertions, 0 deletions
diff --git a/redis/commands/json/__init__.py b/redis/commands/json/__init__.py
index 77fb21c..4f2a0c5 100644
--- a/redis/commands/json/__init__.py
+++ b/redis/commands/json/__init__.py
@@ -38,6 +38,7 @@ class JSON(JSONCommands):
"JSON.GET": self._decode,
"JSON.MGET": bulk_of_jsons(self._decode),
"JSON.SET": lambda r: r and nativestr(r) == "OK",
+ "JSON.MSET": lambda r: r and nativestr(r) == "OK",
"JSON.MERGE": lambda r: r and nativestr(r) == "OK",
"JSON.NUMINCRBY": self._decode,
"JSON.NUMMULTBY": self._decode,
diff --git a/redis/commands/json/commands.py b/redis/commands/json/commands.py
index 5da9245..42e3ef2 100644
--- a/redis/commands/json/commands.py
+++ b/redis/commands/json/commands.py
@@ -253,6 +253,25 @@ class JSONCommands:
pieces.append("XX")
return self.execute_command("JSON.SET", *pieces)
+ def mset(self, name: str, path: str, obj: JsonType, *items) -> Optional[str]:
+ """
+ Set the JSON value at key ``name`` under the ``path`` to ``obj`` for one or more keys.
+ ``items`` accepts a list of additional key/path/value to set.
+
+ For the purpose of using this within a pipeline, this command is also
+ aliased to JSON.MSET.
+
+ For more information see `JSON.MSET <https://redis.io/commands/json.mset>`_.
+ """
+
+ pieces = [name, path, self._encode(obj)]
+
+ for key, path, value in zip(*[iter(items)] * 3):
+ pieces.extend([key, path, self._encode(value)])
+
+ return self.execute_command("JSON.MSET", *pieces)
+
+
def merge(
self,
name: str,
diff --git a/tests/test_json.py b/tests/test_json.py
index 0a85998..ed448a0 100644
--- a/tests/test_json.py
+++ b/tests/test_json.py
@@ -116,6 +116,15 @@ def test_mgetshouldsucceed(client):
@pytest.mark.redismod
+@skip_ifmodversion_lt("2.6.0", "ReJSON") # todo: update after the release
+def test_mset(client):
+ client.json().mset("1", Path.root_path(), 1, "2", Path.root_path(), 2)
+
+ assert client.json().mget(["1"], Path.root_path()) == [1]
+ assert client.json().mget(["1", "2"], Path.root_path()) == [1, 2]
+
+
+@pytest.mark.redismod
@skip_ifmodversion_lt("99.99.99", "ReJSON") # todo: update after the release
def test_clear(client):
client.json().set("arr", Path.root_path(), [0, 1, 2, 3, 4])