Blame view

src/shared/redisproxy.lua 3.53 KB
314bc5df   zhengshouren   提交服务器初始代码
1
  local skynet = require "skynet"
3f604f2e   zhouhaihai   扩容 redis 和 log服务
2
3
4
5
6
7
8
9
  require "utils.init"
  
  local redisd_count = tonumber(skynet.getenv("thread"))
  local redisd
  skynet.init(function()
  	local idx = math.randomInt(1, redisd_count)
  	redisd = skynet.localname(".redis" .. idx)
  end)
314bc5df   zhengshouren   提交服务器初始代码
10
11
12
13
14
  
  local table_insert = table.insert
  
  local redisproxy = {}
  
922ce27a   zhouhaihai   pika 判断
15
16
17
  local isUsePika = false
  
  
314bc5df   zhengshouren   提交服务器初始代码
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
  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}
922ce27a   zhouhaihai   pika 判断
51
52
53
54
55
56
57
58
  function redisproxy:pipelining(block, transaction)
  	if transaction == nil then transaction = true end  -- 默认具有事务性
  	if isUsePika then transaction = false end
  
  	local ops = setmetatable({}, meta)
  	if transaction then
  		ops[#ops+1]={"multi"}
  	end
314bc5df   zhengshouren   提交服务器初始代码
59
  	block(ops)
922ce27a   zhouhaihai   pika 判断
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
  	if transaction then
  		if #ops == 1 then return end
  		ops[#ops+1]={"exec"}
  	else
  		if #ops == 0 then return end
  	end
  	return self:pipeline(ops, transaction)
  end
  
  if isUsePika then
  
  function redisproxy:insertEmail(params)
  	local roleId = params.roleId
  	local emailId = params.emailId
  	local createtime = params.createtime or skynet.timex()
  	local contentPms = params.contentPms or {}
  	local rewardPms = params.rewardPms or {}
  	local title = params.title or ""
  	local stitle = params.stitle or ""
  	local content = params.content or ""
  	local attachments = params.attachments or ""
  
  	local id = self:HINCRBY(string.format("role:%d:autoincr", roleId), "email", 1)
  	self:LPUSH(string.format("role:%d:emailIds", roleId), id)
  	local deleteIds = self:LRANGE(string.format("role:%d:emailIds", roleId), EMAIL_LIMIT, -1)
  	self:pipelining(function(red)
  		for _, deleteId in ipairs(deleteIds) do
  			red:DEL(string.format("email:%d:%d", roleId, deleteId))
  		end
  		red:LTRIM(string.format("role:%d:emailIds", roleId), 0, EMAIL_LIMIT - 1)
  		red:HMSET(string.format("email:%d:%d", roleId, id), 
  			"id", tostring(id), 
  			"emailId", emailId,
  			"status", "0", 
  			"createtime", createtime,
  			"contentPms", MsgPack.pack(contentPms), 
  			"rewardPms", MsgPack.pack(rewardPms), 
  			"title", title, 
  			"stitle", stitle, 
  			"content", content, 
  			"attachments", attachments)
  	end)
314bc5df   zhengshouren   提交服务器初始代码
102
103
  end
  
922ce27a   zhouhaihai   pika 判断
104
105
  else
  
314bc5df   zhengshouren   提交服务器初始代码
106
107
108
109
110
  function redisproxy:insertEmail(params)
  	local pms = {
  		roleId = params.roleId,
  		emailId = params.emailId,
  		createtime = params.createtime or skynet.timex(),
3df73a9e   zhouhaihai   复兴奖励
111
112
  		contentPms = params.contentPms or {},
  		rewardPms = params.rewardPms or {},
314bc5df   zhengshouren   提交服务器初始代码
113
  		title = params.title or "",
eb4b0152   zhouhaihai   邮件增加 stitle
114
  		stitle = params.stitle or "",
314bc5df   zhengshouren   提交服务器初始代码
115
116
117
  		content = params.content or "",
  		attachments = params.attachments or "",
  	}
eb4b0152   zhouhaihai   邮件增加 stitle
118
  	self:runScripts("insertEmail", 10, EMAIL_LIMIT,
314bc5df   zhengshouren   提交服务器初始代码
119
  		pms.roleId, pms.emailId, pms.createtime,
3df73a9e   zhouhaihai   复兴奖励
120
  		MsgPack.pack(pms.contentPms), MsgPack.pack(pms.rewardPms), pms.title, pms.stitle, pms.content, pms.attachments)
314bc5df   zhengshouren   提交服务器初始代码
121
122
123
  	return true
  end
  
922ce27a   zhouhaihai   pika 判断
124
125
126
  
  end
  
314bc5df   zhengshouren   提交服务器初始代码
127
  return redisproxy