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()
获取内存了剩余空间,单位字节。
开源案例
阅读 4963 · 发布于 2020-03-05
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
软考-系统架构设计师:数据库安全阅读 1322
-
Elasticsearch 检索文档 GET阅读 1509
-
Android 获取应用信息阅读 2820
-
SQL 查询出成绩表中成绩大于 90 的学生名字阅读 2194
-
Linux 之 -bash pecl command not found 的解决方法阅读 3768
-
OpenResty 使用 lua-resty-auto-ssl 配置 https 证书阅读 5060
-
Spring Boot 自动配置原理阅读 1156
-
wget ERROR The certificate was signed using an insecure algorithm阅读 2092
-
GitHub OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443阅读 4960
-
Python 内置函数阅读 1176