From 91c6e886b3c55321a14f3022e4c35d71d559e555 Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 5 Jan 2022 15:25:02 +0200 Subject: [PATCH 1/4] add evalsha-ro --- redis/commands/core.py | 13 +++++++++++++ tests/test_scripting.py | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/redis/commands/core.py b/redis/commands/core.py index 4f0accd957..6e13df69f6 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3908,6 +3908,19 @@ def evalsha(self, sha, numkeys, *keys_and_args): """ return self.execute_command("EVALSHA", sha, numkeys, *keys_and_args) + def evalsha_ro(self, sha, numkeys, *keys_and_args): + """ + The read-only variant of the EVALSHA command + + Use the ``sha`` to execute a read-only Lua script already registered via EVAL + or SCRIPT LOAD. Specify the ``numkeys`` the script will touch and the + key names and argument values in ``keys_and_args``. Returns the result + of the script. + + For more information check https://redis.io/commands/evalsha_ro + """ + return self.execute_command("EVALSHA_RO", sha, numkeys, *keys_and_args) + def script_exists(self, *args): """ Check if a script exists in the script cache by specifying the SHAs of diff --git a/tests/test_scripting.py b/tests/test_scripting.py index 9f4f82023f..b216e5377a 100644 --- a/tests/test_scripting.py +++ b/tests/test_scripting.py @@ -1,5 +1,6 @@ import pytest +import redis from redis import exceptions from tests.conftest import skip_if_server_version_lt @@ -66,6 +67,15 @@ def test_evalsha(self, r): # 2 * 3 == 6 assert r.evalsha(sha, 1, "a", 3) == 6 + # @skip_if_server_version_lt("7.0.0") turn on after redis 7 release + def test_evalsha_ro(self, unstable_r): + unstable_r.set("a", "b") + get_sha = unstable_r.script_load("return redis.call('GET', KEYS[1])") + del_sha = unstable_r.script_load("return redis.call('DEL', KEYS[1])") + assert unstable_r.evalsha_ro(get_sha, 1, "a") == b"b" + with pytest.raises(redis.ResponseError): + unstable_r.evalsha_ro(del_sha, 1, "a") + def test_evalsha_script_not_loaded(self, r): r.set("a", 2) sha = r.script_load(multiply_script) From 675630568640d420ce73f044463d0326eb0b0ede Mon Sep 17 00:00:00 2001 From: dvora-h Date: Thu, 13 Jan 2022 14:19:16 +0200 Subject: [PATCH 2/4] fix pr comment --- redis/commands/core.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index 6e13df69f6..c8ec5c5621 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3894,6 +3894,9 @@ def eval(self, script, numkeys, *keys_and_args): """ return self.execute_command("EVAL", script, numkeys, *keys_and_args) + def _evalsha(self, command, sha, numkeys, *keys_and_args): + return self.execute_command(command, sha, numkeys, *keys_and_args) + def evalsha(self, sha, numkeys, *keys_and_args): """ Use the ``sha`` to execute a Lua script already registered via EVAL @@ -3906,7 +3909,7 @@ def evalsha(self, sha, numkeys, *keys_and_args): For more information check https://redis.io/commands/evalsha """ - return self.execute_command("EVALSHA", sha, numkeys, *keys_and_args) + return self._evalsha("EVALSHA", sha, numkeys, *keys_and_args) def evalsha_ro(self, sha, numkeys, *keys_and_args): """ @@ -3919,7 +3922,7 @@ def evalsha_ro(self, sha, numkeys, *keys_and_args): For more information check https://redis.io/commands/evalsha_ro """ - return self.execute_command("EVALSHA_RO", sha, numkeys, *keys_and_args) + return self._evalsha("EVALSHA_RO", sha, numkeys, *keys_and_args) def script_exists(self, *args): """ From 4a9e89c1e8273daee635359ad93c78177150344a Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 19 Jan 2022 03:29:03 +0200 Subject: [PATCH 3/4] add type hints --- redis/commands/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index c8ec5c5621..c0ad50f4c9 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3911,7 +3911,7 @@ def evalsha(self, sha, numkeys, *keys_and_args): """ return self._evalsha("EVALSHA", sha, numkeys, *keys_and_args) - def evalsha_ro(self, sha, numkeys, *keys_and_args): + def evalsha_ro(self, sha: str, numkeys: int, *keys_and_args: list) -> str: """ The read-only variant of the EVALSHA command From 102366548bdf5c4ccc2f1853e663e06ee882926c Mon Sep 17 00:00:00 2001 From: dvora-h Date: Wed, 19 Jan 2022 09:57:41 +0200 Subject: [PATCH 4/4] add type hints --- redis/commands/core.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/redis/commands/core.py b/redis/commands/core.py index c0ad50f4c9..6a21170a72 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3894,10 +3894,12 @@ def eval(self, script, numkeys, *keys_and_args): """ return self.execute_command("EVAL", script, numkeys, *keys_and_args) - def _evalsha(self, command, sha, numkeys, *keys_and_args): + def _evalsha( + self, command: str, sha: str, numkeys: int, *keys_and_args: list + ) -> str: return self.execute_command(command, sha, numkeys, *keys_and_args) - def evalsha(self, sha, numkeys, *keys_and_args): + def evalsha(self, sha: str, numkeys: int, *keys_and_args: list) -> str: """ Use the ``sha`` to execute a Lua script already registered via EVAL or SCRIPT LOAD. Specify the ``numkeys`` the script will touch and the