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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
| local skynet = require "skynet" local contriner_client = require "contriner_client" local redis = require "skynet.db.redis" local string_util = require "string_util" local sha2 = require "sha2" local log = require "log"
local setmetatable = setmetatable local assert = assert local pcall = pcall local ipairs = ipairs local pairs = pairs local type = type local string = string local select = select local tunpack = table.unpack local debug_getinfo = debug.getinfo
local g_sha_map = {}
local M = {} local command = {}
local cmdfuncs = {}
function command:script_run(script_str,...) local conn = self.conn assert(conn,"not connect redis ") assert(type(script_str) == 'string','script_str not string')
local sha = g_sha_map[script_str] if not sha then sha = sha2.sha1(script_str) g_sha_map[script_str] = sha end
local isok,ret = pcall(conn.evalsha,conn,...) if not isok then if string.find(ret,"NOSCRIPT",nil,true) then ret = conn:eval(script_str,...) end end return ret end
local function get_line_info() local info = debug_getinfo(3,"Sl") local lineinfo = info.short_src .. ":" .. info.currentline end
local mt = { __index = function(t,k) local f = cmdfuncs[k] if f then t[k] = f return f end
local f = function (self,...) if not self.conn then local ok,conn = pcall(redis.connect,self.conf) if not ok then log.error("connect redis err ",get_line_info(),conn,k,self.conf) return else self.conn = conn end end
local cmd = command[k] if cmd then local ret = {pcall(cmd,self,...)} local isok = ret[1] local err = ret[2] if not isok then log.error("call redis command faild ",get_line_info(),err,k,...) return else return select(2,tunpack(ret)) end else local isok,ret = pcall(self.conn[k],self.conn,...) if not isok then log.error("call redis faild ",get_line_info(),ret,k,...) return end return ret end end
t[k] = f cmdfuncs[k] = f return f end}
function M.new_client(db_name) local cli = contriner_client:new('share_config_m') local conf_map = cli:mod_call('query','redis') assert(conf_map and conf_map[db_name],"not redis conf")
local conf = conf_map[db_name] local t_conn = { conf = conf, conn = false } setmetatable(t_conn,mt) t_conn:get("ping") return t_conn end
function M.add_command(M) for k,func in pairs(M) do assert(not command[k],"command is exists " .. k) command[k] = func end end
function M.new_watch(db_name,subscribe_list,psubscribe_list,call_back) local cli = contriner_client:new('share_config_m') local conf_map = cli:mod_call('query','redis') assert(conf_map and conf_map[db_name],"not redis conf") local conf = conf_map[db_name]
local is_cancel = false local ok,watch
skynet.fork(function() while not watch and not is_cancel do ok,watch = pcall(redis.watch,conf) if not ok then log.error("redisf connect watch err ",conf) end skynet.sleep(100) end for _,key in ipairs(subscribe_list) do if not is_cancel then watch:subscribe(key) end end
for _,key in ipairs(psubscribe_list) do if not is_cancel then watch:psubscribe(key) end end
while not is_cancel do local ok,msg,key,psubkey = pcall(watch.message,watch) if ok then call_back(msg,key,psubkey) else if not is_cancel then log.error("watch.message err :",msg,key,psubkey) end end end end)
return function() is_cancel = true if watch then for _,key in ipairs(subscribe_list) do watch:unsubscribe(key) end for _,key in ipairs(psubscribe_list) do watch:punsubscribe(key) end watch:disconnect() watch = nil end return true end end
return M
|