3 Copyright (C) 2016 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 -- unified layer to access back DB, using two levels of cache mechanism(LRUCache and shcache)
\r
21 _M._VERSION = '1.0.0'
\r
23 local cache = require('lib.tools.db_cache')
\r
24 local dao = require('dao.dao')
\r
25 local msbConf = require('conf.msbinit')
\r
26 local cjson_safe = require "cjson.safe"
\r
27 local cacheConf = msbConf.cacheConf
\r
28 local lru_ttl = cacheConf.lru_ttl or 10
\r
30 function _M.load_serviceinfo(key)
\r
31 -- Try to get from cache
\r
32 local cached_value = cache.get(key)
\r
33 if cached_value then
\r
34 if cache.is_empty(cached_value) then
\r
39 -- Get from shcache or backend redis
\r
40 local svcinfo_str = dao.load_serviceinfo(key)
\r
43 value, err = cjson_safe.decode(svcinfo_str)
\r
46 ngx.log(ngx.WARN, "decode service info error: ", err, " service info: ", svcinfo_str)
\r
51 cache.set(key, value, lru_ttl)
\r
55 function _M.load_backservers(keypattern)
\r
57 -- Try to get from cache
\r
58 value = cache.get(keypattern)
\r
60 -- Get from shcache or backend redis
\r
61 value = dao.load_backservers(keypattern)
\r
62 cache.set(keypattern, value, lru_ttl)
\r
64 if cache.is_empty(value) then
\r
70 function _M.load_customsvcnames(keypattern)
\r
72 -- Try to get from cache
\r
73 value = cache.get(keypattern)
\r
75 -- Get from shcache or backend redis
\r
76 value = dao.load_customsvcnames(keypattern)
\r
77 cache.set(keypattern, value, lru_ttl)
\r
79 if cache.is_empty(value) then
\r