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

相关推荐
Seven9715 小时前
剑指offer-31、整数中1出现的次数
redis
AAA修煤气灶刘哥2 天前
别让Redis「歪脖子」!一次搞定数据倾斜与请求倾斜的捉妖记
redis·分布式·后端
侃侃_天下3 天前
最终的信号类
开发语言·c++·算法
christine-rr3 天前
linux常用命令(4)——压缩命令
linux·服务器·redis
echoarts3 天前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix3 天前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题3 天前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
凯子坚持 c3 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
伍哥的传说3 天前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
weixin_456904273 天前
跨域(CORS)和缓存中间件(Redis)深度解析
redis·缓存·中间件