Nginx + Lua + 共享内存实现动态查询
[*]lua_package_path "/usr/local/share/luajit-2.0.2/jit?.lua;;";
[*]lua_shared_dict devicedb 45m;
[*] location /query {
[*] default_type 'text/plain';
[*] content_by_lua '
[*] local args = ngx.req.get_uri_args()
[*] local devicetype = args["device"]
[*] local devicedb = ngx.shared.devicedb
[*] local res = devicedb:get(devicetype)
[*]
[*] ngx.say(res)
[*] ';
[*] }
[*]
[*] location /update {
[*] default_type 'text/plain';
[*] content_by_lua '
[*] local devicedb = ngx.shared.devicedb
[*]
[*] for item in io.lines("/usr/local/nginx-1.4.2/data/rule.txt") do
[*] _,_,device_type, device_rule = string.find(item, "^(%a+)--(%a+)$")
[*] devicedb:set(device_type,device_rule)
[*] end
[*]
[*] ngx.say("ok")
[*] ';
[*] }
lua_package_path "/usr/local/share/luajit-2.0.2/jit?.lua;;";
lua_shared_dict devicedb 45m;
location /query {
default_type 'text/plain';
content_by_lua '
local args = ngx.req.get_uri_args()
local devicetype = args["device"]
local devicedb = ngx.shared.devicedb
local res = devicedb:get(devicetype)
ngx.say(res)
';
}
location /update {
default_type 'text/plain';
content_by_lua '
local devicedb = ngx.shared.devicedb
for item in io.lines("/usr/local/nginx-1.4.2/data/rule.txt") do
_,_,device_type, device_rule = string.find(item, "^(%a+)--(%a+)$")
devicedb:set(device_type,device_rule)
end
ngx.say("ok")
';
}
rule.txt文件格式。
view plaincopyprint?http://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
[*]SAMSUNG--samRule
[*]APPLE--appRule
[*]XIAOMI--xiaRule
SAMSUNG--samRule
APPLE--appRule
XIAOMI--xiaRule
步骤1,访问/update,更新共享内存devicedb
步骤2,访问query?device=XIAOMI,返回xiaRule
步骤3,修改rule.txt,将xiaRule改为xiaRuleaaaa
步骤4,访问/update,更新共享内存devicedb
步骤5,访问query?device=XIAOMI,返回xiaRuleaaaa
内网响应时间在5~10ms。
参考文章:
http://my.oschina.net/766/blog/158972
http://haili.me/archives/722.html
转自http://blog.csdn.net/lxb_champagne/article/details/17099383
页:
[1]