1. Adjust the directory hierarchy
[msb/apigateway.git] / msb-core / 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   = require "cjson"\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 \r
34 local svc_shcache = ngx.shared.svc_cache\r
35 \r
36 local function load_serviceinfo(key)\r
37 \r
38         -- closure to perform external lookup to redis\r
39         local fetch_svcinfo_from_db = function ()\r
40                 local _db = DB:new(options)\r
41                 return _db:getserviceinfo(key)\r
42         end\r
43 \r
44         local svcinfo_cache_table = shcache:new(\r
45                 svc_shcache,\r
46                 { external_lookup = fetch_svcinfo_from_db,\r
47                         --encode = cmsgpack.pack,\r
48                         encode = cjson.encode,\r
49                         --decode = cmsgpack.unpack\r
50                         decode = cjson.decode\r
51                 },\r
52                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
53                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
54                         name = 'svcinfo_cache'       -- "named" cache, useful for debug / report\r
55                 }\r
56         )\r
57 \r
58         local serviceinfo, from_cache = svcinfo_cache_table:load(key)\r
59 \r
60         return serviceinfo\r
61 end\r
62 _M.load_serviceinfo = load_serviceinfo\r
63 \r
64 local function load_backservers(keypattern)\r
65 \r
66         -- closure to perform external lookup to redis\r
67         local fetch_servers_from_db = function ()\r
68                 local _db = DB:new(options)\r
69                 return _db:getbackservers(keypattern)\r
70         end\r
71 \r
72         local servers_cache_table = shcache:new(\r
73                 svc_shcache,\r
74                 { external_lookup = fetch_servers_from_db,\r
75                         --encode = cmsgpack.pack,\r
76                         encode = cjson.encode,\r
77                         --decode = cmsgpack.unpack\r
78                         decode = cjson.decode\r
79                 },\r
80                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
81                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
82                         name = 'servers_cache'           -- "named" cache, useful for debug / report\r
83                 }\r
84         )\r
85 \r
86         local servers_table, from_cache = servers_cache_table:load(keypattern)\r
87 \r
88         return servers_table\r
89 end\r
90 _M.load_backservers = load_backservers\r
91 \r
92 \r
93 local function load_customsvcnames(keypattern)\r
94         -- closure to perform external lookup to redis\r
95         local fetch_svcnames_from_db = function ()\r
96                 local _db = DB:new(options)\r
97                 return _db:getcustomsvcnames(keypattern)\r
98         end\r
99 \r
100         local svcnames_cache_table = shcache:new(\r
101                 svc_shcache,\r
102                 { external_lookup = fetch_svcnames_from_db,\r
103                         --encode = cmsgpack.pack,\r
104                         encode = cjson.encode,\r
105                         --decode = cmsgpack.unpack\r
106                         decode = cjson.decode\r
107                 },\r
108                 {   positive_ttl = positive_ttl,           -- default cache good data for 60s \r
109                         negative_ttl = negative_ttl,           -- default cache failed lookup for 5s\r
110                         name = 'svcnames_cache'      -- "named" cache, useful for debug / report\r
111                 }\r
112         )\r
113 \r
114         local service_names, from_cache = svcnames_cache_table:load(keypattern)\r
115 \r
116         return service_names\r
117 end\r
118 _M.load_customsvcnames = load_customsvcnames\r
119 \r
120 return _M