diff options
-rw-r--r-- | redis/commands/core.py | 22 | ||||
-rw-r--r-- | tests/test_scripting.py | 9 |
2 files changed, 29 insertions, 2 deletions
diff --git a/redis/commands/core.py b/redis/commands/core.py index 80fad1a..383c635 100644 --- a/redis/commands/core.py +++ b/redis/commands/core.py @@ -3947,7 +3947,12 @@ class ScriptCommands: """ return self._eval("EVAL_RO", script, numkeys, *keys_and_args) - def evalsha(self, 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: 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 @@ -3959,7 +3964,20 @@ class ScriptCommands: 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: str, numkeys: int, *keys_and_args: list) -> str: + """ + 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._evalsha("EVALSHA_RO", sha, numkeys, *keys_and_args) def script_exists(self, *args): """ diff --git a/tests/test_scripting.py b/tests/test_scripting.py index b0d76c7..f4671a5 100644 --- a/tests/test_scripting.py +++ b/tests/test_scripting.py @@ -74,6 +74,15 @@ class TestScripting: # 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) |