SoFunction
Updated on 2025-03-03

Redis execution lua script implementation

The redis EVAL command can execute lua scripts. Redis ensures the atomicity of script execution. The script is executed by the embedded execution engine (Lua 5.1 interpreter).

1. Syntax

EVAL script numkeys [key [key ...]] [arg [arg ...]]

The first parameter script is the source code of the script
The second parameter numkeys is the number of input key name parameters.
The following are key and parameters.

For example:

> EVAL "return 'Hello, scripting!'" 0
"Hello, scripting!"

Among them, "return 'Hello, scripting!'" is the lua script, and the 0 after that means there is no key and no parameters. You can see that what redis returns is the return value of the script.

Another way is to write a fixed script to pass parameters, such as

> EVAL "return ARGV[1]" 0 'hello scripting'
"hello scripting"
> EVAL "return ARGV[1]" 0 hello scripting
"hello"
> EVAL "return ARGV[3]" 0 hello scripting
(nil)

You can see that it is obtained from [1], instead of [0], the parameters are separated by spaces, and if they are out of range, they return nil.

An example of using key

> EVAL "return { KEYS[1], KEYS[2], ARGV[1], ARGV[2], ARGV[3] }" 2 key1 key2 arg1 arg2 arg3
1) "key1"
2) "key2"
3) "arg1"
4) "arg2"
5) "arg3"

You can see that this lua script can use {} to return multiple values. I don't know if this returns a string or a list in java.

2. Call the redis command

The Redis command can be called from a Lua script via () or (). Both are almost the same, except that the error generated when calling the () function will be returned directly to the client that executed the function. Instead, an error encountered when calling the () function will be returned to the execution context of the script.
example:

> EVAL "return ('SET', KEYS[1], ARGV[1])" 1 foo bar
OK

A simple call to a set command seems meaningless, but if there are too many, it will make sense to ensure atomicity, for example

> EVAL "local a=('SET',KEYS[1],ARGV[1]) local b=('SET',KEYS[2],ARGV[2]) return a and b" 2 k1 k2 v1 v2
OK

3. Script cache

> SCRIPT LOAD "return 'Immabe a cached script'"
"c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f"
> EVALSHA c664a3bf70bd1d45c4284ffebb65a6f2299bfc9f 0
"Immabe a cached script"

You can see that a script is cached using script load and returns an ID. After that, it can be called through evalsha ID.

As for the lua scripting language, I will become familiar with it after exploring and writing it. To give a few examples:
Get and delete

local value = ('get', KEYS[1]) if value then ('del', KEYS[1]) return value else return nil end

Explanation: Define a variable value. If the value of keys[1] is obtained, it means that the value is deleted and returns OK. Otherwise, it will return nil, and it can also be written as

if ('get', KEYS[1]) == ARGV[1] then return ('del', KEYS[1]) else return 0 end

It looks similar to a shell script.

This is the end of this article about the implementation of redis execution lua script. For more related redis execution lua script content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!