二、OpenResty实现缓存(nginx缓存)

您所在的位置:网站首页 实现缓存 二、OpenResty实现缓存(nginx缓存)

二、OpenResty实现缓存(nginx缓存)

2023-08-09 09:42| 来源: 网络整理| 查看: 265

上一篇:OpenResty(lua脚本读mysql,写入redis):https://blog.csdn.net/qq_43220949/article/details/113784003)

这一篇是紧跟上一篇写的,上一篇实现了使用openresty的nginx转发请求到lua脚本执行,lua脚本查询mysql,存入redis。

使用openresty实现缓存(nginx缓存) nginx.conf修改 # 定义nginx缓存模块 dis_cache lua_shared_dict dis_cache 5m; server { listen 80; server_name localhost; location /update { content_by_lua_file /root/lua/68/ad_update.lua; } # 用户请求read?id=1,将该请求转到/root/lua/68/ad_update.lua脚本处理 location /read { content_by_lua_file /root/lua/68/ad_read.lua; } .... } 定义nginx缓存空间用户请求read?id=1,将该请求转到ad_update.lua脚本处理 /root/lua/68/ad_read.lua ngx.header.content_type="application/json;charset=utf8" --获取请求中的参数ID local uri_args = ngx.req.get_uri_args(); local position = uri_args["id"]; --获取nginx本地缓存dis_cache local cache_ngx = ngx.shared.dis_cache; --根据ID 获取nginx本地缓存数据 local adCache = cache_ngx:get('ad_cache_'..position); if adCache == "" or adCache == nil then --引入redis库 local redis = require("resty.redis"); --创建redis对象 local red = redis:new() --设置超时时间 red:set_timeout(2000) --连接 local ok, err = red:connect("127.0.0.1", 6379) --获取key的值 red:auth("root") local rescontent=red:get("ad_"..position) --输出到返回响应中 ngx.say(rescontent) --关闭连接 red:close() --将redis中获取到的数据存入nginx本地缓存模块,缓存存活时间为10*60s(即10分钟) cache_ngx:set('ad_cache_'..position, rescontent, 10*60); else --nginx本地缓存中获取到数据直接输出 ngx.say(adCache) end 加载nginx配置文件 #进入nginx的sbin文件夹 /usr/local/openresty/nginx/sbin cd /usr/local/openresty/nginx/sbin #重新加载nginx ./nginx -s reload

访问http://127.0.0.1/read?id=1,即可将redis数据加入nginx缓存。 第一次访问成功后,关闭redis,可以看到该页面仍然可以正常访问。 访问http://127.0.0.1/read?id=1前,需要访问http://127.0.0.1/update?id=1,将数据加载入redis才可以

redis启动、关闭 # 查看redis是否在运行 ps aux | grep redis # 启动redis /etc/init.d/redis start # 关闭redis,[password]是redis密码 redis-cli -a [password] shutdown mysql库表数据 CREATE DATABASE /*!32312 IF NOT EXISTS*/`changgou_content` /*!40100 DEFAULT CHARACTER SET utf8mb4 */; USE `changgou_content`; /*Table structure for table `tb_content` */ DROP TABLE IF EXISTS `tb_content`; CREATE TABLE `tb_content` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `category_id` bigint(20) NOT NULL COMMENT '内容类目ID', `title` varchar(200) DEFAULT NULL COMMENT '内容标题', `url` varchar(500) DEFAULT NULL COMMENT '链接', `pic` varchar(300) DEFAULT NULL COMMENT '图片绝对路径', `status` varchar(1) DEFAULT NULL COMMENT '状态,0无效,1有效', `sort_order` int(11) DEFAULT NULL COMMENT '排序', PRIMARY KEY (`id`) USING BTREE, KEY `category_id` (`category_id`) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC; /*Data for the table `tb_content` */ insert into `tb_content`(`id`,`category_id`,`title`,`url`,`pic`,`status`,`sort_order`) values (28,1,'京东广告','http://www.itheima.com','http://192.168.211.128/group1/M00/00/03/wKjTgFucGIOECJMPAAAAAKbBPuo782.jpg','1',1), (29,1,'深圳真美北京不错南京大美丽','http://www.itheima.com','http://www.itheima.com','1',1), (30,2,'你吃饭了吗','http://www.itheima.com','http://www.itheima.com','1',1), (31,2,'你的衣服好漂亮','http://www.itheima.com','http://www.itheima.com','1',1); 总的lua脚本 先从nginx缓存读取,有则直接输出nginx缓存没有,则从redis读取,redis有则加入nginx缓存,并输出redis没有则mysql读,读完加入redis ngx.header.content_type="application/json;charset=utf8" --获取请求中的参数ID local uri_args = ngx.req.get_uri_args(); local position = uri_args["id"]; --获取nginx本地缓存dis_cache local cache_ngx = ngx.shared.dis_cache; --根据ID 获取nginx本地缓存数据 local adCache = cache_ngx:get('ad_cache_'..position); --((((((((((((((((((((((((((((((((( 如果nginx本地缓存为空,加载redis)))))))))))))))))))))))))))))))) if adCache == "" or adCache == nil then --引入redis库 local redis = require("resty.redis"); --创建redis对象 local red = redis:new() --设置超时时间 red:set_timeout(2000) --连接 local ok, err = red:connect("127.0.0.1", 6379) --获取key的值 red:auth("root") rescontent=red:get("ad_"..position); --((((((((((((((((((((((((((((((( 如果redis为空,加载mysql,并添加进redis))))))))))))))))))))))))))) if rescontent == ngx.null then local cjson = require("cjson"); local mysql = require("resty.mysql"); local db = mysql:new(); db:set_timeout(1000) local props = { host = "127.0.0.1", port = 3306, database = "changgou_content", user = "root", password = "root" } local res_mysql = db:connect(props) local select_sql = "SELECT url,pic FROM tb_content WHERE STATUS ='1' AND category_id =3 ORDER BY sort_order"; res_d= db:query(select_sql) local responsejson = cjson.encode(res_d); red:set("ad_"..position,responsejson); ngx.say(responsejson); db:close() else --将redis中获取到的数据存入nginx本地缓存 cache_ngx:set('ad_cache_'..position, rescontent, 10*60); ngx.say(rescontent) end --关闭连接 red:close() else --nginx本地缓存中获取到数据直接输出 ngx.say(adCache) end

上一篇:OpenResty(lua脚本读mysql,写入redis):https://blog.csdn.net/qq_43220949/article/details/113784003)

这一篇是紧跟上一篇写的,上一篇实现了使用openresty的nginx转发请求到lua脚本执行,lua脚本查询mysql,存入redis。



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3