前言
Redis自2.6.0版本开始内置Lua解释器。
Lua,轻量级脚本语言,号称最快的脚本语言。
两者结合将爆发出巨大的威力。
简介
Redis Lua脚本可以调用原生的Redis命令,也可以包含自定义的控制流、逻辑运算、数学运算等,将复杂的业务逻辑封装起来,形成一个原子事务。
这些特性使我们可以自由地扩展Redis,封装“自定义命令”。
与MULTI+EXEC对比
使用MULTI+EXEC及相关组合命令,也可以将多个命令封装成事务,但灵活性不如Lua脚本。
除此之外,MULTI+EXEC需要多次向Redis server发送事务命令,每次发送都会有RTT(Round Trip Time)消耗,性能低于Lua脚本。
Redis Lua Scripts Debugger (LDB)
Redis自3.2版本开始包含LDB,用于调试Lua脚本。
LDB支持设置断点,逐行执行等。
一个简单的例子
执行set命令,并设置新的ttl
编写Lua脚本,文件名为set_ttl.lua
local set_msg = false
-- 执行redis命令的返回结果被封装成了table,形如{'ok':'OK'}
local key_type = redis.call('TYPE' , KEYS[1])['ok']
-- key的数据类型为string或key不存在时,继续执行
if key_type == 'string' or key_type == 'none' then
local ttl = tonumber(ARGV[2])
set_msg = redis.call('SET', KEYS[1], ARGV[1], 'EX', ttl)['ok']
end
return set_msg -- 正常情况下会返回"OK"
调试Lua脚本
命令如下
$ redis-cli --ldb --eval set_ttl.lua fruit , apple 100
其中,set_ttl.lua为脚本名称,
逗号前的参数将存入变量KEYS,逗号后的参数将存入变量ARGV,多个变量用空格隔开。
默认情况下,LDB将执行脚本第一行并停住。
输入n并回车,执行下一行,重复直到脚本执行结束。
调试输出如下。
从调试输出的最后部分可以看出
- 脚本返回了"OK"。
- 调试修改的数据集被回滚了,调试并不会真的修改数据库
$ redis-cli --ldb --eval set_ttl.lua fruit , apple 100
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local set_msg = false
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3 local key_type = redis.call('TYPE' , KEYS[1])['ok']
lua debugger> n
TYPE fruit
"+none"
* Stopped at 5, stop reason = step over
-> 5 if key_type == 'string' or key_type == 'none' then
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6 local ttl = tonumber(ARGV[2])
lua debugger> n
* Stopped at 7, stop reason = step over
-> 7 set_msg = redis.call('SET', KEYS[1], ARGV[1], 'EX', ttl)['ok']
lua debugger> n
SET fruit apple EX 100
"+OK"
* Stopped at 9, stop reason = step over
-> 9 return set_msg -- 正常情况下会返回"OK"
lua debugger> n
"OK"
(Lua debugging session ended -- dataset changes rolled back)
一个复杂的例子
批量获取并更新hash类型的key,做必要的类型检查,获取key的ttl,最终返回每个key的执行结果
编写lua脚本,文件名getset_hash.lua
local results = {}
for index , key in ipairs(KEYS) do
local old_value = false
local mset_msg = false
local ttl = false
local key_type = redis.call('TYPE' , key)['ok']
if key_type == 'hash' then
old_value = redis.call('HMGET' , key , ARGV[1], ARGV[2])
ttl = redis.call('TTL', key)
end
if key_type == 'hash' or key_type == 'none' then
mset_msg = redis.call('HMSET' , key , ARGV[1], ARGV[3], ARGV[2], ARGV[4])['ok']
end
results[index] = { key, old_value, ttl, mset_msg }
end
return results
调试输出
$ redis-cli --ldb --eval getset_hash.lua person1 person2 , name age Alice 22
Lua debugging session started, please use:
quit -- End the session.
restart -- Restart the script in debug mode again.
help -- Show Lua script debugging commands.
* Stopped at 1, stop reason = step over
-> 1 local results = {}
lua debugger> n
* Stopped at 2, stop reason = step over
-> 2 for index , key in ipairs(KEYS) do
lua debugger> n
* Stopped at 3, stop reason = step over
-> 3 local old_value = false
lua debugger> n
* Stopped at 4, stop reason = step over
-> 4 local mset_msg = false
lua debugger> n
* Stopped at 5, stop reason = step over
-> 5 local ttl = false
lua debugger> n
* Stopped at 6, stop reason = step over
-> 6 local key_type = redis.call('TYPE' , key)['ok']
lua debugger> n
TYPE person1
"+none"
* Stopped at 7, stop reason = step over
-> 7 if key_type == 'hash' then
lua debugger> n
* Stopped at 11, stop reason = step over
-> 11 if key_type == 'hash' or key_type == 'none' then
lua debugger> n
* Stopped at 12, stop reason = step over
-> 12 mset_msg = redis.call('HMSET' , key , ARGV[1], ARGV[3], ARGV[2], ARGV[4])['ok']
lua debugger> n
HMSET person1 name Alice age 22
"+OK"
* Stopped at 14, stop reason = step over
-> 14 results[index] = { key, old_value, ttl, mset_msg }
# 略去循环执行的输出
-> 16 return results
lua debugger> n
1) 1) "person1"
2) (nil)
3) (nil)
4) "OK"
2) 1) "person2"
2) (nil)
3) (nil)
4) "OK"
(Lua debugging session ended -- dataset changes rolled back)
执行脚本
命令
$ redis-cli --eval getset_hash.lua person1 person2 , name age Alice 22
查看数据
$ redis-cli
127.0.0.1:6379> keys *
1) "person2"
2) "person1"
127.0.0.1:6379> hgetall person1
1) "name"
2) "Alice"
3) "age"
4) "22"
127.0.0.1:6379> hgetall person2
1) "name"
2) "Alice"
3) "age"
4) "22"
参考链接
- https://redis.io/topics/transactions
- https://redis.io/topics/ldb
- https://redis.io/commands/multi
- https://www.tutorialspoint.com/lua/lua_operators.htm
- https://github.com/limen/redisun-py