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 秒左右

相关推荐
CryptoRzz几秒前
墨西哥股票数据 API 对接实战指南(含实时行情与 IPO 功能)
java·后端·websocket·区块链
leo_232几秒前
从开发语言角度来谈谈SMP(中)--SMP(软件制作平台)语言基础知识之十三
开发语言·开发工具·smp(软件制作平台)·应用系统
leo_232几秒前
从开发语言角度来谈谈SMP(下)--SMP(软件制作平台)语言基础知识之十三
linux·运维·开发语言·开发工具·smp(软件制作平台)·应用系统
t198751281 分钟前
基于MATLAB的Bezier曲线曲面绘制实现
开发语言·matlab
_codemonster1 分钟前
python易混淆知识点(十五)迭代器
开发语言·windows·python
hgz07102 分钟前
Spring Boot自动配置
java·springboot
@淡 定3 分钟前
慢查询分析与优化
java
大学生资源网5 分钟前
基于springboot的南京特色美食小吃商城(源码+文档)
java·spring boot·后端·mysql·毕业设计·源码
molaifeng5 分钟前
从 utf8.RuneCountInString 看 Go 是如何高性能、安全地解码 UTF-8 的
开发语言·安全·golang
小此方5 分钟前
Re: ゼロから学ぶ C++ 入門(七)类和对象·第四篇:拷贝构造函数&赋值运算符重载
开发语言·c++