1、redis不用多说了
nginx加载lua脚本过程参见 Nginx安装Openresty加载Lua代码_IT东东歌的博客-CSDN博客
文档 https://github.com/openresty/lua-resty-redis
2、直接上测试脚本
这是从官网扒下来的
local redis = require "resty.redis"
local red = redis:new()red:set_timeouts(1000, 1000, 1000) -- 1 sec
-- or connect to a unix domain socket file listened
-- by a redis server:
-- local ok, err = red:connect("unix:/path/to/redis.sock")-- connect via ip address directly
local ok, err = red:connect("127.0.0.1", 6379)-- or connect via hostname, need to specify resolver just like above
--local ok, err = red:connect("redis.openresty.com", 6379)if not ok then
ngx.say("failed to connect: ", err)
return
endok, err = red:set("dog", "an animal")
if not ok then
ngx.say("failed to set dog: ", err)
return
endngx.say("set result: ", ok)
local res, err = red:get("dog")
if not res then
ngx.say("failed to get dog: ", err)
return
endif res == ngx.null then
ngx.say("dog not found.")
return
endngx.say("dog: ", res)
red:init_pipeline()
red:set("cat", "Marry")
red:set("horse", "Bob")
red:get("cat")
red:get("horse")
local results, err = red:commit_pipeline()
if not results then
ngx.say("failed to commit the pipelined requests: ", err)
return
endfor i, res in ipairs(results) do
if type(res) == "table" then
if res[1] == false then
ngx.say("failed to run command ", i, ": ", res[2])
else
-- process the table value
end
else
-- process the scalar value
end
end-- put it into the connection pool of size 100,
-- with 10 seconds max idle time
local ok, err = red:set_keepalive(10000, 100)
if not ok then
ngx.say("failed to set keepalive: ", err)
return
end-- or just close the connection right away:
-- local ok, err = red:close()
-- if not ok then
-- ngx.say("failed to close: ", err)
-- return
-- end
测试ok