The Redis 2.6.0 built-in Lua script support allows you to run a lot of logic once on the server side of Redis.
- The entire script is in one transaction by default.
- All the keys involved in the script are passed in as far as possible from the outside, so that Redis knows what key you want to change from the beginning.
- Eval each time a full section of the script compares the bandwidth, you can load the script with script load, and return the hash value. Then execute with Evalhash.
- The built-in Lua library is also very intimate with Cjson, which can handle JSON strings.
Reference: http://www.searchdatabase.com.cn/showcontent_70423.htm
command to execute LUA in Redis with Eval, Evalsha, script load
Http://redis.readthedocs.org/en/latest/script/index.html
The EVAL command parameters for Redis are described below:
EVAL script Numkeys key [key ...] arg [arg ...]
The script parameter is a LUA 5.1 script that runs in the Redis server context, which does not have to (and should not) be defined as a LUA function.
The Numkeys parameter is used to specify the number of key name parameters.
Key Name parameter key [key ...] starting with the third parameter of EVAL, the Redis key (key) used in the script, these key name parameters can be accessed through the global variables keys array in Lua, in the form of a base address of 1 (keys[1], keys[ 2], etc.).
At the end of the command, those additional parameters that are not key-name parameters arg [arg ...] can be accessed in Lua via a global variable ARGV array, accessed in the same form as the keys variable (argv[1], argv[2], and so on).
These long lengths of instructions can be summed up in a simple example:
> eval "return {keys[1],keys[2],argv[1],argv[2]}" 2 Key1 Key2 First Second
1) "Key1"
2) "Key2"
3) "First"
4) "Second"
where "return {keys[1],keys[2],argv[1],argv[2]}" is an evaluated Lua script, the number 2 specifies the number of key name parameters, Key1 and Key2 are key name parameters, respectively, using keys[1] and keys[2] access, and the most The first and second are additional parameters that can be accessed through argv[1] and argv[2].
Reference: http://redis.readthedocs.org/en/latest/script/eval.html
Script LOAD Script
Script scripts are added to the script cache, but the script is not executed immediately.
The EVAL command will also add the script to the script cache, but it will immediately evaluate the input script.
If the given script is already in the cache, then do not do the action.
After the script is added to the cache, the script can be invoked with the Evalsha command, using the SHA1 checksum of the script.
Scripts can remain in the cache for an unlimited amount of time until the script FLUSH is executed.
redis> SCRIPT LOAD "return ' Hello Moto '" "232fd51614574cf0867b83d384a5e898cfd24e5a" redis> Evalsha 232fd51614574cf0867b83d384a5e898cfd24e5a 0 "Hello moto"
Evalsha SHA1 Numkeys key [key ...] arg [arg ...]
The script that is cached in the server is evaluated based on the given SHA1 checksum code.
Caching the script to the server can be done through the script LOAD command.
Other places of this command, such as the way parameters are passed in, are the same as the EVAL command.
The Lua scripting support for Redis