1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| # you do not need the following line if you are using # the ngx_openresty bundle: lua_package_path "/path/to/lua-resty-redis/lib/?.lua;;";
server { location /withoutpipeline { 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
local ok, err = red:set("cat", "Marry") ngx.say("set result: ", ok) local res, err = red:get("cat") ngx.say("cat: ", res)
ok, err = red:set("horse", "Bob") ngx.say("set result: ", ok) res, err = red:get("horse") ngx.say("horse: ", res)
-- 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 } }
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", "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 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 } } }
|