Millisecond response hit back at OpenResty + Lua access Redis, high concurrent access

Configuration-dependent:

1, OpenResty of lua access redis plugin: https: //github.com/openresty/lua-resty-redis

Once downloaded, the corresponding import plug-ins:

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

2, using lua access 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])
                        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

                -- 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, using the connection pool redis

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

4, requires a password to access the redis: Use auth method

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

    local res, err = red:auth("password")
    if not res then
        return
    end

5, redis operation, no separate packaging method, lua-resty-redis support method of automatically generating a corresponding lua

The specific configuration is: redis.lua in, common_cmds the array, where addition method requires the use of

For example: the need to use hincrby redis hsah, then add hincrby in common_cmds, can be used, directly in lua in Red: hincrby (key, field, 1)

6, project usage scenarios

(1) the front end of some data http query directly get access through lua redis in nginx, returns directly to the front end, to reduce the pressure on the server; the update data through the server actively redis

(2) the number of clicks and page opens analysis: between clicks and page opens, with a request to reach statistical nginx, nginx When a request arrives, the number of pages to be accessed by lua write redis, and then clicks through analysis, the number of requests nginx obtained, the number of times a specific page open business

Guess you like

Origin www.cnblogs.com/gxyandwmm/p/11305526.html