Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / dao / db_access.lua
1 --[[\r
2 \r
3     Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
4 \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
8 \r
9             http://www.apache.org/licenses/LICENSE-2.0\r
10 \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
16 \r
17 --]]\r
18 \r
19 -- unified layer to access back DB, using two levels of cache mechanism(LRUCache and shcache)\r
20 local _M = {}\r
21 _M._VERSION = '1.0.0'\r
22 \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
29 \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
35                         return nil\r
36                 end\r
37                 return cached_value\r
38         end\r
39         -- Get from shcache or backend redis\r
40         local svcinfo_str = dao.load_serviceinfo(key)\r
41         local value,err\r
42         if svcinfo_str then\r
43                 value, err = cjson_safe.decode(svcinfo_str)\r
44                 if err then\r
45                         value = nil\r
46                         ngx.log(ngx.WARN, "decode service info error: ", err, " service info: ", svcinfo_str)\r
47                 end\r
48         else\r
49                 value = nil\r
50         end\r
51         cache.set(key, value, lru_ttl)\r
52         return value\r
53 end\r
54 \r
55 function _M.load_backservers(keypattern)\r
56         local value, err\r
57         -- Try to get from cache\r
58         value = cache.get(keypattern)\r
59         if not value then\r
60                 -- Get from shcache or backend redis\r
61                 value = dao.load_backservers(keypattern)\r
62                 cache.set(keypattern, value, lru_ttl)\r
63         end\r
64         if cache.is_empty(value) then\r
65                 return nil\r
66         end\r
67         return value\r
68 end\r
69 \r
70 function _M.load_customsvcnames(keypattern)\r
71         local value, err\r
72         -- Try to get from cache\r
73         value = cache.get(keypattern)\r
74         if not value then\r
75                 -- Get from shcache or backend redis\r
76                 value = dao.load_customsvcnames(keypattern)\r
77                 cache.set(keypattern, value, lru_ttl)\r
78         end\r
79         if cache.is_empty(value) then\r
80                 return nil\r
81         end\r
82         return value\r
83 end\r
84 \r
85 return _M