Concept Flow - Lua script syntax in Redis
Start Script
Define Variables
Use Redis Commands
Return Result
End Script
A Lua script in Redis starts, defines variables, runs Redis commands, returns a result, and ends.
local val = redis.call('GET', KEYS[1]) if val == false then return 'No value' end return val
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call redis.call('GET', KEYS[1]) | KEYS[1] = 'mykey' | val = 'hello' |
| 2 | Check if val == false | val = 'hello' | Condition false |
| 3 | Return val | val = 'hello' | Output: 'hello' |
| 4 | End script | Script ends |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| val | nil | 'hello' | 'hello' | 'hello' |
Lua scripts in Redis:
- Use redis.call('COMMAND', args) to run Redis commands
- KEYS and ARGV hold input keys and arguments
- Use local variables to store results
- Return a value with return statement
- redis.call returns false if key missing