OpenResty 中使用 ngx.share.DICT 完成内存存储
OpenResty Lua 大约 736 字方法介绍
ngx.share.DICT
是OpenResty
内置实现的本地内存库。
使用HUP
信号或nginx -s reload
内存不会清除。
只能存放字符串类型,可借助cjson
完成json
格式存储。
文档地址:https://github.com/openresty/lua-nginx-module#ngxshareddict
快速入门
代码示例
Nginx
配置
http {
lua_shared_dict memory 5m;
}
set
local result = {
a = "hello",
b = "world"
}
local memory = ngx.shared.memory
local success, err = memory:set("web_stat", json.encode(result))
if not success then
ngx.log(ngx.ERR, "set shm err#", err)
end
get
local memory = ngx.shared.memory
local result = memory:get("web_stat")
local json_obj = json.decode(result)
常用方法
capacity_bytes = ngx.shared.DICT:capacity()
获取内存总容量,单位字节。
free_page_bytes = ngx.shared.DICT:free_space()
获取内存了剩余空间,单位字节。
开源案例
阅读 5300 · 发布于 2020-03-05
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Arthas 监控安装成 Windows 服务的 Tomcat阅读 2062
-
数据结构:环形链表-约瑟夫环阅读 1108
-
macOS 更改默认打开方式阅读 91
-
使用 base64 命令绕开服务器无权限上传文件问题阅读 1808
-
Spring Boot Tomcat 启动流程阅读 1566
-
Arthas 使用 trace 查找耗时操作和调用链路阅读 7573
-
算法每日一题20190619:两数之和阅读 2022
-
Spring Boot 整合 JWT JSON Web Token阅读 512
-
Elasticsearch 获取记录总数 _count阅读 10364
-
MySQL 性能优化之 explain type 字段阅读 1347