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

相关推荐
yj爆裂鼓手15 分钟前
c#万能变量
开发语言·c#
GGGG寄了18 分钟前
HTML——文本标签
开发语言·前端·html
yangSnowy25 分钟前
PHP变量回收机制
开发语言·php
C+-C资深大佬1 小时前
C++类型判断
开发语言·c++
曾经的三心草1 小时前
redis-2-数据结构内部编码-单线程-String命令
数据结构·数据库·redis
2501_944521591 小时前
Flutter for OpenHarmony 微动漫App实战:推荐动漫实现
android·开发语言·前端·javascript·flutter·ecmascript
不绝1911 小时前
C#进阶:委托
开发语言·c#
喜欢喝果茶.1 小时前
跨.cs 文件传值(C#)
开发语言·c#
zmzb01031 小时前
C++课后习题训练记录Day74
开发语言·c++
小冷coding1 小时前
【Java】Dubbo 与 OpenFeign 的核心区别
java·开发语言·dubbo