Use a Lua Table
定义一个函数和一个 table,把日志记录在 table 中,并返回
local logtable = {}
local function log(msg)
logtable[#logtable+1] = msg
end
log("hello")
log("world")
return logtable
$ redis-cli –eval log.lua
1) “hello”
2) “world”
Use a Redis List
local loglist = KEYS[1]
redis.pcall("del", loglist)
local function log(msg)
redis.pcall("rpush", loglist, msg)
end
log("hello")
log("world")
return 'ok' .. 'ok'
$redis-cli –eval log.lua mylist
“okok”
lrange mylist 0 -1
1) “hello”
2) “world”
Use Redis’Pub/Sub
local function log(msg)
redis.pcall("publish", 'log', msg)
end
log("hello")
log("world")
return 'ok' .. 'ok'
执行脚本客户端
$ redis-cli –eval log.lua
“okok”
日志订阅者客户端
subscribe log
Reading messages… (press Ctrl-C to quit)
1) “subscribe”
2) “log”
3) (integer) 1
1) “message”
2) “log”
3) “hello”
1) “message”
2) “log”
3) “world”
参考:
https://redis.com/blog/5-6-7-…