diff options
author | 2017-07-25 15:18:33 +0800 | |
---|---|---|
committer | 2017-07-25 18:11:59 +0800 | |
commit | 672f3d40be83d9e380fd7be4b674d5e8d5fa36de (patch) | |
tree | 43105e1d5e2ba8e8accea8648e57e1cf87db3f00 /openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua | |
parent | 41d3db15a8e1a0496f9c2a5e15db2998a32bb9bf (diff) |
Divide the MSB source codes into two repos
Change-Id: Ie76d545b214a8ce5191f215350a623e1529983d9
Issue-id: MSB-5
Signed-off-by: HuabingZhao <zhao.huabing@zte.com.cn>
Diffstat (limited to 'openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua')
-rw-r--r-- | openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua | 85 |
1 files changed, 85 insertions, 0 deletions
diff --git a/openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua new file mode 100644 index 0000000..35572cc --- /dev/null +++ b/openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua @@ -0,0 +1,85 @@ +--[[
+
+ Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+--]]
+
+-- unified layer to access back DB, using two levels of cache mechanism(LRUCache and shcache)
+local _M = {}
+_M._VERSION = '1.0.0'
+
+local cache = require('lib.tools.db_cache')
+local dao = require('dao.dao')
+local msbConf = require('conf.msbinit')
+local cjson_safe = require "cjson.safe"
+local cacheConf = msbConf.cacheConf
+local lru_ttl = cacheConf.lru_ttl or 10
+
+function _M.load_serviceinfo(key)
+ -- Try to get from cache
+ local cached_value = cache.get(key)
+ if cached_value then
+ if cache.is_empty(cached_value) then
+ return nil
+ end
+ return cached_value
+ end
+ -- Get from shcache or backend redis
+ local svcinfo_str = dao.load_serviceinfo(key)
+ local value,err
+ if svcinfo_str then
+ value, err = cjson_safe.decode(svcinfo_str)
+ if err then
+ value = nil
+ ngx.log(ngx.WARN, "decode service info error: ", err, " service info: ", svcinfo_str)
+ end
+ else
+ value = nil
+ end
+ cache.set(key, value, lru_ttl)
+ return value
+end
+
+function _M.load_backservers(keypattern)
+ local value, err
+ -- Try to get from cache
+ value = cache.get(keypattern)
+ if not value then
+ -- Get from shcache or backend redis
+ value = dao.load_backservers(keypattern)
+ cache.set(keypattern, value, lru_ttl)
+ end
+ if cache.is_empty(value) then
+ return nil
+ end
+ return value
+end
+
+function _M.load_customsvcnames(keypattern)
+ local value, err
+ -- Try to get from cache
+ value = cache.get(keypattern)
+ if not value then
+ -- Get from shcache or backend redis
+ value = dao.load_customsvcnames(keypattern)
+ cache.set(keypattern, value, lru_ttl)
+ end
+ if cache.is_empty(value) then
+ return nil
+ end
+ return value
+end
+
+return _M
\ No newline at end of file |