Usage
Parameters
The SHA-1 hash of the script to execute. Obtain this by using SCRIPT LOAD or by computing the SHA-1 hash of your script.
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
Load and Execute a Script
Handling Missing Scripts
If the script isn’t loaded, EVALSHA will fail with a NOSCRIPT error:Conditional Operations
EVALSHA vs EVALSHA_RO
EVALSHA
Theevalsha command can execute scripts that modify data:
EVALSHA_RO (Read-Only)
UseevalshaRo for read-only scripts that don’t modify data:
- Can be executed on read replicas
- Are optimized for read-heavy workloads
- Will error if they attempt to modify data
Using the Script Class
The SDK provides aScript class that automatically handles SHA-1 computation and caching:
exec method is recommended as it:
- First tries EVALSHA (fast, using cached script)
- If script not found, falls back to EVAL
- Subsequent calls will use EVALSHA
Benefits of EVALSHA
- Reduced bandwidth: Only the SHA-1 hash is sent instead of the entire script
- Better performance: Script is pre-compiled and cached on the server
- Ideal for frequently executed scripts: Especially beneficial for scripts executed many times
See Also
- EVAL - Execute a Lua script
- SCRIPT LOAD - Load a script into the server cache