使用nginx+lua来读取redis

以下单机测试,并发5000正常跑

local redis = require "resty.redis"  
local cjson = require "cjson"  
local red = redis:new()

red:set_timeout(50) -- 1 sec  
red:set_keepalive(10000, 100) -- 长链接,保持10秒,100个连接数,应对并发  
out = {}  
out["success"] = false  
out["result"]={}  
local ok, err = red:connect("192.168.10.106", 6379)  
if not ok then  
    out["status"]=10001
    out["message"]="connect to redis server failed"
    ngx.print(cjson.encode(out))
    return
end  
red:auth("00000000")  
red:init_pipeline()  
red:lrange("IndexScrollInvestListF",0,100)  
red:lrange("IndexScrollRegisterListF",0,100)  
red:get("TotalRegisterUser")  
red:get("PlatformInvestedAmountTotal")  
local results, err = red:commit_pipeline()  
if not results then  
    ngx.say(cjson.encode("failed to commit the pipelined requests"))
    return
end  
--  redis读取完毕,由于是长链接,所以不用关闭即可,剩下的工作就是输出为json
out["success"] = true

out["result"]["_ts"] = ngx.time()  
out["result"]["rt"] = results[3]  
out["result"]["pa"] = results[4]  
out["result"]["sl"] = {}  
out["result"]["sr"] = {}

for i,res in ipairs(results[1]) do  
    t = cjson.decode(res)
    t["b"] = t["investMoney"]
    t["a"] = t["ageInterest"]
    t["p"] = t["projectId"]
    t["m"] = t["moblie"]
    t["u"] = t["userId"]
    t["pt"] = t["projectType"]
    t["investMoney"]=nil
    t["ageInterest"]=nil
    t["projectId"]=nil
    t["moblie"]=nil
    t["userId"]=nil
    t["projectType"]=nil
    table.insert(out["result"]["sl"],t)
end  
for i,res in ipairs(results[2]) do  
    t = cjson.decode(res)
    t["m"] = t["moblie"]
    t["u"] = t["userId"]
    t["r"] = t["rewardMoney"]
    t["moblie"]=nil
    t["userId"]=nil
    t["rewardMoney"]=nil
    -- table.remove(t,t["moblie"])
    table.insert(out["result"]["sr"],t)
end  
ngx.say(cjson.encode(out))  
浙ICP备16045237号-1