当前位置:网站首页>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
边栏推荐
- Utilisation du cookie du module de demande de noeud
- Combination sum II of leetcode topic analysis
- 进入小公司的初级程序员要如何自我提高?
- June 22, 2022: golang multiple choice question, what does the following golang code output? A:3; B:1; C:4; D: Compilation failed.
- 636. Exclusive Time of Functions
- Servlet-02 生命周期
- The results of CDN node and source station are inconsistent
- Longest substring without repeated characters (C language)
- 4sum of leetcode topic analysis
- Leetcode topic analysis contains duplicate III
猜你喜欢

16.系统启动流程

学习SCI论文绘制技巧(E)

'教练,我想打篮球!' —— 给做系统的同学们准备的 AI 学习系列小册

"Coach, I want to play basketball" -- AI Learning Series booklet for students who are making systems

“教练,我想打篮球“ —— 给做系统的同学们准备的 AI 学习系列小册

Basic process of code scanning login

线性表(SequenceList)的顺序表示与实现----线性结构

Simple student management

Typora设置图片上传服务

General paging (1)
随机推荐
Leetcode topic analysis count primes
点击添加下拉框
Lua的基本使用
Simple student management
ionic5表单输入框和单选按钮
Cookie和Session入门
[qnx hypervisor 2.2 user manual]5.6.1 silent device during guest shutdown
简易学生管理
65. Valid Number
MySQL故障案例 | ERROR 1071 (42000): Specified key was too long
In June, China database industry analysis report was released! Smart wind, train storage and regeneration
438. Find All Anagrams in a String
How thingjs enables low threshold 3D visualization development
Quartz Crystal Drive Level Calculation
Community article | mosn building subset optimization ideas sharing
嵌入式系统概述(学习笔记)
297. Serialize and Deserialize Binary Tree
528. Random Pick with Weight
MySQL fault case | error 1071 (42000): specified key was too long
Restore the default routing settings of the primary network card