Redis › Lua Scripting
How do you call other Redis commands from within a Lua script, and what restrictions apply to this?
IntermediateAnswer
You call other Redis commands from within a Lua script using the redis.call or redis.pcall functions, passing the command name and its arguments, and while you have access to most standard Redis commands this way, certain commands that could introduce non determinism or block the server for a long time are restricted or behave differently within a script's context.
local current = redis.call('get', KEYS[1])
if current == false then
redis.call('set', KEYS[1], ARGV[1])
endReal-world example An inventory management script checks whether a product's stock key already exists using redis.call, only initializing it with a default value if it does not already exist, all within a single atomic script execution.