ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"]; --获取请求中的id
--获取本地缓存
local cache_ngx = ngx.shared.dis_cache; -- 加载nginx缓存模块
--根据ID 获取本地缓存数据
local contentCache = cache_ngx:get('content_cache_'..id); --在nginx中找缓存
if contentCache == "" or contentCache == nil then --查看在nginx 是不是空无效的
local redis = require("resty.redis");
local red = redis:new()
red:set_timeout(2000)
red:connect("192.168.211.132", 6379)
local rescontent=red:get("content_"..id); --在获取redis中的缓存数据
if ngx.null == rescontent then --判断redis中有没有缓存数据
local cjson = require("cjson");
local mysql = require("resty.mysql");
local db = mysql:new();
db:set_timeout(2000)
local props = {
host = "192.168.211.132",
port = 3306,
database = "changgou_content",
user = "root",
password = "123456"
}
local res = db:connect(props);
local select_sql = "select url,pic from tb_content where status ='1' and category_id="..id.." order by sort_order";
res = db:query(select_sql);
local responsejson = cjson.encode(res);
red:set("content_"..id,responsejson);
ngx.say(responsejson);
db:close()
else
cache_ngx:set('content_cache_'..id, rescontent, 10*60); --redis给ngin缓存10分钟
ngx.say(rescontent) --nginx输出redis的数据
end
red:close()
else
ngx.say(contentCache) --nginx有数据 不为空 nginx 输出
end
|