欢迎投稿

今日深度:

ngx_lua连接redis,ngx_luaredis

ngx_lua连接redis,ngx_luaredis



local redis = require("redis")
--创建实例 
local red = redis:new()
--设置超时(毫秒) 
red:set_timeout(1000)
--建立连接 
local ip = "127.0.0.1"
    ngx.say("connect to redis error : ", err)
    return close_redis(red)
end
--调用API进行处理 
ok, err = red:set("msg", "hello world")
if not ok then

ngx.say("ok")
--调用API获取数据 
local resp, err = red:get("msg") 
if not resp then 
    ngx.say("get msg error : ", err) 
    return close_redis(red) 
if resp == ngx.null then 
    resp = ''  --比如默认值 
end 
ngx.say("msg : ", resp)


local redis = require("redis")
ngx.say("hello")

--创建实例 
local red = redis:new()
--设置超时(毫秒) 
red:set_timeout(1000)
--建立连接 
local ip = "127.0.0.1"
local port = 6379
local ok, err = red:connect(ip, port)
if not ok then
    ngx.say("connect to redis error : ", err)
    return close_redis(red)
end
--调用API进行处理 
ok, err = red:set("msg", "hello world")
if not ok then
    ngx.say("set msg error : ", err)
    return close_redis(red)
end

ngx.say("ok")
--调用API获取数据 
local resp, err = red:get("msg")
if not resp then
    ngx.say("get msg error : ", err)
    return close_redis(red)
end
--得到的数据为空处理 
if resp == ngx.null then
    resp = ''  --比如默认值 
end
ngx.say("msg : ", resp)

local function close_redis(red)
    if not 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)
    if not ok then
        ngx.say("set keepalive error : ", err)
    end
end 

close_redis(red)



local cjson = require "cjson"
    local redis = require "redis"

    local red1 = redis:new()
    local red2 = redis:new()

    red1:set_timeout(1000) -- 1 sec
    red2:set_timeout(1000) -- 1 sec

https://github.com/openresty/lua-resty-redis

    local ok, err = red1:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("1: failed to connect: ", err)
        return
    end

    ok, err = red2:connect("127.0.0.1", 6379)
    if not ok then
        ngx.say("2: failed to connect: ", err)
        return
    end

    local res, err = red1:subscribe("dog")
    if not res then
        ngx.say("1: failed to subscribe: ", err)
        return
    end

    ngx.say("1: subscribe: ", cjson.encode(res))

    res, err = red2:publish("dog", "Hello")
    if not res then
        ngx.say("2: failed to publish: ", err)
        return
    end

    ngx.say("2: publish: ", cjson.encode(res))

    res, err = red:read_reply()
    if not res then
        ngx.say("1: failed to read reply: ", err)
        return
    end

    ngx.say("1: receive: ", cjson.encode(res))


close_redis(red1)
close_redis(red2)


需求,
利用lua监控redis的订阅发布,

www.htsjk.Com true http://www.htsjk.com/redis/36579.html NewsArticle ngx_lua连接redis,ngx_luaredis local redis = require ( "redis" ) --创建实例 local red = redis:new() --设置超时(毫秒) red:set_timeout( 1000 ) --建立连接 local ip = "127.0.0.1" ngx.say( "connect to redis error : " , err)...
相关文章
    暂无相关文章
评论暂时关闭