Skip to content

Instantly share code, notes, and snippets.

@Ryan4CN
Last active October 2, 2015 18:04
Show Gist options
  • Save Ryan4CN/d9e5a8b4401c1bfeafdb to your computer and use it in GitHub Desktop.
Save Ryan4CN/d9e5a8b4401c1bfeafdb to your computer and use it in GitHub Desktop.
-- nginx lua 模块缓存
cjson = require "cjson"
redis = require "resty.redis"
if ngx.req.get_headers()['PARAMS'] == nil then
ngx.say('params error')
return
end
j=cjson.new().decode(ngx.req.get_headers()["PARAMS"])
red = redis:new()
red:set_timeout(1000)
ok, err = red:connect("127.0.0.1", 6379)
if not ok then
ngx.log("LUA REDIS: ", err)
return
end
akey = j.channel..j.version_code
res, err = red:get(akey)
if not res or res == ngx.null then
lres = ngx.location.capture("/real/api/base")
res = lres.body
red:set(akey, res)
red:expire(akey, 360)
end
ngx.say(res)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment