aboutsummaryrefslogtreecommitdiffstats
path: root/openresty-ext/src/assembly/resources/openresty/nginx/luaext/dao/db_access.lua
blob: 35572cca635c30ab9ae9ceaad748d077b1af94d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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