Skip to content

Commit 7f7f4f6

Browse files
authored
Add support for EVALSHA_RO (#1863)
* add evalsha-ro * fix pr comment * add type hints * add type hints
1 parent 58b28e4 commit 7f7f4f6

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

redis/commands/core.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,12 @@ def eval_ro(self, script: str, numkeys: int, *keys_and_args: list) -> str:
39473947
"""
39483948
return self._eval("EVAL_RO", script, numkeys, *keys_and_args)
39493949

3950-
def evalsha(self, sha, numkeys, *keys_and_args):
3950+
def _evalsha(
3951+
self, command: str, sha: str, numkeys: int, *keys_and_args: list
3952+
) -> str:
3953+
return self.execute_command(command, sha, numkeys, *keys_and_args)
3954+
3955+
def evalsha(self, sha: str, numkeys: int, *keys_and_args: list) -> str:
39513956
"""
39523957
Use the ``sha`` to execute a Lua script already registered via EVAL
39533958
or SCRIPT LOAD. Specify the ``numkeys`` the script will touch and the
@@ -3959,7 +3964,20 @@ def evalsha(self, sha, numkeys, *keys_and_args):
39593964
39603965
For more information check https://redis.io/commands/evalsha
39613966
"""
3962-
return self.execute_command("EVALSHA", sha, numkeys, *keys_and_args)
3967+
return self._evalsha("EVALSHA", sha, numkeys, *keys_and_args)
3968+
3969+
def evalsha_ro(self, sha: str, numkeys: int, *keys_and_args: list) -> str:
3970+
"""
3971+
The read-only variant of the EVALSHA command
3972+
3973+
Use the ``sha`` to execute a read-only Lua script already registered via EVAL
3974+
or SCRIPT LOAD. Specify the ``numkeys`` the script will touch and the
3975+
key names and argument values in ``keys_and_args``. Returns the result
3976+
of the script.
3977+
3978+
For more information check https://redis.io/commands/evalsha_ro
3979+
"""
3980+
return self._evalsha("EVALSHA_RO", sha, numkeys, *keys_and_args)
39633981

39643982
def script_exists(self, *args):
39653983
"""

tests/test_scripting.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ def test_evalsha(self, r):
7474
# 2 * 3 == 6
7575
assert r.evalsha(sha, 1, "a", 3) == 6
7676

77+
# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
78+
def test_evalsha_ro(self, unstable_r):
79+
unstable_r.set("a", "b")
80+
get_sha = unstable_r.script_load("return redis.call('GET', KEYS[1])")
81+
del_sha = unstable_r.script_load("return redis.call('DEL', KEYS[1])")
82+
assert unstable_r.evalsha_ro(get_sha, 1, "a") == b"b"
83+
with pytest.raises(redis.ResponseError):
84+
unstable_r.evalsha_ro(del_sha, 1, "a")
85+
7786
def test_evalsha_script_not_loaded(self, r):
7887
r.set("a", 2)
7988
sha = r.script_load(multiply_script)

0 commit comments

Comments
 (0)