好吧,是我自己没看文档
Attempts to connect to the remote host and port that the redis server
is listening to or a local unix domain socket file listened by the
redis server.
Before actually resolving the host name and connecting to the remote
backend, this method will always look up the connection pool for matched
idle connections created by previous calls of this method.
An optional Lua table can be specified as the last argument to this method to specify various connect options:
On Monday, November 17, 2014 12:50:53 PM UTC+8, 王二伟 wrote:
我的做法
nginx.cof
http段增加
include /Users/venkman/data/server/
openresty/nginx/conf/lua.conf;
init_by_lua '
redis_conn = nil
function redis_record(key_name, key_value)
local ok, err
if redis_conn == nil then
local redis = require "resty.redis"
redis_conn = redis:new()
ok, err = redis_conn:connect("127.0.0.1", 8888)
if not ok then
ngx.say("error connect")
return false
end
end
ok, err = redis_conn:set(key_name, key_value)
if not ok then
ngx.say(err)
return false
end
return true
end
';
server段
location / {
default_type 'text/plain';
content_by_lua '
if ngx.var.is_args ~= "" then
if ngx.var.arg_log ~= nil then
local ok = redis_record("log", ngx.var.arg_log)
if ok then
ngx.say("todo")
else
ngx.say("error")
end
else
ngx.say("error arg name")
end
else
ngx.say("error")
end
';
}
测试发现第二次请求后redis_conn就断开了,请教如何保持redis的一次连接多次使用