Blame view

src/shared/redisproxy.lua 1.94 KB
314bc5df   zhengshouren   提交服务器初始代码
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
  local skynet = require "skynet"
  local harbor = require "skynet.harbor"
  
  local table_insert = table.insert
  
  local redisproxy = {}
  
  setmetatable(redisproxy, { __index = function(t, k)
  	local cmd = string.upper(k)
  	local f = function (self, ...)
  		local ok, result = pcall(skynet.call, redisd, "lua", cmd, ...)
  		if not ok then
  			skynet.error(cmd, ..., "\n", debug.traceback(coroutine.running(), nil))
  			return
  		end
  		return result
  	end
  	t[k] = f
  	return f
  end})
  
  function redisproxy:runScripts(name, ...)
  	local RedisScripts = require("rdsscripts/RedisScripts")
  
  	if not RedisScripts[name].sha1 then
  		local content = io.readfile(RedisScripts[name].file)
  		RedisScripts[name].sha1 = self:script("LOAD", content)
  	end
  
  	-- 不存在脚本(系统问题或者需要刷新脚本)
  	local existScript = self:script("EXISTS", RedisScripts[name].sha1)
  	if existScript[1] == 0 then
  		local content = io.readfile(RedisScripts[name].file)
  		RedisScripts[name].sha1 = self:script("LOAD", content)
  	end
  
  	return self:evalsha(RedisScripts[name].sha1, ...)
  end
  
  local meta = {__index = function (tab, name) return function (_, ...) tab[#tab+1]={name, ...} end end}
  function redisproxy:pipelining(block)
  	local ops = setmetatable({{"multi"}}, meta)
  	block(ops)
  	if #ops == 1 then return end
  	ops[#ops+1]={"exec"}
  	return self:pipeline(ops)
  end
  
  function redisproxy:insertEmail(params)
  	local pms = {
  		roleId = params.roleId,
  		emailId = params.emailId,
  		createtime = params.createtime or skynet.timex(),
  		con1 = params.con1 or "",
  		con2 = params.con2 or "",
  		con3 = params.con3 or "",
  		att1 = params.att1 or "",
  		att2 = params.att2 or "",
  		att3 = params.att3 or "",
  		title = params.title or "",
  		content = params.content or "",
  		attachments = params.attachments or "",
  	}
  	self:runScripts("insertEmail", 12,
  		pms.roleId, pms.emailId, pms.createtime,
  		pms.con1, pms.con2, pms.con3,
  		pms.att1, pms.att2, pms.att3,
  		pms.title, pms.content, pms.attachments)
  	return true
  end
  
  return redisproxy