Redis is used in conjunction with LUA

preamble:

The principle is to redis affairs affairs in order first into the queue, when the client submitted the exec command, redis will queue each command executed in order again. If the transaction before executing exec interrupted, then all commands will not be executed; if after exec command execution, then all the commands are executed sequentially. But if redis be forced to shut down during the execution of a transaction, then you need to use redis-check-aof redis tools to repair, remove those portions of the command execution. Redis to ensure a positive during script execution script, or any other script commands can not be executed. It is this property atoms, script can alternatively MULTI / EXEC as transaction using

 

A: Redis the benefits of using the LUA

1: reduce network overhead, in Lua script can put multiple commands in the same script runs.

2: atomic operations, the Redis entire script will be executed as a whole, can not be inserted into the middle of other commands. In other words, the process of writing the script without worrying about race conditions occur.

3: reusability, script sent by the client will always be stored in Redis, which means that other clients can reuse this script to accomplish the same logic.

 

Two: Redis + LUA integrated use

EVAL command input script defines a Lua function, and then execute the script by executing this function.

EVALSHA by constructing the function name, a direct call Lua functions have been defined in order to perform the appropriate script.

 

EVAL 命令: EVAL script numkeys key [key ...] arg [arg ...]

(1) script parameters: a period of Lua script, it will be run in the context of Redis server, this script does not (and should not) be defined as a Lua function.

(2) numkeys parameters: a number of parameters specified key.

(3) key [key ...] parameters: the third argument of EVAL since the beginning of the use of numkeys keys (key), indicate that Redis key (key) in the script are used, these parameters keys KEYS array by the global variable in Lua, a base address 1 in the form of access (KEYS [1], KEYS [2] ···).

(4) arg [arg ...] parameters: the array can be accessed through the global variable ARGV in Lua, and a form accessible KEYS variable similarly (ARGV [1], ARGV [2] ···).

 

EVALSHA command

1) EVAL command requires that each time you execute the script sends a body of the script (script body).

2) Redis has an internal caching mechanism, so it will not always have to re-compile the script, but on many occasions, to pay unnecessary bandwidth to transmit script body is not the best choice.

3) In order to reduce bandwidth consumption, Redis realizes EVALSHA command, and its role as EVAL, are used to evaluate a script, but the first argument is not acceptable to script it, but SHA1 digest script.

Performance 4) EVALSHA command is as follows

(1) If the server still remember a given SHA1 checksum script specified, then the implementation of the script

(2) If the server does not remember a given SHA1 checksum script specified, it returns a special error, alert the user instead of using EVAL EVALSHA

5) So, how do we get the script SHA1 digest it? Here we look at SCRIPT command:

(1) SCRIPT FLUSH: clear all cache script.

(2) SCRIPT EXISTS: checking the given script, the script checks whether the specified script is present in the cache.

(3) SCRIPT LOAD: a script into the script caches, return SHA1 digest, but does not run it immediately.

(4) SCRIPT KILL: kills the script currently running.

 

 

 

 

Published 50 original articles · won praise 2 · Views 2287

Guess you like

Origin blog.csdn.net/eafun_888/article/details/104714595