OpenResty+nginx+lua+redis实现二级缓存
382 字
2 分钟
OpenResty+nginx+lua+redis实现二级缓存
-
下载OpenResty镜像
Terminal window docker pull openresty/openresty -
运行镜像
Terminal window docker run -d -p 880:80 --name openresty --restart always -v /etc/localtime:/etc/localtime openresty/openresty -
进入容器
Terminal window docker exec -it openresty /bin/bash -
安装lua
apt-get update -yapt-get install -y gccapt-get install -y curlapt-get install -y makeapt-get install -y vimapt-get install libreadline-dev -ycurl -R -O http://www.lua.org/ftp/lua-5.3.5.tar.gztar -zxf lua-5.3.5.tar.gzcd lua-5.3.5make linux testmake install -
编写lua脚本并上传到服务器
ngx.header.content_type = "application/json;charset=utf8"--获取本地缓存local cache_ngx = ngx.shared.dis_cache;-- 获取本地缓存数据local ngCache = cache_ngx:get('lua:application-data')if ngCache == "" or ngCache == nil then--引入redis库local cjson = require("cjson")local redis = require("resty.redis")local red = redis:new()red:set_timeout(2000)local ip = "127.0.0.1"local port = 6379red:connect(ip, port)red:auth("123456")local redCache = red:get("lua:application-data")if redCache == ngx.null or redCache == "" or redCache == nil then--引入mysql库local mysql = require("resty.mysql")local db = mysql:new()db:set_timeout(3000)local props = {host = "127.0.0.1",port = 3306,database = "test",user = "root",password = "root"}local res = db:connect(props)local select_sql = "SELECT xxx from tb_level where xxx"res = db:query(select_sql)db:close()red:set("lua:application-data", cjson.encode(res))red:close()--将redis中获取到的数据存入nginx本地缓存10分钟 测试阶段先1分钟cache_ngx:set('lua-game-milk-tea:level-data', cjson.encode(res), 1 * 60)ngx.say("{\"code\":0,\"message\":\"sql success\",\"data\":" .. cjson.encode(res) .. "}")else--将redis中获取到的数据存入nginx本地缓存10分钟 测试阶段先1分钟cache_ngx:set('lua:application-data', redCache, 1 * 60)ngx.say("{\"code\":0,\"message\":\"redis success\",\"data\":" .. redCache .. "}")endelsengx.say("{\"code\":0,\"message\":\"nginx success\",\"data\":" .. ngCache .. "}")end-
特别说明
lua读取redis数据返回结果为空时,返回的结果不是nil而是userdata类型的ngx.null
-
-
在http节点下开启nginx共享内存
Terminal window vim /usr/local/openresty/nginx/conf/nginx.conflua_shared_dict dis_cache 10m; -
在server节点添加lua配置
location /lua {content_by_lua_file /opt/lua/cache.lua;add_header Access-Control-Allow-Origin *;add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS';add_header Access-Control-Allow-Headers '*';}
1624507084958 
1624507099317 
1624507152639
文章分享
如果这篇文章对你有帮助,欢迎分享给更多人!
OpenResty+nginx+lua+redis实现二级缓存
https://tianch.com.cn/posts/tutorials/openresty-nginx-lua-redis-shi-xian-er-ji-huan-cun/相关文章智能推荐
1
SpringBoot3+MySQL+OpenResty+Lua实现三级缓存架构
Nginx从零搭建三级缓存架构:OpenResty共享内存缓存(L1)+Redis缓存(L2)+MySQL(通过SpringBoot3接口兜底),手写Lua三级缓存脚本,详解缓存一致性、穿透/击穿/雪崩防护,附完整可运行的Nginx配置、Lua脚本与SpringBoot3后端代码。
2
Nginx性能优化实战:从压测诊断到参数调优
Nginx以“先测量、再调优、再验证”为主线,手把手优化Nginx性能:用ab/wrk定位瓶颈,逐项调优worker进程模型、sendfile与零拷贝、gzip压缩、open_file_cache、keepalive、proxy_cache反向代理缓存、HTTP/2,附优化前后对比方法与防踩坑指南。
3
FRP v0.68.0 内网穿透完整教程
Nginx文档版本:v1.0 | 适用 FRP 版本:v0.68.0 | 更新日期:2026-07-31
4
布隆过滤器详解:原理、误判率与实战应用
中间件深入讲解布隆过滤器的原理:位数组+多个哈希函数如何实现O(k)判断,推导误判率公式与最优参数,对比Bitmap/HyperLogLog/Cuckoo Filter,手写Java实现并用Guava、Redis、Redisson实战缓存穿透防护、URL去重、黑名单拦截等场景。
5
Redis缓存穿透、缓存击穿、缓存雪崩的区别和解决方案
中间件深入剖析Redis缓存穿透、缓存击穿、缓存雪崩三者的区别与产生原因,并给出缓存空值、布隆过滤器、互斥锁、逻辑过期、过期时间随机化、多级缓存、高可用集群等解决方案,附Java代码示例。
随机文章随机推荐


