当前位置:网站首页>Redis学习笔记—Redis与Lua
Redis学习笔记—Redis与Lua
2022-06-23 09:00:00 【爱锅巴】
使用Lua的好处
- Lua脚本在Redis中是原子执行的,执行过程中间不会插入其他命令
- Lua脚本可以帮助开发和运维人员创造出自己定制的命令,并可以将这些命令常驻在Redis内存中,实现复用的效果
- Lua脚本可以将多条命令一次性打包,有效地减少网络开销
在Redis执行Lua脚本有两种方法:eval和evalsha
eval
eval 脚本内容 key 个数 key 列表 参数列表
下面例子使用了key列表和参数列表来为Lua脚本提供更多的灵活性
127.0.0.1:6379> eval 'return "hello " ..KEYS[1]..ARGV[1]' 1 redis world
"hello redisworld"
此时KEYS[1]=“redis”,ARGV[1]=“world”,所以最终的返回结果是"hello redisworld"
如果Lua脚本较长,还可以使用redis-cli–eval直接执行文件,eval命令和–eval参数本质是一样的,客户端如果想执行Lua脚本,首先在客户端编写好Lua脚本代码,然后把脚本作为字符串发送给服务端,服务端会将执行结果返回给客户端,执行流程如下图
evalsha
首先要将Lua脚本加载到Redis服务端,得到该脚本的SHA1校验和,evalsha命令使用SHA1作为参数可以直接执行对应Lua脚本,避免每次发送Lua脚本的开销。这样客户端就不需要每次执行脚本内容,而脚本也会常驻在服务端,脚本功能得到了复用
加载脚本:script load命令可以将脚本内容加载到Redis内存中,例如下面将lua_get.lua加载到Redis中,得到SHA1
evalsha 脚本 SHA1 值 key 个数 key 列表 参数列表
新建脚本文件
vim lua_test.lua
脚本内容,保存
return "hello" ..KEYS[1]..ARGV[1]
加载脚本
[[email protected] ~]# redis-cli script load "$(cat lua_test.lua)"
"af54f206bd1c4e5de6b4a1edefa9b22622ea0805"
执行脚本
[[email protected] ~]# redis-cli
127.0.0.1:6379> evalsha af54f206bd1c4e5de6b4a1edefa9b22622ea0805 1 redis world
"helloredisworld"
Lua的Redis API
Lua可以使用redis.call函数实现对Redis的访问,例如下面代码是Lua使用redis.call调用了Redis的set和get操作
redis.call("set", "hello", "world")
redis.call("get", "hello")
放在Redis的执行效果如下
127.0.0.1:6379> eval 'return redis.call("get", KEYS[1])' 1 hello
"world"
除此之外Lua还可以使用redis.pcall函数实现对Redis的调用,redis.call和redis.pcall的不同在于,如果redis.call执行失败,那么脚本执行结束会直接返回错误,而redis.pcall会忽略错误继续执行脚本,所以在实际开发中要根据具体的应用场景进行函数的选择
举例使用:如果排名功能,用户的列表是存储在key为user:rank的列表中,各个键各自也作为string的键存储排名,现在要用lua来原子性的把每个用户的排名数+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"
编写脚本incr_user_rank.lua
--定义局部变量list读取传入的redis list类型的数据
local list = redis.call("lrange",keys[1],0,-1)
--count是自增的次数
local count = 0
for i,key in ipairs(list)
do
redis.call("incr",key)
count = count + 1
end
return count
执行脚本,增加了三条数据
[[email protected] ~]# redis-cli --eval incr_user_rank.lua user:rank
(integer) 3
执行结果每条数据都+1了
127.0.0.1:6379> mget Tom Jerry Spike
1) "255"
2) "88"
3) "16"
Redis中的lua脚本命令
- scripts flush 清除Redis内存已经加载的所有Lua脚本
- scripts exists sha1 [sha1 … ] 返回SHA1 是否存在redis脚本缓存中
- script load script 将Lua脚本加载到Redis内存中
- scripts kill 杀掉正在执行的Lua脚本
边栏推荐
- 986. Interval List Intersections
- [QNX Hypervisor 2.2用户手册]6.2 网络
- 一个采用直接映射方式的32KB缓存......存储器课后习题
- 173. Binary Search Tree Iterator
- A method of realizing video call and interactive live broadcast in small programs
- Longest substring without repeated characters (C language)
- Lighthouse cloud desktop experience
- 4、 Database design
- Typora设置图片上传服务
- 125. Valid Palindrome
猜你喜欢

社区文章|MOSN 构建 Subset 优化思路分享

自定义标签——jsp标签基础

Geoserver添加mongoDB数据源
![[learning resources] understand and love mathematics](/img/a3/e1b0915c48c85d17c48a4bee523424.png)
[learning resources] understand and love mathematics

力扣之滑动窗口《循序渐进》(209.长度最小的子数组、904. 水果成篮)

Testing -- automated testing selenium (about API)

636. Exclusive Time of Functions

Typora设置图片上传服务

Flink错误--Caused by: org.apache.calcite.sql.parser.SqlParseException: Encountered “time“

Simple student management
随机推荐
Quartz Crystal Drive Level Calculation
GeoServer adding mongodb data source
点击添加下拉框
5、 Project management
Leetcode topic analysis sort colors
Comprehensive analysis of news capture
H-index of leetcode topic analysis
125. Valid Palindrome
[qnx hypervisor 2.2 user manual]6.2 network
Le rapport d'analyse de l'industrie chinoise des bases de données a été publié en juin. Le vent intelligent se lève, les colonnes se régénèrent
[cloud native | kubernetes] kubernetes principle and installation (II)
6月《中國數據庫行業分析報告》發布!智能風起,列存更生
438. Find All Anagrams in a String
Custom tags - JSP tag enhancements
16.系统启动流程
3. caller service call - dapr
In depth interpretation of poca smart contract platform gear: the road to parallel architecture public chain
三层架构与SSM之间的对应关系
297. Serialize and Deserialize Binary Tree
670. Maximum Swap