• 采用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中的数据。

      

  • 相关阅读:
    PHP实反向代理-收藏
    微信小程序实例-获取当前的地理位置、速度
    Entity Framework Core 实现读写分离
    将ASP.NET Core应用程序部署至生产环境中(CentOS7)(转)
    Centos 7防火墙firewalld开放80端口
    Asp.net Core 使用Redis存储Session
    .net core 使用Autofac依赖注入
    .net core 1.0 实现负载多服务器单点登录
    用ASP.NET Core 1.0中实现邮件发送功能-阿里云邮件推送篇
    阿里大鱼.net core 发送短信
  • 原文地址:https://www.cnblogs.com/moris5013/p/12363378.html
Copyright © 2020-2023  润新知