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

相关推荐
曹轲恒7 小时前
Java中断
java·开发语言
施棠海8 小时前
监听与回调的三个demo
java·开发语言
時肆4858 小时前
C语言造轮子大赛:从零构建核心组件
c语言·开发语言
赴前尘8 小时前
golang 查看指定版本库所依赖库的版本
开发语言·后端·golang
Mr__Miss8 小时前
Redis的持久化
数据库·redis·缓存
de之梦-御风8 小时前
【C#.Net】C#开发的未来前景
开发语言·c#·.net
知乎的哥廷根数学学派9 小时前
基于数据驱动的自适应正交小波基优化算法(Python)
开发语言·网络·人工智能·pytorch·python·深度学习·算法
de之梦-御风9 小时前
【C#.Net】C#在工业领域的具体应用场景
开发语言·c#·.net
sunfove9 小时前
将 Python 仿真工具部署并嵌入个人博客
开发语言·数据库·python
Learner9 小时前
Python类
开发语言·python