Scripting Commands¶
Execute Lua scripts on the Redis server.
eval¶
# EVAL script numkeys [key ...] [arg ...]
result = r.eval("return KEYS[1]", 1, "mykey") # 'mykey'
result = r.eval("return ARGV[1]", 0, "hello") # 'hello'
result = r.eval("return redis.call('GET', KEYS[1])", 1, "mykey")
evalsha¶
Execute a cached script by its SHA1 hash. Use with script_load to avoid sending the script body repeatedly.
script_load¶
Best practices¶
Use EVALSHA in production
Load scripts once with script_load, then call evalsha. This saves bandwidth and parsing time on repeated calls.
Use KEYS and ARGV correctly
Always pass key names via KEYS[] (not hardcoded in the script). This enables correct routing in Redis Cluster.