OpenResty 中使用 ngx.share.DICT 完成内存存储
OpenResty Lua About 736 words方法介绍
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)
endget
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()
获取内存了剩余空间,单位字节。
开源案例
                Views: 7,580 · Posted: 2020-03-05
            
            ————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
 
        Loading...