3 Copyright (C) 2016 ZTE, Inc. and others. All rights reserved. (ZTE)
\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
9 http://www.apache.org/licenses/LICENSE-2.0
\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
19 -- the client for redis, include the connection pool management and api implements
\r
20 local redis = require('resty.redis')
\r
21 local tbl_util = require('lib.utils.table_util')
\r
25 _M._VERSION = '0.0.1'
\r
26 _M._DESCRIPTION = 'msb_redis_module'
\r
28 local mt = { __index = _M }
\r
30 local tbl_insert = table.insert
\r
31 local tbl_sort = table.sort
\r
32 local tbl_isempty = tbl_util.isempty
\r
34 function _M.new(self, conf)
\r
35 self.host = conf.host
\r
36 self.port = conf.port
\r
37 self.timeout = conf.timeout
\r
38 self.dbid = conf.dbid
\r
39 self.poolsize = conf.poolsize
\r
40 self.idletimeout = conf.idletimeout
\r
42 local red = redis:new()
\r
43 return setmetatable({redis = red}, mt)
\r
46 function _M.connectdb(self)
\r
47 local host = self.host
\r
48 local port = self.port
\r
49 local dbid = self.dbid
\r
50 local red = self.redis
\r
52 if not (host and port) then
\r
53 return nil, 'no host:port avaliable provided'
\r
57 if not dbid then dbid = 0 end
\r
58 local timeout = self.timeout
\r
59 if not timeout then
\r
60 timeout = 1000 -- 1s
\r
63 red:set_timeout(timeout)
\r
66 if host and port then
\r
67 ok, err = red:connect(host, port)
\r
68 if ok then return red:select(dbid) end
\r
74 function _M.keepalivedb(self)
\r
75 local max_idle_timeout = self.idletimeout --ms
\r
76 local pool_size = self.poolsize
\r
78 if not pool_size then pool_size = 100 end
\r
79 if not max_idle_timeout then max_idle_timeout = 90000 end --90s
\r
81 local ok, err = self.redis:set_keepalive(max_idle_timeout, pool_size)
\r
83 ngx.log(ngx.ERR, "redis pool keepalive error",err)
\r
89 --inner function,only used in this module
\r
90 local function _hgetall(red,key)
\r
91 local resp,err = red:hgetall(key)
\r
92 --if not resp or next(resp) == nil then
\r
93 if tbl_isempty(resp) then
\r
94 return nil, "key "..key.." not found"
\r
96 local hashinfo = red:array_to_hash(resp)
\r
100 function _M.getserviceinfo(self,key)
\r
102 return nil,'no key is provided'
\r
104 local c, err = self:connectdb()
\r
109 local red = self.redis
\r
110 local resp,err = red:get(key) --the key will create dynamically
\r
112 if not resp or resp == ngx.null then
\r
113 return nil, "key "..key.." not found"
\r
119 function _M.getbackservers(self,keypattern)
\r
120 if not keypattern then
\r
121 return nil,'no keypattern is provided'
\r
123 local c, err = self:connectdb()
\r
128 local red = self.redis
\r
130 local resp, err = red:keys(keypattern)
\r
131 if tbl_isempty(resp) then
\r
133 return nil, "no server matched"
\r
137 for i, v in ipairs(resp) do
\r
138 local serverinfo,err = _hgetall(red,v)
\r
140 tbl_insert(servers,serverinfo)
\r
147 function _M.getcustomsvcnames(self,keypattern)
\r
148 if not keypattern then
\r
149 return nil,'no keypattern is provided'
\r
151 local c, err = self:connectdb()
\r
156 local red = self.redis
\r
157 --store svc names into the Set
\r
158 local svcname_set={}
\r
160 local res, err = red:scan("0","count",50,"match",keypattern)
\r
163 return {}, "failed to scan in getcustomsvcnames()"
\r
166 local cursor, keys, err = unpack(res)
\r
167 for _, svckey in ipairs(keys) do
\r
168 local m, err = ngx.re.match(svckey, "^.+:custom:([^:]+)", "o")
\r
170 svcname_set[m[1]]=true
\r
174 while( cursor ~= "0" )
\r
176 res = red:scan(cursor,"count",50,"match",keypattern)
\r
180 cursor, keys, err = unpack(res)
\r
181 for _, svckey in ipairs(keys) do
\r
182 local m, err = ngx.re.match(svckey, "^.+:custom:([^:]+)", "o")
\r
184 svcname_set[m[1]]=true
\r
189 local svcnames = {}
\r
190 for svcname,_ in pairs(svcname_set) do
\r
191 tbl_insert(svcnames,svcname)
\r
193 --sort the key_table in reverse order
\r
194 tbl_sort(svcnames, function (a, b)
\r
197 return svcnames,nil
\r