--- /dev/null
+--[[\r
+\r
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)\r
+\r
+ Licensed under the Apache License, Version 2.0 (the "License");\r
+ you may not use this file except in compliance with the License.\r
+ You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+--]]\r
+\r
+--This module integrates the shcache with an abstraction for various databases(redis and so on)\r
+local _M = {}\r
+_M._VERSION = '1.0.0'\r
+\r
+local shcache = require("vendor.shcache")\r
+local cjson = require "cjson"\r
+local msbConf = require('conf.msbinit')\r
+\r
+local DB = require('dao.redis_db')\r
+local options = msbConf.redisConf\r
+\r
+local cacheConf = msbConf.cacheConf\r
+local positive_ttl = cacheConf.positive_ttl or 60\r
+local negative_ttl = cacheConf.negative_ttl or 2\r
+\r
+local svc_shcache = ngx.shared.svc_cache\r
+\r
+local function load_serviceinfo(key)\r
+\r
+ -- closure to perform external lookup to redis\r
+ local fetch_svcinfo_from_db = function ()\r
+ local _db = DB:new(options)\r
+ return _db:getserviceinfo(key)\r
+ end\r
+\r
+ local svcinfo_cache_table = shcache:new(\r
+ svc_shcache,\r
+ { external_lookup = fetch_svcinfo_from_db,\r
+ --encode = cmsgpack.pack,\r
+ encode = cjson.encode,\r
+ --decode = cmsgpack.unpack\r
+ decode = cjson.decode\r
+ },\r
+ { positive_ttl = positive_ttl, -- default cache good data for 60s \r
+ negative_ttl = negative_ttl, -- default cache failed lookup for 5s\r
+ name = 'svcinfo_cache' -- "named" cache, useful for debug / report\r
+ }\r
+ )\r
+\r
+ local serviceinfo, from_cache = svcinfo_cache_table:load(key)\r
+\r
+ return serviceinfo\r
+end\r
+_M.load_serviceinfo = load_serviceinfo\r
+\r
+local function load_backservers(keypattern)\r
+\r
+ -- closure to perform external lookup to redis\r
+ local fetch_servers_from_db = function ()\r
+ local _db = DB:new(options)\r
+ return _db:getbackservers(keypattern)\r
+ end\r
+\r
+ local servers_cache_table = shcache:new(\r
+ svc_shcache,\r
+ { external_lookup = fetch_servers_from_db,\r
+ --encode = cmsgpack.pack,\r
+ encode = cjson.encode,\r
+ --decode = cmsgpack.unpack\r
+ decode = cjson.decode\r
+ },\r
+ { positive_ttl = positive_ttl, -- default cache good data for 60s \r
+ negative_ttl = negative_ttl, -- default cache failed lookup for 5s\r
+ name = 'servers_cache' -- "named" cache, useful for debug / report\r
+ }\r
+ )\r
+\r
+ local servers_table, from_cache = servers_cache_table:load(keypattern)\r
+\r
+ return servers_table\r
+end\r
+_M.load_backservers = load_backservers\r
+\r
+\r
+local function load_customsvcnames(keypattern)\r
+ -- closure to perform external lookup to redis\r
+ local fetch_svcnames_from_db = function ()\r
+ local _db = DB:new(options)\r
+ return _db:getcustomsvcnames(keypattern)\r
+ end\r
+\r
+ local svcnames_cache_table = shcache:new(\r
+ svc_shcache,\r
+ { external_lookup = fetch_svcnames_from_db,\r
+ --encode = cmsgpack.pack,\r
+ encode = cjson.encode,\r
+ --decode = cmsgpack.unpack\r
+ decode = cjson.decode\r
+ },\r
+ { positive_ttl = positive_ttl, -- default cache good data for 60s \r
+ negative_ttl = negative_ttl, -- default cache failed lookup for 5s\r
+ name = 'svcnames_cache' -- "named" cache, useful for debug / report\r
+ }\r
+ )\r
+\r
+ local service_names, from_cache = svcnames_cache_table:load(keypattern)\r
+\r
+ return service_names\r
+end\r
+_M.load_customsvcnames = load_customsvcnames\r
+\r
+return _M
\ No newline at end of file