当前位置:网站首页>Redis learning notes - redis and Lua

Redis learning notes - redis and Lua

2022-06-23 09:11:00 Love Guoba

Use Lua The benefits of

  • Lua Script in Redis It's atomic execution , No other commands are inserted during execution
  • Lua Scripts can help developers and operators create their own customized commands , And they can be resident in Redis In the memory , Realize the effect of reuse
  • Lua Scripts can package multiple commands at one time , Effectively reduce network overhead

stay Redis perform Lua There are two ways to script :eval and evalsha

eval

eval   The script content  key  Number  key  list   parameter list 

The following example uses key List and parameter list for Lua Scripts provide more flexibility

127.0.0.1:6379> eval 'return "hello " ..KEYS[1]..ARGV[1]' 1 redis world
"hello redisworld"

here KEYS[1]=“redis”,ARGV[1]=“world”, So the final result is "hello redisworld"

If Lua The script is longer , You can also use redis-cli–eval Direct execution of documents ,eval Command and –eval The parameters are essentially the same , If the client wants to execute Lua Script , First, write it on the client side Lua Script code , Then send the script as a string to the server , The server will return the execution result to the client , The execution flow is as follows

evalsha

First of all to Lua The script is loaded into Redis Server side , Get the SHA1 The checksum ,evalsha Command to use SHA1 As a parameter, you can directly execute the corresponding Lua Script , Avoid sending... Every time Lua The cost of the script . So the client doesn't need to execute the script content every time , And the script will reside on the server , Script functions are reused

Load script :script load Command to load the script content into Redis In the memory , For example, the following lua_get.lua Load into Redis in , obtain SHA1

evalsha  Script  SHA1  value  key  Number  key  list   parameter list 

New script file

vim lua_test.lua

The script content , preservation

return "hello" ..KEYS[1]..ARGV[1]

Load script

[[email protected] ~]# redis-cli script load "$(cat lua_test.lua)"
"af54f206bd1c4e5de6b4a1edefa9b22622ea0805"

Execute the script

[[email protected] ~]# redis-cli
127.0.0.1:6379> evalsha af54f206bd1c4e5de6b4a1edefa9b22622ea0805 1 redis world
"helloredisworld"

Lua Of Redis API

Lua have access to redis.call Function implementation to Redis The interview of , For example, the following code is Lua Use redis.call Called Redis Of set and get operation

redis.call("set", "hello", "world")
redis.call("get", "hello")

Put it in Redis The implementation effect is as follows

127.0.0.1:6379> eval 'return redis.call("get", KEYS[1])' 1 hello
"world"

besides Lua You can also use redis.pcall Function implementation to Redis Call to ,redis.call and redis.pcall The difference is , If redis.call Execution failure , Then the script will directly return an error after execution , and redis.pcall Will ignore the error and continue to execute the script , So in the actual development, we should choose the function according to the specific application scenario

Use... For example : If the ranking function , The list of users is stored in key by user:rank List of , Each key is also used as string Key storage ranking for , Now use lua To atomically rank each user +1

127.0.0.1:6379> rpush user:rank Tom
(integer) 1
127.0.0.1:6379> rpush user:rank Jerry
(integer) 2
127.0.0.1:6379> rpush user:rank Spike
(integer) 3
127.0.0.1:6379> lrange user:rank 0 -1
1) "Tom"
2) "Jerry"
3) "Spike"
127.0.0.1:6379> set Tom 254
OK
127.0.0.1:6379> set Jerry 87
OK
127.0.0.1:6379> set Spike 15
OK
127.0.0.1:6379> mget Tom Jerry Spike
1) "254"
2) "87"
3) "15"

Write a script incr_user_rank.lua

-- Defining local variables list Read the incoming redis list Data of type 
local list = redis.call("lrange",keys[1],0,-1)
--count Is the number of self increasing 
local count = 0
for i,key in ipairs(list)
do
 redis.call("incr",key)
 count = count + 1
end
return count

Execute the script , Three pieces of data are added

[[email protected] ~]# redis-cli --eval incr_user_rank.lua user:rank
(integer) 3

Each data of the execution result is +1 了

127.0.0.1:6379> mget Tom Jerry Spike
1) "255"
2) "88"
3) "16"

Redis Medium lua Script command

  • scripts flush eliminate Redis All the memory that has been loaded Lua Script
  • scripts exists sha1 [sha1 … ] return SHA1 Whether there is redis Script cache
  • script load script take Lua The script is loaded into Redis In the memory
  • scripts kill Kill the executing Lua Script
原网站

版权声明
本文为[Love Guoba]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230900184536.html