Skip to content

Commit

Permalink
Add support for EVALSHA_RO (#1863)
Browse files Browse the repository at this point in the history
* add evalsha-ro

* fix pr comment

* add type hints

* add type hints
  • Loading branch information
dvora-h committed Feb 2, 2022
1 parent 58b28e4 commit 7f7f4f6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
22 changes: 20 additions & 2 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3947,7 +3947,12 @@ def eval_ro(self, script: str, numkeys: int, *keys_and_args: list) -> str:
"""
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
Expand All @@ -3959,7 +3964,20 @@ 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: 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):
"""
Expand Down
9 changes: 9 additions & 0 deletions tests/test_scripting.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,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)
Expand Down

0 comments on commit 7f7f4f6

Please sign in to comment.