采用OpenResty给首页广告位做缓存

OpenResty是国人章亦春发起的一个开源项目,可以理解成封装了nginx,并且集成了LUA脚本,足以胜任 10K 乃至 1000K 以上单机并发连接。

首页的访问频率一般都比较高,对于首页上的广告位,广告变更频率比较低,可以利用缓存提高访问速度

 

广告位缓存架构图

 

openresty安装

1》添加仓库

  yum install yum-utils

  yum-config-manager --add-repo https://openresty.org/package/centos/openresty.repo

2》执行安装

  yum install openresty

3》安装成功后会在默认的目录下

  /usr/local/openresty

 安装成功后默认启动了nginx进程,访问域名就可以看到默认的首页了。

 

openresty的启动,停止,重启

cd    /usr/local/openresty/nginx  

停止  ./sbin/nginx  -s stop

启动  ./sbin/nginx  -c conf/nginx.conf

重启  ./sbin/nginx  -s reload

 

设置缓存

1)  修改/usr/local/openresty/nginx/conf/nginx.conf,将配置文件使用的根设置为root,目的就是将来要使用lua脚本的时候 ,直接可以加载在root下的lua脚本。

 

 2) 定义lua缓存命名空间大小

 

 3)新建lua脚本文件  /root/lua/read_content.lua  , 内容如下

ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
local cache_ngx = ngx.shared.dis_cache;
local contentCache = cache_ngx:get('content_cache_'..id);

if contentCache == "" or contentCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    red:connect("192.168.200.128", 6379)
    local rescontent=red:get("content_"..id);

    if ngx.null == rescontent then
        local cjson = require("cjson");
        local mysql = require("resty.mysql");
        local db = mysql:new();
        db:set_timeout(2000)
        local props = {
            host = "192.168.200.128",
            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);
        red:expire("content_"..id,3*60);
        ngx.say(responsejson);
        db:close()
    else
        cache_ngx:set('content_cache_'..id, rescontent, 2*60);
        ngx.say(rescontent)
    end
    red:close()
else
    ngx.say(contentCache)
end

4)在/usr/local/openresty/nginx/conf/nginx.conf中配置如下

location /read_content {
     content_by_lua_file /root/lua/read_content.lua;
}

5)前端访问地址为http://192.168.200.128/read_content?id=1

 这样就实现了首页广告位的lua缓存+redis缓存 , lua的过期时间为2分钟,redis的为3分钟。当修改数据库记录后,什么都不用更改,缓存失效后会自动同步为mysql中的数据。

 

 

 

 

  

posted @ 2020-02-25 19:43  踏月而来  阅读(406)  评论(0编辑  收藏  举报