Hi, new to lua, any kindly suggestion or better way is helpful to reuse the same connection in openresty?
is there any possible ways to reuse the same cosocket object(redis-connection with resty-redis)?
Please took at this snippet code:
function M.openRedisConnection()
local inittime = os.clock();
local red = redis:new();
red:settimeout(prop.redisTimeout);
local ok, err = red:connect(prop.redisHost, prop.redisPort ,prop.connect_options)
ngx.say("reused times: ", red.sock:getreusedtimes())
if red.sock:getreusedtimes() ==0 then
if ok then
ngx.say("Connected to redis DB : ", ok)
local res, autherr = red:auth(prop.redisPasskey)
if not res then
ngx.say("Failed to Auth : ", autherr)
else
ngx.say("Authentication : ", res)
end
else
ngx.say("failed to connect: ", err)
end
end
red.sock:setkeepalive(prop.redisSetkeepalive, prop.redisSize);
ngx.say("Time taken : ", os.clock()- inittime)
return red;
end
####################################
function M.getRedisConnection()
local read = rr:openRedisConnection();
--read.sock:connect(prop.redisHost, prop.redisPort ,prop.connect_options)
local res, err = read:get("Vijay")
if not res then
ngx.say("failed to get : ", err)
else
ngx.say("Getting {} : ", res)
end
ngx.say("reused times: ", read.sock:getreusedtimes())
read.sock:setkeepalive(prop.redisSetkeepalive, prop.redisSize)
end
why should i need to use connect method again in getRedisConnection() to do get/set opertaions?
without that my connection is getting closed.
Thanks in advance for the any help