Redis executes Lua scripts with benefits of sample code

  • 2020-06-07 05:33:19
  • OfStack

preface

Redis introduced support for Lua scripting in version 2.6. By embedding the Lua environment in the server, Redis clients can use Lua scripting to execute multiple Redis commands directly on the server atom.

Where, the EVAL command can be used to directly evaluate the input script:


redis>EVAL "return 'hello world'" 0
"hello world"

The benefits of using scripts are as follows:

1. Reduce network overhead: The operation of the original five network requests can be completed with one request, and the logic of the original five requests can be completed on the redis server. Use scripts to reduce network round-trip latency.

2. Atomic operation: Redis will execute the entire script as a whole without being inserted by other commands.

3. Reuse: Scripts sent by the client are permanently stored in Redis, meaning that other clients can reuse the 1 script without having to use code to complete the same logic.

Redis executes the Lua script

To achieve 1 access frequency control, an ip frequently visits the page in a short period of time, and needs to record and detect, it can be effectively implemented through Lua script
On the redis client machine, create a new file ratelimiting.lua, which reads as follows


local times = redis.call('incr',KEYS[1])

if times == 1 then
 redis.call('expire',KEYS[1], ARGV[1])
end

if times > tonumber(ARGV[2]) then
 return 0
end
return 1

How do you test this script on an redis client machine? As follows:


redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3

-- The eval parameter tells ES47en-ES48en to read and run the following Lua script. ratelimiting.lua is the location of the script, followed by the parameters passed to the Lua script. rate. limiting:127.0.0.1 before "," is the key to be operated, which can be obtained by using KEYS[1] in the script. The 10 and 3 after "," are parameters, which can be obtained by using ARGV[1] and ARGV[2] in the script. Note: The blanks on both sides of "," cannot be omitted otherwise an error will occur

Combined with the contents of the script, it can be seen that the function of this command is to limit the access frequency to 3 times every 10 seconds at most, so running this command continuously in the terminal will find that 1 is returned when the access frequency is less than or equal to 3 times in 10 seconds, otherwise 0 is returned.

The test runs are as follows:


[root@rhel6 redis-learning]# redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3
(integer) 1
[root@rhel6 redis-learning]# redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3
(integer) 1
[root@rhel6 redis-learning]# redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3
(integer) 1
[root@rhel6 redis-learning]# redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3
(integer) 0
[root@rhel6 redis-learning]# redis-cli --eval ratelimiting.lua rate.limitingl:127.0.0.1 , 10 3
(integer) 0

Supplement:

Lua scripts are used in many games, mainly Lua scripts can be embedded into other programs to run, when the game upgrade, you can directly upgrade the script, without having to reinstall the game. For many levels of the game, just add the lua script, embed the Lua interpreter in the game, update the Lua script online, and then the game automatically downloads the latest levels. Many previous games, such as Angry Birds, have been achieved using Lua.

From The Redis Guide to Getting Started

conclusion


Related articles: