使用nginx+lua+Redis实现缓存预热、二级缓存查询、多级缓存查询

使用nginx+lua+Redis实现缓存预热、二级缓存查询、多级缓存查询

安装好Lua、Redis、openresty(默认安装了nginx)。
lua安装
在linux系统中执行下面的命令

1.yum install ‐y gcc 
2.yum install libtermcap‐devel ncurses‐devel libevent‐devel readline‐devel 
3.curl ‐R ‐O http://www.lua.org/ftp/lua‐5.3.5.tar.gz 
4.tar ‐zxf lua‐5.3.5.tar.gz 
5.cd lua‐5.3.5 
6.make linux test 
7.make install

OpenResty安装

1.yum install yum‐utils yum‐config‐manager ‐‐add‐repo
2.https://openresty.org/package/centos/openresty.repo
3.yum install openresty

OpenResty(又称:ngx_openresty) 是一个基于 NGINX 的可伸缩的 Web 平台,提供了很多高质量的第三方模块
OpenResty 简单理解,就相当于封装了nginx,并且集成了LUA脚本,开发人员只需要简 单的其提供了模块就可以实现相关的逻辑,而不再像之前,还需要在nginx中自己编写 lua的脚本,再进行调用了。

缓存预热
查询数据库中的信息更新到Redis中

1.编写lua脚本
实现连接mysql,查询数据,并存储到Redis中
脚本文件名 ad_update.lua(注:nginx通过文件名加载lua脚本)

ngx.header.content_type="application/json;charset=utf8"  #表明进行json数据传递
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()  #开启mysql新的连接
db:set_timeout(1000) 
local props = {     #数据库信息
	host = "192.168.200.128", 
	port = 3306, 
	database = "ad_business", 
	user = "root", 
	password = "12121" }
	
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()"  #连接到mysql需要执行的语句
res = db:query(select_sql) 
db:close() 

local redis = require("resty.redis")  #引入模块
local red = redis:new() #建立Redis连接
red:set_timeout(2000) 
local ip ="192.168.200.128" 

local port = 6379 red:connect(ip,port) red:set("ad_"..position,cjson.encode(res))  #设置存放Redis的内容
red:close()  #关闭Redis连接
ngx.say("{flag:true}") #lua脚本执行完成之后,返回true

2.修改Nginx配置文件
修改/usr/local/openresty/nginx/conf/nginx.conf文件
将下面注释 #添加 的代码 复制到nginx.conf文件相应位置

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; }   #加载特定lua文件
}

二级缓存查询
通过lua脚本直接从Redis中读取数据

1.编写lua脚本
文件名ad_read.lua

ngx.header.content_type="application/json;charset=utf8"
1ocal uri_args = ngx.req.get_uri_args();
local position = uri_args["position'"];
local redis = require("resty.redis");  #引入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()

多级缓存查询配置

ngx.header.content_type="application/json;charset=utf8"
1ocal uri_args = ngx.req.get_uri_args();
1ocal position = uri_args["position"];
local cache_ngx = ngx.shared.dis_cache;  #开启openresty本地缓存
local adCache = cache_ngx:get('ad_cache_'..position) ;
if adCache =="" or adCache == nil then   #如果本地缓存没有数据,则连接Redis查询数据
	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

2.修改nginx.conf配置文件(在配置文件server下添加以下配置)

location /ad_read {
	content_by_lua_file /root/ 1ua/ad_read.lua;

**注:**二级缓存存在一定的缺陷,当所有请求直接发送到Redis,请求过多时,Redis服务器压力会过大。为了降低Redis服务器的压力,便可以使用多级缓存,多级缓存配置后,请求首先会到本地缓存查询有没有所需数据,如果没有,则会去Redis中查询,查询到数据后,将数据保存到本地并返回数据。


版权声明:本文为weixin_43783003原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。