Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for EVALSHA_RO #1863

Merged
merged 4 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions tests/test_scripting.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import pytest

import redis
from redis import exceptions
from tests.conftest import skip_if_server_version_lt

Expand Down Expand Up @@ -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)
Expand Down