[samuel@localhost nginx]$ cat conf/nginx.conf
worker_processes 1;
error_log logs/error.log error;
events {
worker_connections 1024;
}
http {
lua_shared_dict ngx_cache 1m;
server {
listen 8080;
lua_code_cache off;
location ~ ^/api/([\w_]+) {
content_by_lua_file lua/$1.lua;
}
}
}
-- https://moonbingbing.gitbooks.io/openresty-best-practices/content/redis/auth_connect.html
local redis = require "resty.redis"
local red = redis:new()
red:set_timeout(1000) -- 1 sec
local ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local count, err = red:get_reused_times()
ngx.log(ngx.ERR, "get_reused_times: " .. count)
local 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)
local ok, err = red:set_keepalive(10000, 1)
if not ok then
ngx.say("failed to set keepalive ", err)
return
end
[samuel@localhost resty]$ netstat -tnp | grep 6379
(Not all processes could be identified, non-owned process info
will not be shown, you would have to be root to see it all.)
tcp 0 0 127.0.0.1:40108 127.0.0.1:6379 TIME_WAIT -
tcp 0 0 127.0.0.1:40116 127.0.0.1:6379 TIME_WAIT -
tcp 0 0 127.0.0.1:40112 127.0.0.1:6379 TIME_WAIT -