首页 > 数据库 >nginx+lua+redis实现广告缓存

nginx+lua+redis实现广告缓存

时间:2022-10-27 10:58:06浏览次数:70  
标签:ad nginx redis lua position local

需求分析#

需要在页面上显示广告的信息。

OpenResty#

OpenResty介绍#

OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,由中国人章亦春发起,提供了很多高质量的第三方模块。

OpenResty 是一个强大的 Web 应用服务器,Web 开发人员可以使用 Lua 脚本语言调动 Nginx 支持的各种 C 以及 Lua 模块,更主要的是在性能方面,OpenResty可以 快速构造出足以胜任 10K 乃至1000K以上并发连接响应的超高性能 Web 应用系统。

360,UPYUN,阿里云,新浪,腾讯网,去哪儿网,酷狗音乐等都是 OpenResty 的深度用户。

OpenResty 简单理解,就相当于封装了nginx,并且集成了LUA脚本,开发人员只需要简单的其提供了模块就可以实现相关的逻辑,而不再像之前,还需要在nginx中自己编写lua的脚本,再进行调用了。

OpenResty安装#

linux安装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#

默认已经安装好了nginx,在目录:/usr/local/openresty/nginx 下。

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

#user nobody; 配置文件第一行原来为这样, 现改为下面的配置
user root root;

测试访问 http://192.168.200.128

实现思路#

表结构分析#

tb_ad (广告表)

字段名称字段含义字段类型字段长度备注
id ID INT    
name 广告名称 VARCHAR    
position 广告位置 VARCHAR   系统定义
start_time 开始时间 DATETIME    
end_time 到期时间 DATETIME    
status 状态 CHAR   0:无效 1:有效
image 图片地址 VARCHAR    
url URL VARCHAR    
remarks 备注 VARCHAR    
web_index_lb 首页轮播图      
web_index_amusing 有趣区      
web_index_ea_lb 家用电器楼层轮播图      
web_index_ea 家用电器楼层广告      
web_index_mobile_lb 手机通讯楼层轮播图      
web_index_mobile 手机通讯楼层广告      

缓存预热与二级缓存查询#

步骤一:编写lua脚本实现缓存预热(将mysql里的数据查询出来存入redis)

步骤二:编写lua脚本实现二级缓存读取

代码实现#

缓存预热#

实现思路:

定义请求:用于查询数据库中的数据更新到redis中。

(1)连接mysql ,按照广告分类ID读取广告列表,转换为json字符串。

(2)连接redis,将广告列表json字符串存入redis 。

定义请求:

复制代码
请求:
    /ad_update
参数:
    position  --指定广告位置
返回值:
    json
复制代码

在/root/lua目录下创建ad_load.lua ,实现连接mysql 查询数据 并存储到redis中。

复制代码
ngx.header.content_type="application/json;charset=utf8"
local cjson = require("cjson")
local mysql = require("resty.mysql")
local uri_args = ngx.req.get_uri_args()
local position = uri_args["position"]
​
local db = mysql:new()
db:set_timeout(1000)  
local props = {  
    host = "192.168.200.128",  
    port = 3306,  
    database = "changgou_business",  
    user = "root",  
    password = "root"  
}
​
local res = db:connect(props)  
local select_sql = "select url,image from tb_ad where status ='1' and position='"..position.."' and start_time<= NOW() AND end_time>= NOW()"  
res = db:query(select_sql)  
db:close()  
​
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)
​
local ip ="192.168.200.128"
local port = 6379
red:connect(ip,port)
​
red:set("ad_"..position,cjson.encode(res))
red:close()
​
ngx.say("{flag:true}")
复制代码

修改/usr/local/openresty/nginx/conf/nginx.conf文件:

代码如下:

复制代码
#user  nobody;
user root root;
worker_processes  1;
​
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
​
#pid        logs/nginx.pid;
​
events {
    worker_connections  1024;
}
​
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    #tcp_nopush     on;
​
    #keepalive_timeout  0;
    keepalive_timeout  65;
​
    #gzip  on;
​
    server {
        listen       80;
        server_name  localhost;
        charset utf-8;
        #access_log  logs/host.access.log  main;
        # 添加
        location /ad_update {
            content_by_lua_file /root/lua/ad_update.lua;
        }
        
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }        
    }
}
复制代码

重新启动nginx

测试:http://192.168.200.128/ad_update?position=web_index_lb

广告缓存读取#

实现思路:

通过lua脚本直接从redis中获取数据即可。

定义请求:

请求:/ad_read
参数:position
返回值:json

在/root/lua目录下创建ad_read.lua

复制代码
ngx.header.content_type="application/json;charset=utf8"
​
local uri_args = ngx.req.get_uri_args();
local position = uri_args["position"];
​
local redis = require("resty.redis");
​
local red = redis:new()
​
red:set_timeout(2000)
​
local ok, err = red:connect("192.168.200.128", 6379)
​
local rescontent=red:get("ad_"..position)
​
ngx.say(rescontent)
​
red:close()
复制代码

在/usr/local/openresty/nginx/conf/nginx.conf中server下添加配置

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

测试 http://192.168.200.128/ad_read?position=web_index_lb 输出

[{"url":"img\/banner1.jpg","image":"img\/banner1.jpg"},{"url":"img\/banner2.jpg","image":"img\/banner2.jpg"}]

二级缓存-加入openresty本地缓存#

如上的方式没有问题,但是如果请求都到redis,redis压力也很大,所以我们一般采用多级缓存的方式来减少下游系统的服务压力。

先查询openresty本地缓存 如果没有再查询redis中的数据

  • 修改/root/lua目录下ad_read文件, 内容如下:
复制代码
ngx.header.content_type="application/json;charset=utf8"
local uri_args = ngx.req.get_uri_args();
local position = uri_args["position"];
local cache_ngx = ngx.shared.dis_cache;
local adCache = cache_ngx:get('ad_cache_'..position);
if adCache == "" or adCache == nil then
    local redis = require("resty.redis");
    local red = redis:new()
    red:set_timeout(2000)
    local ok, err = red:connect("192.168.200.128", 6379)
    local rescontent=red:get("ad_"..position)
    ngx.say(rescontent)
    red:close()
    cache_ngx:set('ad_cache_'..position, rescontent, 10*60);
else
    ngx.say(adCache)
end
复制代码
  • 修改nginx配置文件vi /usr/local/openresty/nginx/conf/nginx.conf ,http节点下添加配置:
#包含redis初始化模块
lua_shared_dict dis_cache 5m;  #共享内存开启

前端页面实现(了解)#

(1)修改index.html,编写脚本

复制代码
<script>
       new Vue({
        el: '#app',
        data: {
            ad: {
                web_index_lb:[]
            }
        },
        methods: {
            adRead: function(position) {
                axios.get('ad_read?position='+position).then(response =>{
                    this.ad[position]=response.data                 
                })
            }
        },
        created(){
            this.adRead('web_index_lb')
        }
      })
</script>
复制代码

在页面上添加<div id='app'> ... </div>

(2)修改index.html,渲染广告轮播图

复制代码
<div id="myCarousel" data-ride="carousel" data-interval="4000" class="sui-carousel slide">
    <ol class="carousel-indicators">
        <li data-target="#myCarousel" data-slide-to="0" class="active" v-for="item in ad.web_index_lb"></li>
    </ol>
    <div class="carousel-inner" id="lbt">
        <div class="item" v-for="item in contentList">
            <a :href="item.url">
            <img :src="item.pic"  />
          </a>
        </div>      
    </div>
    <a href="#myCarousel" data-slide="prev" class="carousel-control left">‹</a>
    <a href="#myCarousel" data-slide="next" class="carousel-control right">›</a>
</div>
复制代码

(3)上传至服务器并测试

更改

复制代码
# 加载首页
        location / {
            root   html;
            index  index.html index.htm;
        }
复制代码

 

标签:ad,nginx,redis,lua,position,local
From: https://www.cnblogs.com/smallfa/p/16831392.html

相关文章

  • nginx限流+jmeter
    一般情况下,首页的并发量是比较大的,即使有了多级缓存,如果有大量恶意的请求,也会对系统造成影响。而限流就是保护措施之一。nginx提供两种限流的方式:一是控制速率二是控......
  • Rsyslog 收集 Redis 及第三方日志简单记录
    背景​ 现在服务器数量较多,经常需要做日志收集。耳熟能详的方案是ELK和后起之秀Loki,但是有的时候项目组的资源有限,用这些方案稍显笨重,所以这时候需要操作系统自带的Rs......
  • Redis
    1.Redis官网redis.io2.操作流程2.1下载版本5.0.14cd/data/#下载wgethttps://download.redis.io/releases/redis-5.0.14.tar.gz#解压tarxfredis-5.0.14.tar.g......
  • nginx 常用命令
    命令查看nginx版本号命令/usr/local/nginx-v查看nginx安装的参数/安装的模块本质就是看编译安装中./configure步骤添加的参数命令/usr/local/nginx-V发......
  • 一篇文章带你了解NoSql数据库——Redis简单入门
    一篇文章带你了解NoSql数据库——Redis简单入门Redis是一个基于内存的key-value结构数据库我们会利用其内存存储速度快,读写性能高的特点去完成企业中的一些热门数据的储......
  • Redis持久化的研究---AOF
    Redis持久化的研究Redis的持久化的方式----AOF在redis中存在一个日志文件(aof日志),此日志文件中保存着在redis中执行的有效的命令;当需要进行数据恢复时就会加载该文件......
  • 如何修改 Nginx 源码实现 worker 进程隔离
    背景最近我们线上网关替换为了APISIX,也遇到了一些问题,有一个比较难解决的问题是APISIX的进程隔离问题。APISIX不同种类请求的互相影响首先我们遇到的就是APISIXPromet......
  • 使用nginx limit_except 保护暴露外网的minio 服务
    minio做为比较热门的开源s3服务,受到好多团队的青睐,使用率还是比较高的,如果我们的服务是部署在内网的还好一般不用太担忧安全问题(但是也得做好内网的安全防护),但是如果直......
  • Nginx初接触
    Nginx的反向代理与负载均衡以及动静分离正向代理与反向代理正向代理代理客户端反向代理代理服务器**Nginx提供的负载均衡策略有2种:内置策略和扩展策略。内置策略为轮......
  • nginx 又出现了 404
    我使用下面的nginx配置, 看起来好像没问题:server{listen80;#listensomename:80;#server_namesomenamealiasanother.alias;......