Nginx加载Lua脚本lua_shared_dict缓存

1、介绍

lua_shared_dict缓存是nginx为lua提供的一个多进程共享空间,为了避免多进程修改造成脏数据,lua_shared_dict修改数据是用锁来实现的。这样就会有qps访问瓶颈变小的问题。这是性能缺点。

2、使用

1)首先在nginx.conf里申请一块共享内存空间

http {
...

# 不缓存lua代码,修改lua及时生效,但是每次请求会加载lua代码,牺牲了性能,

# 所以测试阶段开始关掉。默认是开启的。

lua_code_cache off;

# shared_data_d是空间变量名,可以自定义;1m:是空间大小

lua_shared_dict shared_data_d 1m;

server {
location /lua {
            default_type   text/html;
            content_by_lua_file lua/main.lua;
        }

}

}

2)测试代码

local shared_data = ngx.shared.shared_data_d
local i = shared_data:get("count")
if not i then
    i= 1
    shared_data:set("count", i)
    ngx.say("set count ", i, "<br/>")
end
i = shared_data:incr("count", 1)
ngx.say("count=", i , " <br/>")

 代码大意:在shared_data_d内存空间获取count,如果没获取到就设置为1,如果有就加一。

亲测无误!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值