引入
local redis = require("resty.redis")
local red = redis:new()
local redis_config = {
host = "redis_v1",
port = "6379",
pass = "123456",
db = "0"
}
local function conn_redis()
local ok, err = red:connect(redis_config.host, redis_config.port)
if not ok then
core.log.error("failed to connect: ", err)
return false
end
local res, err = red:auth(redis_config.pass)
if not res then
core.log.error("failed to auth: ", err)
return false
end
local db, err = red:select(redis_config.db)
if not db then
core.log.error("failed to select: ", err, redis_config.db)
return false
end
return true
end
local function close_redis()
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
core.log.error("redis red:set_keepalive err : ", err)
end
end