local _M = {}
local redis = require("resty.redis")
local HOST = "127.0.0.1"
local PORT = 6379
local DATABASE = 0
local TIMEOUT = 5000 --ms
local MAX_IDLE_TIME = 10000 --ms
local POOL_SIZE = 3000
function _M.getConn()
local conn, err = redis:new()
if not conn then
ngx.say("failed to new: ", err)
end
conn:set_timeout(TIMEOUT)
local ok, err = conn:connect(HOST, PORT)
if not ok then
ngx.say("failed to connect: ", err)
return
end
local res, err = conn:select(DATABASE)
if not res then
ngx.say("falied to select: ", err)
return
end
--conn:set_keepalive(MAX_IDLE_TIME, POOL_SIZE)
return conn
end
return _M
业务逻辑代码:(类似操作,业务逻辑比较长)
local redis = require("resty.redis")
local pool = require("project/redisConn")
local cjson = require("cjson")
local conn = pool.getConn()
--str = ""
--for i = 1, 77000 do
-- str = "d"..str
--end
--conn:hset("hello", "world", str)
for i = 1, 6 do
conn:init_pipeline()
for i = 1, 6 do
value, err= conn:hget("hello", "world")
end
conn:commit_pipeline()
end
if not value then
ngx.say(err)
return
end
ngx.say(value)
conn:set_keepalive(5000, 1000)。
火焰图:包括该例子的和实际业务的。可以看到主要集中在C:ngx_http_lua_socket_tcp_receive/send上。
在 2016年6月29日星期三 UTC+8下午5:10:14,Zexuan Luo写道:
可以考虑下用redis的pipeline+
lock或者lua脚本合并下hmget操作,减少IO的次数。
在 2016年6月28日星期二 UTC+8下午2:23:52,徐重峰写道:大家好:
我打算使用openresty建立接口服务,下面是一个测试例子:
* 一个请求包含325次redis的hmget操作,无其它耗CPU的操作;
nginx和redis的配置,resty.redis的连接池,超时配置都注意了,无问题;
但是QPS极低。
我在考虑是否由于单次请求包含过多redis的IO操作,导致lua协程频繁切换,影响性能?