当前位置:网站首页>Redis使用Lua脚本简介
Redis使用Lua脚本简介
2022-07-03 05:20:00 【Young丶】

1. 基本用法
1.1 EVAL script numkeys key [key …] arg [arg …]
numkeys 是key的个数,后边接着写key1 key2… val1 val2…,举例
127.0.0.1:6379> eval "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 val1 val2
1) "key1"
2) "key2"
3) "val1"
4) "val2"
1.2 SCRIPT LOAD script
把脚本加载到脚本缓存中,返回SHA1校验和。但不会立马执行,举例
127.0.0.1:6379> SCRIPT LOAD "return 'hello world'"
"5332031c6b470dc5a0dd9b4bf2030dea6d65de91"
1.3 EVALSHA sha1 numkeys key [key …] arg [arg …]
根据缓存码执行脚本内容。举例
127.0.0.1:6379> SCRIPT LOAD "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}"
"a42059b356c875f0717db19a51f6aaca9ae659ea"
127.0.0.1:6379> EVALSHA "a42059b356c875f0717db19a51f6aaca9ae659ea" 2 key1 key2 val1 val2
1) "key1"
2) "key2"
3) "val1"
4) "val2"
1.4 SCRIPT EXISTS script [script …]
通过sha1校验和判断脚本是否在缓存中
1.5 SCRIPT FLUSH
清空缓存
127.0.0.1:6379> SCRIPT LOAD "return 'hello jihite'"
"3a43944275256411df941bdb76737e71412946fd"
127.0.0.1:6379> SCRIPT EXISTS "3a43944275256411df941bdb76737e71412946fd"
1) (integer) 1
127.0.0.1:6379> SCRIPT FLUSH
OK
127.0.0.1:6379> SCRIPT EXISTS "3a43944275256411df941bdb76737e71412946fd"
1) (integer) 0
1.6 SCRIPT KILL
杀死目前正在执行的脚本
2. 主要优势
减少网络开销:多个请求通过脚本一次发送,减少网络延迟
原子操作:将脚本作为一个整体执行,中间不会插入其他命令,无需使用事务
复用:客户端发送的脚本永久存在redis中,其他客户端可以复用脚本
可嵌入性:可嵌入JAVA,C#等多种编程语言,支持不同操作系统跨平台交互
3. 实战
直接在redis-cli中直接写lua脚本,这样非常不方便编辑,通常情况下我们都是把lua script放到一个lua文件中,然后执行这个lua脚本,
**示例:活跃用户判断:**判断一个游戏用户是否属于活跃用户,如果符合标准,则活跃用户人数+1
if redis.call("EXISTS",KEYS[1]) == 1 then
return redis.call("INCRBY",KEYS[1],ARGV[1])
else
return nil
end
存储位置:
/Users/jihite/activeuser.lua
执行
$ redis-cli --eval /Users/jihite/activeuser.lua user , 1
(integer) 1
127.0.0.1:6379> get user
"1"
127.0.0.1:6379> exit
$ redis-cli --eval /Users/jihite/activeuser.lua user , 1
(integer) 2
$ redis-cli
127.0.0.1:6379> get user
"2"
127.0.0.1:6379> exit
$ redis-cli --eval /Users/jihite/activeuser.lua user , 4
(integer) 6
4. 脚本的安全性
如生成随机数这一命令,如果在master上执行完后,再在slave上执行会不一样,这就破坏了主从节点的一致性
为了解决这个问题, Redis 对 Lua 环境所能执行的脚本做了一个严格的限制 —— 所有脚本都必须是无副作用的纯函数(pure function)。所有刚才说的那种情况压根不存在。Redis 对 Lua 环境做了一些列相应的措施:
- 不提供访问系统状态状态的库(比如系统时间库)
- 禁止使用 loadfile 函数
- 如果脚本在执行带有随机性质的命令(比如 RANDOMKEY ),或者带有副作用的命令(比如 TIME )之后,试图执行一个写入命令(比如 SET ),那么 Redis 将阻止这个脚本继续运行,并返回一个错误。
- 如果脚本执行了带有随机性质的读命令(比如 SMEMBERS ),那么在脚本的输出返回给 Redis 之前,会先被执行一个自动的字典序排序,从而确保输出结果是有序的。
- 用 Redis 自己定义的随机生成函数,替换 Lua 环境中
math表原有的 math.random 函数和 math.randomseed 函数,新的函数具有这样的性质:每次执行 Lua 脚本时,除非显式地调用math.randomseed,否则math.random生成的伪随机数序列总是相同的。
参考
https://www.runoob.com/redis/redis-scripting.html (基本使用)
https://www.cnblogs.com/Don/articles/5731856.html (实例)
https://www.cnblogs.com/huangxincheng/p/6230129.html
https://redisbook.readthedocs.io/en/latest/feature/scripting.html#lua (安全性)
https://blog.csdn.net/demon7552003/article/details/120165160 Redis与Lua详解
边栏推荐
- Webrtc native M96 version opening trip -- a reading code download and compilation (Ninja GN depot_tools)
- 獲取並監控遠程服務器日志
- Go practice -- gorilla / websocket used by gorilla web Toolkit
- (subplots用法)matplotlib如何绘制多个子图(轴域)
- 【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
- Redis breakdown penetration avalanche
- "Hands on deep learning" pytorch edition Chapter II exercise
- Skip table: principle introduction, advantages and disadvantages of skiplist
- Common methods of JS array
- (subplots usage) Matplotlib how to draw multiple subgraphs (axis field)
猜你喜欢

Common interview questions of microservice

DEX net 2.0 for crawl detection

BIO、NIO、AIO区别

(perfect solution) how to set the position of Matplotlib legend freely

Webrtc protocol introduction -- an article to understand ice, stun, NAT, turn

About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller

求质数的方法

Technical analysis of qianyuantong multi card aggregation router

Go practice - gorilla / handlers used by gorilla web Toolkit

Webrtc M96 release notes (SDP abolishes Plan B and supports opus red redundant coding)
随机推荐
Brief introduction of realsense d435i imaging principle
3dslam with 16 line lidar and octomap
獲取並監控遠程服務器日志
Go language interface learning notes Continued
Skip table: principle introduction, advantages and disadvantages of skiplist
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
1118 birds in forest (25 points)
Go practice -- design patterns in golang's singleton
Common interview questions of microservice
【批处理DOS-CMD命令-汇总和小结】-CMD窗口的设置与操作命令-关闭cmd窗口、退出cmd环境(exit、exit /b、goto :eof)
Dynamic programming - related concepts, (tower problem)
穀歌 | 蛋白序列的深度嵌入和比對
乾元通多卡聚合路由器的技术解析
动态规划——相关概念,(数塔问题)
Introduction to redis and explanation of data types
Go practice -- closures in golang (anonymous functions, closures)
Redis 击穿穿透雪崩
[backtrader source code analysis 4] use Python to rewrite the first function of backtrader: time2num, which improves the efficiency by 2.2 times
How to connect the network: Chapter 1 CSDN creation punch in
appium1.22. Appium inspector after X version needs to be installed separately