Regarding redis, everyone often uses it, and the most used scenario is data caching.
review
Starting with redis 2.6.0, redis has a built-in Lua interpreter and provides an eval command to parse the Lua script evaluation.
Syntax: eval script numkeys keys args
Parameters:
eval — Redis provides commands to parse the lua script
script — lua script
numkeys — Specifies the number of keys in the keyname parameter set
keys — The keyname parameter set, represented by the global variable KEYS array, starting with a subscript of 1
args — A set of key-value parameters, represented by an array of global variables ARGV, starting with a subscript of 1
Advantages of using Lua in Redis
Reduce network overhead. Multiple requests can be sent at once in the form of scripts to reduce network latency Atomic manipulation. redis will execute the entire script as a whole, with no other commands inserted in between. Therefore, there is no need to worry about race conditions and transactions during the scripting process. Reinstated. The footsteps sent by the client are persistently stored in redis so that other clients can reuse the script without having to use code to complete the same logic.
Script atomicity
Lua scripts cannot have time-consuming operations or dead loops, otherwise redis will not accept other commands and execute to stop the script running
Redis uses a single Lua interpreter to run all scripts, and ensures that the scripts are executed atomically.This means that when a script is running, no other scripts or redis commands will be executed! Therefore, if the current script is running slowly, the server may not be able to execute the command because it is busy, such as:
Each script has a maximum execution time limit, the default value is 5s. The maximum execution time is controlled by the lua-time-limit option of the configuration file redis.conf, or directly by using the config get and config set commands. When a script execution reaches its maximum execution time, Redis does not actively terminate it, it performs the following steps:
(1) Redis records that a script is running out of time
(2) Redis starts to re-accept requests from other clients, but only accepts the execution of script kill commands and shutdown nosave.
(3) If the script only performs read operations, use the script kill command to stop the script immediately; If the script performs a write operation, only the shutdown save/nosave command is allowed to stop the server to prevent the current data from being written to disk. (At this point, the server is down and the data will not be saved)
example
Execute the script, the parameters are 2 key and value, and the command is as follows:
Deadloop scripts, executing the following script will cause redis to be unable to process other commands and get stuck:
Try using a script to add data of type string with the following command:
Execute some more complex scripts, if the value of the key is equal to the value we passed in, then delete the cache, otherwise do anything, the command is as follows:
The results of the implementation are as follows:
(End)
|