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 EVAL_RO #1862

Merged
merged 10 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
12 changes: 12 additions & 0 deletions redis/commands/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -3894,6 +3894,18 @@ def eval(self, script, numkeys, *keys_and_args):
"""
return self.execute_command("EVAL", script, numkeys, *keys_and_args)

def eval_ro(self, script, numkeys, *keys_and_args):
"""
The read-only variant of the EVAL command

Execute the read-only Lue ``script`` specifying 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/eval_ro
"""
return self.execute_command("EVAL_RO", script, numkeys, *keys_and_args)

def evalsha(self, sha, numkeys, *keys_and_args):
"""
Use the ``sha`` to execute a Lua script already registered via EVAL
Expand Down
8 changes: 8 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 @@ -31,6 +32,13 @@ def test_eval(self, r):
# 2 * 3 == 6
assert r.eval(multiply_script, 1, "a", 3) == 6

# @skip_if_server_version_lt("7.0.0") turn on after redis 7 release
def test_eval_ro(self, unstable_r):
unstable_r.set("a", "b")
assert unstable_r.eval_ro("return redis.call('GET', KEYS[1])", 1, "a") == b"b"
with pytest.raises(redis.ResponseError):
unstable_r.eval_ro("return redis.call('DEL', KEYS[1])", 1, "a")

@skip_if_server_version_lt("6.2.0")
def test_script_flush_620(self, r):
r.set("a", 2)
Expand Down