1.需求概述
一些网站,比如首页,在大量用户存在的时候,很高并发的时候,在页面数据不怎么变化的情况下,访问数据库很频繁,造成数据库压力过大,这个时候就需要对这部分不怎么变化的数据缓存起来
2.Lua+Nginx配置
2.1 实现思路
- 查询Nginx缓存,如果无缓存,则直接将缓存中的广告返回
- 如果Nginx缓存中没有广告数据,则通过Lua脚本查询Redis,如果Redis有数据,则会将数据存入到Nginx的缓存,并返回查询到的数据
- 如果Redis中也没有缓存,则此时会通过Lua脚本查询Mysql,如果Mysql中有数据,则将数据存入到Redis中并返回查询到的数据
2.2 Lua流程
- 连接mysql,安装广告分类Id读取广告列表,转换为json字符串
- 连接redis,将广告json字符串存入redis中
2.2.3 更新广告脚本
http://192.168.xxx.xxx/update_content?id=1
下面目录下出口update_content.lua脚本
/usr/local/server/lua65
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 id = uri_args["id"]
local db = mysql:new()
db:set_timeout(1000)
local props = {
host = "192.168.xxx.xxx",
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)
db:close()
local redis = require("resty.redis")
local red = redis:new()
red:set_timeout(2000)
local ip ="192.168.xxx.xxx"
local port = 6379
red:connect(ip,port)
red:set("content_"..id,cjson.encode(res))
red:close()
ngx.say("{flag:true}")
2.2.3.1 修改nginx配置
usr/local/openresty/nginx/conf/nginx.conf
添加头信息和localhost信息
server {
listen 80;
server_name localhost;
location /update_content {
content_by_lua_file /root/lua/update_content.lua;
}
}
定义lua缓存命名空间,修改nginx.conf,添加如下代码
lua_shared_dict dis_cache 128m;
2.2.3.2 测试
2.2.4 获取广告脚本
在下面目录层级read_content.lua
/usr/local/server/lua65
--设置响应头类型
ngx.header.content_type="application/json;charset=utf8"
--获取请求中的参数ID
local uri_args = ngx.req.get_uri_args();
local id = uri_args["id"];
--引入redis库
local redis = require("resty.redis");
--创建redis对象
local red = redis:new()
--设置超时时间
red:set_timeout(2000)
--连接
local ok, err = red:connect("192.168.211.132", 6379)
--获取key的值
local rescontent=red:get("content_"..id)
--输出到返回响应中
ngx.say(rescontent)
--关闭连接
red:close()
2.2.4.1 修改nginx配置
usr/local/openresty/nginx/conf/nginx.conf
添加localhost信息
location /read_content {
content_by_lua_file /root/lua/read_content.lua;
}
2.2.4.2 openresty本地缓存
如果请求都到redis,redis压力也很大,我们一般采用多级缓存思路减少下游压力。
- 先查询openresty本地缓存,如果没有
- 在查询redis 中的数据,如果没有
- 在查询mysql中的数据,有数据,则返回
2.2.4.3 修改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;
--根据ID 获取本地缓存数据
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.211.132", 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.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);
ngx.say(rescontent)
end
red:close()
else
ngx.say(contentCache)
end