Commit bda0f7dc authored by 周尚's avatar 周尚

use lua_shared_dict for cache instead

parent 05aa372f
...@@ -31,14 +31,14 @@ http { ...@@ -31,14 +31,14 @@ http {
#keepalive_timeout 0; #keepalive_timeout 0;
keepalive_timeout 65; keepalive_timeout 65;
lua_code_cache on; lua_code_cache on;
lua_package_path "/Users/zhoush/openresty/nginx/lua/?.lua;;"; lua_package_path "/Users/zhoush/openresty/nginx/lua/?.lua;;";
init_by_lua_file "lua/luainit/init.lua"; init_by_lua_file "lua/luainit/init.lua";
init_worker_by_lua_file "lua/luabiz/settle.lua"; init_worker_by_lua_file "lua/luabiz/settle.lua";
# shared_dict 缓存
lua_shared_dict dns_cache 128m;
#gzip on; #gzip on;
server { server {
...@@ -98,10 +98,14 @@ http { ...@@ -98,10 +98,14 @@ http {
} }
} }
location /csv {
content_by_lua_file "lua/luabiz/doftp.lua";
}
location /get_addr { location /get_addr {
content_by_lua_block { content_by_lua_block {
local zx_base = _G.zx_base or loadmod("luabiz.zx_base") local zx_base = _G.zx_base or loadmod("luabiz.zx_base")
local host = zx_base:_get_addr("www.baidu.com") local host = zx_base:_get_addr("baidu.com")
ngx.say(host) ngx.say(host)
} }
} }
......
local json = require('cjson')
local function GetLines(filename)
local file = io.input(filename)
local res = {}
for line in file:lines() do
table.insert(res, line)
end
return res --returns number of lines and line table
end
local function getValues(str)
local idx = 0
local res = {}
if str ~= nil then
while string.find(str, ",") ~= nil do
local i, j =string.find(str, ",");
idx =idx + 1;
res[idx] = string.sub(str, 1, j-1);
str = string.sub(str, j+1, string.len(str))
end
idx = idx + 1
res[idx] = str
end
return idx, res
end
local function doLines(lines)
local vals = {}
for i = 1, #lines do
local _, val = getValues(lines[i])
table.insert(vals, val)
end
return vals
end
local function parseBasis(vals)
local res = {}
for i=1, #vals do
local line = {
user_sn =vals[i][1],
user_name = vals[i][2],
last_name = vals[i][3],
first_name = vals[i][4],
cmbc_hire_date = vals[i][5],
name_pinin = vals[i][6],
sex = vals[i][7],
cmbc_oa_name = vals[i][8],
birth_date = vals[i][9],
folk = vals[i][10],
user_email = vals[i][11],
birth_country_code = vals[i][12],
mobile = vals[i][13],
telephone_number = vals[i][14],
address = vals[i][15],
city = vals[i][16],
state = vals[i][17],
address_postal = vals[i][18],
dept_id = vals[i][19],
id_info = vals[i][20],
cmbc_user_status = vals[i][21],
manageType = vals[i][22],
user_type_id = vals[i][23],
psn_jobinfo_order = vals[i][24],
job_order = vals[i][25]
}
res[i] = line
end
return res
end
local function parseHR_Org(vals)
local res = {}
for i=1, #vals do
local line = {
setid = vals[i][1],
deptid = vals[i][2],
org_name = vals[i][3],
org_short_name = vals[i][4],
effdt = vals[i][5],
eff_status = vals[i][6],
cmbc_dept_type = vals[i][7],
parent_node_num = vals[i][8],
parent_set_num = vals[i][9],
company = vals[i][10],
tree_level_num = vals[i][11],
manageType = vals[i][12],
org_order = vals[i][13]
}
res[i] = line
end
return res
end
local function parseHR_job(vals)
local res = {}
for i=1, #vals do
local line = {
hr_position_code = vals[i][1],
hr_position_name = vals[i][2],
hr_position_short_name = vals[i][3],
hr_position_job_code = vals[i][4],
hr_position_set_id = vals[i][5],
hr_position_dept_id = vals[i][6],
hr_position_status = vals[i][7],
hr_position_eff_date = vals[i][8],
hr_position_eff_status = vals[i][9],
manageType = vals[i][10]
}
res[i] = line
end
return res
end
local function doftp()
local lines = GetLines("/tmp/test.csv")
local vals = doLines(lines)
ngx.say(json.encode(vals))
end
doftp()
...@@ -487,13 +487,13 @@ local function sdt_sql(tk_biz, tk_control, total_time) ...@@ -487,13 +487,13 @@ local function sdt_sql(tk_biz, tk_control, total_time)
shop['t_order_novip_amt'] = shop['t_order_amt'] - shop['t_order_vip_amt'] shop['t_order_novip_amt'] = shop['t_order_amt'] - shop['t_order_vip_amt']
shop['t_order_novip_cnt'] = shop['t_order_cnt'] - shop['t_order_vip_cnt'] shop['t_order_novip_cnt'] = shop['t_order_cnt'] - shop['t_order_vip_cnt']
shop['sku_turn_t'] = 0 shop['sku_turn_t'] = 0
shop['benefit_amt'] = 0 shop['benefit_amt'] = 0
shop['rank_benefit'] = 0 shop['rank_benefit'] = 0
shop['rank_sale_cnt'] = 0 shop['rank_sale_cnt'] = 0
shop['rank_sale_amt'] = 0 shop['rank_sale_amt'] = 0
shop['rank_sale_profit'] = 0 shop['rank_sale_profit'] = 0
shop['rank_sale_profit_per'] = 0 shop['rank_sale_profit_per'] = 0
......
...@@ -80,12 +80,11 @@ function ZX_BASE.date2time(self, date) ...@@ -80,12 +80,11 @@ function ZX_BASE.date2time(self, date)
end end
-- DNS Resolver
local require = require local require = require
local ngx_re_find = ngx.re.find local ngx_re_find = ngx.re.find
local lrucache = require "resty.lrucache"
local resolver = require "resty.dns.resolver" local resolver = require "resty.dns.resolver"
local cache_storage = lrucache.new(200) local dns_cache = ngx.shared.dns_cache
local function _is_addr(hostname) local function _is_addr(hostname)
return ngx_re_find(hostname, [[\d+?\.\d+?\.\d+?\.\d+$]], "jo") return ngx_re_find(hostname, [[\d+?\.\d+?\.\d+?\.\d+$]], "jo")
...@@ -98,8 +97,7 @@ function ZX_BASE._get_addr(self,hostname) ...@@ -98,8 +97,7 @@ function ZX_BASE._get_addr(self,hostname)
return hostname, hostname return hostname, hostname
end end
local addr = cache_storage:get(hostname) local addr = dns_cache:get(hostname)
if addr then if addr then
return addr, hostname return addr, hostname
end end
...@@ -115,14 +113,13 @@ function ZX_BASE._get_addr(self,hostname) ...@@ -115,14 +113,13 @@ function ZX_BASE._get_addr(self,hostname)
end end
local answers, err = r:query(hostname, {qtype = r.TYPE_A}) local answers, err = r:query(hostname, {qtype = r.TYPE_A})
if not answers or answers.errcode then if not answers or answers.errcode then
return nil, hostname return nil, hostname
end end
for i, ans in ipairs(answers) do for i, ans in ipairs(answers) do
if ans.address then if ans.address then
cache_storage:set(hostname, ans.address, 300) dns_cache:set(hostname, ans.address, 300)
return ans.address, hostname return ans.address, hostname
end end
end end
......
...@@ -29,6 +29,14 @@ if hostname == "zzd-mer01" then -- 生产 ...@@ -29,6 +29,14 @@ if hostname == "zzd-mer01" then -- 生产
database = "tk_biz", database = "tk_biz",
timeout = 30 timeout = 30
} }
},
cmbc_card = {
host = "115.29.241.68",
user = "root",
password = "xingdata",
port = 3306,
database = "cmbc_card",
timeout = 30
} }
} }
else else
......
--- 定义NULL常量 --- 定义NULL常量
_G.NULL = ngx.null _G.NULL = ngx.null
-- Get DNS Servers
local pcall = pcall local pcall = pcall
local io_open = io.open local io_open = io.open
local ngx_re_gmatch = ngx.re.gmatch local ngx_re_gmatch = ngx.re.gmatch
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment