缓存这样写,可以承受10k到1000k的并发

废话少说,直接上干货

Dino开始说:

用到的4个工具:这里不讲Lua 和 OpenResty

  1. OpenResty
  2. Lua
  3. Redis
  4. Mysql

Lua+Redis+OpenResty+Mysql

	在高并发的环境下 ,网站的首页,是访问最高的地方,如果不实施高并发的缓存操作,服务的一系列问题会暴漏的淋漓尽致
缓存的预热(Lua实现脚本操作来进行 数据的传输)

在这里插入图片描述

Lua脚本:
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.xxx.xxx",  
    port = 3306,  
    database = "xxxxxxxx",  
    user = "root",  
    password = "xxxx"  
}
​
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.xxxx.xxx"
local port = 6379
red:connect(ip,port)
​
red:set("ad_"..position,cjson.encode(res))
red:close()
​
ngx.say("{flag:true}")
缓存的三级操作

在这里插入图片描述
一:发送请求到达OpenResty(自带nginx)
二:查看nginx_cache 是否有缓存,如果有则返回
三: nginx_cache 没有,通过Lua来进行访问Redis 中是否有缓存
四: 看Redis中是否有缓存,如果有缓存的话,会将数据通过Lua脚本 将数据保存在nginx_cache中
五: Redis 没有,通过Lua脚本进行Mysql查询
六: 查看Mysql ,将数据通过Lua脚本 将数据保存在Redis中去,进而保存在nginx_cache中去。

这里提供一下Lua脚本:
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.xxxx.xxx", 6379)
    local rescontent=red:get("ad_"..position)
    if rescontext =="" or rescontext ==nil then
    	//如果没有数据 则需要查数据库
    	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.xxxx.xxx",  
		    port = 3306,  
		    database = "xxxxx",  
		    user = "root",  
		    password = "rxxt"  
		}
		​
		local rescontext = 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()"  
		rescontext = db:query(select_sql)  
		db:close()  
	else	 //这里使将从数据库查到的数据保存在 Redis中去
		local redis = require("resty.redis")
		local red = redis:new()
		red:set_timeout(2000)
		​
		local ip ="192.168.xxxx.xx"
		local port = 6379
		red:connect(ip,port)
		​
		red:set("ad_"..position,cjson.encode(rescontext ))
		red:close()
		ngx.say("{flag:true}")
else
    ngx.say(adCache)
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值