相关文章推荐
追风的牛肉面  ·  肺癌相关单核苷酸多态性的研究进展- PMC·  7 月前    · 
追风的牛肉面  ·  透過全基因組核酸多態型資料研究台灣南部埃及斑 ...·  7 月前    · 
追风的牛肉面  ·  DNA多态性_百度百科·  7 月前    · 
追风的牛肉面  ·  DNA修复基因多态性在六价铬遗传损伤中的作用·  7 月前    · 
追风的牛肉面  ·  麂属(Muntiacus)动物线粒体DNA多 ...·  7 月前    · 
小百科  ›  OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回 ...
OpenResty 毫秒 lua redis openresty
追风的牛肉面
7 月前
小勇DW3

OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回

腾讯云
开发者社区
文档 建议反馈 控制台
首页
学习
活动
专区
圈层
工具
MCP广场
文章/答案/技术大牛
发布
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
小勇DW3
首页
学习
活动
专区
圈层
工具
MCP广场
返回腾讯云官网
社区首页 > 专栏 > OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回

OpenResty + Lua访问Redis,实现高并发访问时的毫秒级响应打回

作者头像
小勇DW3
发布 于 2019-08-07 15:26:05
发布 于 2019-08-07 15:26:05
6K 0
举报
文章被收录于专栏: 小勇DW3 小勇DW3

配置依赖:

1、OpenResty的lua访问redis的插件:https://github.com/openresty/lua-resty-redis

下载后,导入对应的插件:

lua_package_path "/opt/openresty/lualib/kafka/?.lua;;"; lua_need_request_body on;

2、使用lua访问redis:

server {         location /test {             content_by_lua_block {                 local redis = require "resty.redis"                 local red = redis:new()

red:set_timeout(1000) -- 1 sec

-- or connect to a unix domain socket file listened                 -- by a redis server:                 --     local ok, err = red:connect("unix:/path/to/redis.sock")

local ok, err = red:connect("127.0.0.1", 6379)                 if not ok then                     ngx.say("failed to connect: ", err)                     return                 end

ok, err = red:set("dog", "an animal")                 if not ok then                     ngx.say("failed to set dog: ", err)                     return                 end

ngx.say("set result: ", ok)

local res, err = red:get("dog")                 if not res then                     ngx.say("failed to get dog: ", err)                     return                 end

if res == ngx.null then                     ngx.say("dog not found.")                     return                 end

ngx.say("dog: ", res)

red:init_pipeline()                 red:set("cat", "Marry")                 red:set("horse", "Bob")                 red:get("cat")                 red:get("horse")                 local results, err = red:commit_pipeline()                 if not results then                     ngx.say("failed to commit the pipelined requests: ", err)                     return                 end

for i, res in ipairs(results) do                     if type(res) == "table" then                         if res[1] == false then                             ngx.say("failed to run command ", i, ": ", res[2])                             -- process the table value                         -- process the scalar value                 end

-- put it into the connection pool of size 100,                 -- with 10 seconds max idle time                 local ok, err = red:set_keepalive(10000, 100)                 if not ok then                     ngx.say("failed to set keepalive: ", err)                     return                 end

-- or just close the connection right away:                 -- local ok, err = red:close()                 -- if not ok then                 --     ngx.say("failed to close: ", err)                 --     return                 -- end     }

3、使用redis连接池

local ok, err = red:set_keepalive(60000, 20)

4、需要密码的redis的访问:使用 auth 方法

local ok, err = red.connect(red, "127.0.0.1", "6379")     if not ok then

 
推荐文章
追风的牛肉面  ·  肺癌相关单核苷酸多态性的研究进展- PMC
7 月前
追风的牛肉面  ·  透過全基因組核酸多態型資料研究台灣南部埃及斑蚊在小尺度的族群 ...
7 月前
追风的牛肉面  ·  DNA多态性_百度百科
7 月前
追风的牛肉面  ·  DNA修复基因多态性在六价铬遗传损伤中的作用
7 月前
追风的牛肉面  ·  麂属(Muntiacus)动物线粒体DNA多态性及其遗传分化-SciEngine
7 月前
Link管理   ·   Sov5搜索   ·   小百科
小百科 - 百科知识指南