栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

关于nginx 的配置lua

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

关于nginx 的配置lua

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
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/844887.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号