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

相关推荐
Han.miracle22 分钟前
JavaEE —— 网路编程 Socket套接字
java·java-ee
j***894629 分钟前
Spring Boot整合Redisson的两种方式
java·spring boot·后端
Laravel技术社区29 分钟前
php 读取视频流(mp4视频文件),快速读取视频解决方案(案例及配置)
开发语言·php·音视频
q***965830 分钟前
Spring Boot 各种事务操作实战(自动回滚、手动回滚、部分回滚)
java·数据库·spring boot
ytttr87332 分钟前
基于MATLAB实现晶体共晶凝固模拟
开发语言·算法·matlab
今天有个Bug32 分钟前
Java 读取RTSP、RTMP等网络流、图像、视频指南,易于理解,方便使用
java·推流拉流·javacv·图片视频处理
JavaGuide40 分钟前
Spring Boot 4.0 正式发布,真学不动了!
java·spring boot
90后小陈老师1 小时前
用户管理系统 03 搭建基本结构 | Java新手实战 | 最小架构用户管理系统(SpringBoot+Vue3)
java·spring boot·架构
鄃鳕1 小时前
装饰器【Python】
开发语言·python·数码相机
n***84071 小时前
Spring Boot(快速上手)
java·spring boot·后端