localfunctionclose_redes( red ) ifnot red then return end local ok, err = red:close() ifnot ok then ngx.say("close redis error:", err) end end
local redis = require("resty.redis")
-- 创建实例 local red = redis:new() -- 设置超时(毫秒) red:set_timeout(2000) -- 建立连接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) ifnot ok then return end local res, err = red:auth("wsy@123456") ifnot res then ngx.say("connect to redis error : ", err) return end -- 调用API进行处理 res, err = red:set("msg", "hello world") ifnot res then ngx.say("set msg error : ", err) return close_redes(red) end
-- 调用API获取数据 local resp, err = red:get("msg") ifnot resp then ngx.say("get msg erro:", err) return close_redes(red) end -- 得到数据为空处理 if resp == ngx.null then resp = ''-- 比如默认值 end ngx.say("msg:", resp)
localfunctionclose_redes( red ) ifnot red then return end -- 释放连接(连接池实现) local pool_max_idle_time = 10000-- 毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.say("set keepalive error : ", err) end end
-- local function close_redes( red ) -- if not red then -- return -- end -- local ok, err = red:close() -- if not ok then -- ngx.say("close redis error:", err) -- end -- end
localfunctionclose_redes( red ) ifnot red then return end -- 释放连接(连接池实现) local pool_max_idle_time = 10000-- 毫秒 local pool_size = 100--连接池大小 local ok, err = red:set_keepalive(pool_max_idle_time, pool_size) ifnot ok then ngx.say("set keepalive error : ", err) end end
local redis = require("resty.redis")
-- 创建实例 local red = redis:new() -- 设置超时(毫秒) red:set_timeout(2000) -- 建立连接 local ip = "172.19.73.87" local port = 6379 local ok, err = red:connect(ip, port) ifnot ok then return end local res, err = red:auth("wsy@123456") ifnot res then ngx.say("connect to redis error : ", err) return end