3 Copyright (C) 2018 ZTE, Inc. and others. All rights reserved. (ZTE)
\r
5 Licensed under the Apache License, Version 2.0 (the "License");
\r
6 you may not use this file except in compliance with the License.
\r
7 You may obtain a copy of the License at
\r
9 http://www.apache.org/licenses/LICENSE-2.0
\r
11 Unless required by applicable law or agreed to in writing, software
\r
12 distributed under the License is distributed on an "AS IS" BASIS,
\r
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
14 See the License for the specific language governing permissions and
\r
15 limitations under the License.
\r
19 --This module integrates the shcache with an abstraction for various databases(redis and so on)
\r
21 _M._VERSION = '1.0.0'
\r
23 local shcache = require("vendor.shcache")
\r
24 local cjson_safe = require "cjson.safe"
\r
25 local msbConf = require('conf.msbinit')
\r
27 local DB = require('dao.redis_db')
\r
28 local options = msbConf.redisConf
\r
30 local cacheConf = msbConf.cacheConf
\r
31 local positive_ttl = cacheConf.positive_ttl or 60
\r
32 local negative_ttl = cacheConf.negative_ttl or 2
\r
33 local actualize_ttl = cacheConf.actualize_ttl or 120
\r
34 local stats_prefix = "monitor:stats:"
\r
36 local svc_shcache = ngx.shared.svc_cache
\r
38 local function load_serviceinfo(key)
\r
40 -- closure to perform external lookup to redis
\r
41 local fetch_svcinfo_from_db = function ()
\r
42 local _db = DB:new(options)
\r
43 return _db:getserviceinfo(key)
\r
46 local svcinfo_cache_table = shcache:new(
\r
48 { external_lookup = fetch_svcinfo_from_db,
\r
49 --encode = cmsgpack.pack,
\r
50 --encode = cjson_safe.encode,
\r
51 --decode = cmsgpack.unpack
\r
52 --decode = cjson_safe.decode
\r
54 { positive_ttl = positive_ttl, -- default cache good data for 60s
\r
55 negative_ttl = negative_ttl, -- default cache failed lookup for 5s
\r
56 actualize_ttl = actualize_ttl,
\r
57 name = 'svcinfo_cache' -- "named" cache, useful for debug / report
\r
61 local serviceinfo, from_cache = svcinfo_cache_table:load(key)
\r
65 _M.load_serviceinfo = load_serviceinfo
\r
67 local function load_backservers(keypattern)
\r
69 -- closure to perform external lookup to redis
\r
70 local fetch_servers_from_db = function ()
\r
71 local _db = DB:new(options)
\r
72 return _db:getbackservers(keypattern)
\r
75 local servers_cache_table = shcache:new(
\r
77 { external_lookup = fetch_servers_from_db,
\r
78 --encode = cmsgpack.pack,
\r
79 encode = cjson_safe.encode,
\r
80 --decode = cmsgpack.unpack
\r
81 decode = cjson_safe.decode
\r
83 { positive_ttl = positive_ttl, -- default cache good data for 60s
\r
84 negative_ttl = negative_ttl, -- default cache failed lookup for 5s
\r
85 name = 'servers_cache' -- "named" cache, useful for debug / report
\r
89 local servers_table, from_cache = servers_cache_table:load(keypattern)
\r
91 return servers_table
\r
93 _M.load_backservers = load_backservers
\r
96 local function load_customsvcnames(keypattern)
\r
97 -- closure to perform external lookup to redis
\r
98 local fetch_svcnames_from_db = function ()
\r
99 local _db = DB:new(options)
\r
100 return _db:getcustomsvcnames(keypattern)
\r
103 local svcnames_cache_table = shcache:new(
\r
105 { external_lookup = fetch_svcnames_from_db,
\r
106 --encode = cmsgpack.pack,
\r
107 encode = cjson_safe.encode,
\r
108 --decode = cmsgpack.unpack
\r
109 decode = cjson_safe.decode
\r
111 { positive_ttl = positive_ttl, -- default cache good data for 60s
\r
112 negative_ttl = negative_ttl, -- default cache failed lookup for 5s
\r
113 actualize_ttl = actualize_ttl,
\r
114 name = 'svcnames_cache' -- "named" cache, useful for debug / report
\r
118 local service_names, from_cache = svcnames_cache_table:load(keypattern)
\r
120 return service_names
\r
122 _M.load_customsvcnames = load_customsvcnames
\r
125 function _M.save_reqnum_stats(date,info)
\r
126 local _db = DB:new(options)
\r
127 local c, err = _db:connectdb()
\r
132 local red = _db.redis
\r
133 local resp,err = red:rpush(stats_prefix..date,info)
\r
136 return nil, "save_reqnum_stats failed! key:"..date.." value:"..info
\r
142 function _M.delete_reqnum_stats(date)
\r
143 local _db = DB:new(options)
\r
144 local c, err = _db:connectdb()
\r
148 local red = _db.redis
\r
149 red:del(stats_prefix..date)
\r
153 function _M.get_reqnum_stats(date,latest_num)
\r
154 if(type(latest_num)~="number") then
\r
155 return nil,"the input num is illegal!"
\r
157 local _db = DB:new(options)
\r
158 local c, err = _db:connectdb()
\r
162 local red = _db.redis
\r
163 local resp,err = red:lrange(stats_prefix..date,0-latest_num,-1)
\r