7.OpenResty系列之LuaRestyRedisLibrary

1. 连接需授权的redis
sh 复制代码
server {
    location /redis1 {
        content_by_lua_block {
            local redis = require "resty.redis"
            local red = redis:new()
	    -- 1秒
            red:set_timeout(1000) 

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

            -- 请注意这里 auth 的调用过程
            local count
            count, err = red:get_reused_times()
	    -- 如果当前连接不是从内建连接池中获取的,该方法总是返回 0 ,也就是说,该连接还没有被使用过
            if 0 == count then
                ok, err = red:auth("password")
                if not ok then
                    ngx.say("failed to auth: ", err)
                    return
                end
            elseif err then
                ngx.say("failed to get reused times: ", err)
                return
            end

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

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

            -- 连接池大小是100个,并且设置最大的空闲时间是 10 秒
            local ok, err = red:set_keepalive(10000, 100)
            if not ok then
                ngx.say("failed to set keepalive: ", err)
                return
            end
        }
    }
}
2. pipeline 压缩请求数量
sh 复制代码
server {
    location /withpipeline {
            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

                red:init_pipeline()
                red:set("cat", "CaiCai")
                red:set("dog", "doge coin")
                red:get("cat")
                red:get("dog")
                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 not res[1] then
                            ngx.say("failed to run command ", i, ": ", res[2])
                        else
                            -- process the table value
                        end
                    else
                        -- process the scalar value
                    end
                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
           }
     }
}

OpenResty最佳实践作者:曾经某个后台应用,逐个处理大约 100 万条记录需要几十分钟,经过 pileline 压缩请求数量后,最后时间缩小到 20 秒左右

相关推荐
没有bug.的程序员2 分钟前
订单系统重构史诗:从单体巨兽到微服务矩阵的演进、数据一致性内核与分布式事务
java·微服务·矩阵·重构·分布式事务·数据一致性·订单系统
sycmancia4 分钟前
C++——友元、函数重载、操作符重载
开发语言·c++
m0_738120725 分钟前
应急响应——Solar月赛emergency靶场溯源过程(内含靶机下载以及流量分析)
java·开发语言·网络·redis·web安全·系统安全
逍遥德9 分钟前
Maven教程.03-如何阅读pom.xml文件
xml·java·后端·maven
Java面试题总结12 分钟前
Tube - Video Reactions
开发语言·前端·javascript
上海合宙LuatOS12 分钟前
LuatOS核心库API——【json 】json 生成和解析库
java·前端·网络·单片机·嵌入式硬件·物联网·json
没有bug.的程序员12 分钟前
金融风控系统:实时规则引擎内核、决策树物理建模与 Drools 性能压榨
java·数据库·决策树·金融·drools·物理建模·实时规则
0x5318 分钟前
JAVA|面向对象
java
NGC_661122 分钟前
插入排序算法
java·数据结构·算法
kylezhao201923 分钟前
C#中 Invoke、begininvoke、InvokeRequired的详细讲解和三者之间的区别
开发语言·c#