Divide the MSB source codes into two repos
[msb/apigateway.git] / openresty-ext / src / assembly / resources / openresty / nginx / luaext / dao / dao.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 --This module integrates the shcache with an abstraction for various databases(redis and so on)\r
20 local _M = {}\r
21 _M._VERSION = '1.0.0'\r
22 \r
23 local shcache = require("vendor.shcache")\r
24 local cjson_safe =  require "cjson.safe"\r
25 local msbConf = require('conf.msbinit')\r
26 \r
27 local DB      = require('dao.redis_db')\r
28 local options = msbConf.redisConf\r
29 \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 \r
35 local svc_shcache = ngx.shared.svc_cache\r
36 \r
37 local function load_serviceinfo(key)\r
38 \r
39         -- closure to perform external lookup to redis\r
40         local fetch_svcinfo_from_db = function ()\r
41                 local _db = DB:new(options)\r
42                 return _db:getserviceinfo(key)\r
43         end\r
44 \r
45         local svcinfo_cache_table = shcache:new(\r
46                 svc_shcache,\r
47                 { external_lookup = fetch_svcinfo_from_db,\r
48                         --encode = cmsgpack.pack,\r
49                         --encode = cjson_safe.encode,\r
50                         --decode = cmsgpack.unpack\r
51                         --decode = cjson_safe.decode\r
52                 },\r
53                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
54                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
55                         actualize_ttl = actualize_ttl,\r
56                         name = 'svcinfo_cache'       -- "named" cache, useful for debug / report\r
57                 }\r
58         )\r
59 \r
60         local serviceinfo, from_cache = svcinfo_cache_table:load(key)\r
61 \r
62         return serviceinfo\r
63 end\r
64 _M.load_serviceinfo = load_serviceinfo\r
65 \r
66 local function load_backservers(keypattern)\r
67 \r
68         -- closure to perform external lookup to redis\r
69         local fetch_servers_from_db = function ()\r
70                 local _db = DB:new(options)\r
71                 return _db:getbackservers(keypattern)\r
72         end\r
73 \r
74         local servers_cache_table = shcache:new(\r
75                 svc_shcache,\r
76                 { external_lookup = fetch_servers_from_db,\r
77                         --encode = cmsgpack.pack,\r
78                         encode = cjson_safe.encode,\r
79                         --decode = cmsgpack.unpack\r
80                         decode = cjson_safe.decode\r
81                 },\r
82                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
83                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
84                         name = 'servers_cache'           -- "named" cache, useful for debug / report\r
85                 }\r
86         )\r
87 \r
88         local servers_table, from_cache = servers_cache_table:load(keypattern)\r
89 \r
90         return servers_table\r
91 end\r
92 _M.load_backservers = load_backservers\r
93 \r
94 \r
95 local function load_customsvcnames(keypattern)\r
96         -- closure to perform external lookup to redis\r
97         local fetch_svcnames_from_db = function ()\r
98                 local _db = DB:new(options)\r
99                 return _db:getcustomsvcnames(keypattern)\r
100         end\r
101 \r
102         local svcnames_cache_table = shcache:new(\r
103                 svc_shcache,\r
104                 { external_lookup = fetch_svcnames_from_db,\r
105                         --encode = cmsgpack.pack,\r
106                         encode = cjson_safe.encode,\r
107                         --decode = cmsgpack.unpack\r
108                         decode = cjson_safe.decode\r
109                 },\r
110                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
111                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
112                         actualize_ttl = actualize_ttl,\r
113                         name = 'svcnames_cache'      -- "named" cache, useful for debug / report\r
114                 }\r
115         )\r
116 \r
117         local service_names, from_cache = svcnames_cache_table:load(keypattern)\r
118 \r
119         return service_names\r
120 end\r
121 _M.load_customsvcnames = load_customsvcnames\r
122 \r
123 return _M