Usage
Parameters
The Lua script to execute.
Array of key names that the script will access. These are available in the script as
KEYS[1], KEYS[2], etc.Array of arguments to pass to the script. These are available in the script as
ARGV[1], ARGV[2], etc.Response
The value returned by the Lua script. The type depends on what the script returns.
Examples
Basic Script Execution
Incrementing a Counter
Conditional Set
Working with Multiple Keys
EVAL vs EVAL_RO
EVAL
Theeval command can execute scripts that modify data:
EVAL_RO (Read-Only)
UseevalRo for read-only scripts that don’t modify data. This is more efficient for read replicas:
- Can be executed on read replicas
- Are optimized for read-heavy workloads
- Will error if they attempt to modify data
Using the Script Class
For better performance with frequently used scripts, use theScript class which automatically handles SHA-1 caching:
Script class provides three methods:
eval(keys, args)- Always uses EVALevalsha(keys, args)- Always uses EVALSHA (requires script to be loaded)exec(keys, args)- Optimistically tries EVALSHA, falls back to EVAL if needed
createScript with the readOnly option:
See Also
- EVALSHA - Execute a cached script by SHA-1 hash
- SCRIPT LOAD - Load a script into the server cache
- FCALL - Call a Redis function