Skip to content

Instantly share code, notes, and snippets.

@pahud
Last active January 3, 2016 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pahud/8470143 to your computer and use it in GitHub Desktop.
Save pahud/8470143 to your computer and use it in GitHub Desktop.
--package.path = package.path .. ';/opt/openresty/lualib/?.lua'
local redis = require "resty.redis"
local cjson = require "cjson"
local red_host = 'xxx.xxx.xxx.xxx'
local ts = os.time()
local red = redis:new()
--local tb_resp = {}
--local req_args = ngx.req.get_uri_args()
red:set_timeout(10000) -- 10 sec
local ok, err = red:connect(red_host, 6379)
function hset(key, field, value )
return red:hset(key, field, value)
end
res, err = red:zrevrangebyscore('chat:rcv:1' , "("..ts, 0, 'withscores', 'limit', 0, 100 )
res = red:array_to_hash(res)
--ngx.say( type(res) )
local threads = {}
for f,v in pairs(res) do
ngx.say(f..'=>'..v)
t = ngx.thread.spawn(hset, 'pahud:hash', f, v)
table.insert(threads, t)
end
for i = 1, #threads do
local ok, res = ngx.thread.wait(threads[i])
if not ok then
ngx.say('err - '.. res)
else
ngx.say('good ')
end
end
ngx.eof()
local ok, err = red:set_keepalive(300000, 50000)
if not ok then
print("[ERROR]failed to set redis keepalive: ", err)
return
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment