Hello!
On Tue, Oct 16, 2012 at 2:22 AM, xocecd wrote:
> 我用resty-redis写了一个连接redis的测试程序,发现connect后,连接就中断了,导致后续set,get操作无法进行。
> nginx.conf:
> location /get {
> default_type text/html;
> set $ip $arg_ip;
> set $key $arg_key;
> content_by_lua '
> local redis = require "resty.redis";
> local res = redis:new();
> local ok = res:set_timeout(5000);
> ngx.say(ok);
>
> local ok,err = res:connect(ngx.var.ip,6379);
> if not ok then
> ngx.say("failed to connect",err);
> return;
> end
> res:set_keepalive(0, 100);
你这里的 lua-resty-redis set_keepalive 方法的用法是错误的,因为 set_keepalive
方法是把当前连接放入连接池中,这样你后续就不能再操作当前对象了,效果类似你调用 close()
方法(只是连接并不关闭,而是放入池中)。引用文档中的原话是:
"Puts the current Redis connection into the ngx_lua cosocket
connection pool...Only call this method in the place you would have
called the close method instead. Calling this method will immediately
turn the current redis object into the closed state. Any subsequent
operations on the current objet will return the closed error."
更多细节可以参见
https://github.com/agentzh/lua-resty-redis#set_keepalive
如果你仔细看文档中的示例的话,set_keepalive 方法也是在使用完后才调用的 :)
Best regards,
-agentzh