apisix lua插件使用redis

引入

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

相关推荐
沐知全栈开发14 小时前
HTTP Content-Type
开发语言
小马爱打代码14 小时前
Spring Boot :使用 Spring Cache 注解方式集成 Redis
spring boot·redis·spring
一切尽在,你来14 小时前
C++多线程教程-1.2.2 C++标准库并发组件的设计理念
开发语言·c++
m0_5613596714 小时前
代码热更新技术
开发语言·c++·算法
兩尛14 小时前
c++知识点1
java·开发语言·c++
凯子坚持 c14 小时前
Qt常用控件指南(9)
开发语言·qt
ONE_PUNCH_Ge15 小时前
Go 语言泛型
开发语言·后端·golang
leaves falling15 小时前
c语言单链表
c语言·开发语言
独自破碎E15 小时前
【中心扩展法】LCR_020_回文子串
java·开发语言