当前位置:网站首页>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详解
边栏推荐
- Pan details of deep learning
- XML Configuration File
- Intégration profonde et alignement des séquences de protéines Google
- Gbase8s composite index (I)
- Redis 过期淘汰机制
- 1086 tree traversals again (25 points)
- Redis expiration elimination mechanism
- Objects. Requirenonnull method description
- [backtrader source code analysis 5] rewrite several time number conversion functions in utils with Python
- 获取并监控远程服务器日志
猜你喜欢

Audio Focus Series: write a demo to understand audio focus and audiomananger
![[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)](/img/ce/d6f4fb30727e7436b6443537429ad4.png)
[batch dos-cmd command - summary and summary] - CMD window setting and operation command - close CMD window and exit CMD environment (exit, exit /b, goto: EOF)

求质数的方法

Gbase8s composite index (I)

Technical analysis of qianyuantong multi card aggregation router

Shallow and first code

Burp suite plug-in based on actual combat uses tips

Use posture of sudo right raising vulnerability in actual combat (cve-2021-3156)

cookie session jwt

leetcode406. Rebuild the queue based on height
随机推荐
Webapidom get page elements
Introduction to redis and explanation of data types
Interview question -- output the same characters in two character arrays
Appium 1.22. L'Inspecteur appium après la version X doit être installé séparément
JS dynamic table creation
Map的扩容机制
Pan details of deep learning
Intégration profonde et alignement des séquences de protéines Google
leetcode860. Lemonade change
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
Go practice -- design patterns in golang's singleton
@Autowired 导致空指针报错 解决方式
How to connect the network: Chapter 2 (Part 1): a life cycle of TCP connection | CSDN creation punch in
Maximum continuous sub segment sum (dynamic programming, recursive, recursive)
JS function algorithm interview case
Messy change of mouse style in win system
Ueditor, FCKeditor, kindeditor editor vulnerability
小学校园IP网络广播-基于校园局域网的小学IP数字广播系统设计
Principles of BTC cryptography
[set theory] relation properties (transitivity | transitivity examples | transitivity related theorems)