Commit 922ce27a77367f095107220737ef2eb9c097a014
1 parent
c6940d28
pika 判断
Showing
1 changed file
with
59 additions
and
5 deletions
Show diff stats
src/shared/redisproxy.lua
... | ... | @@ -5,6 +5,10 @@ local table_insert = table.insert |
5 | 5 | |
6 | 6 | local redisproxy = {} |
7 | 7 | |
8 | + | |
9 | +local isUsePika = false | |
10 | + | |
11 | + | |
8 | 12 | setmetatable(redisproxy, { __index = function(t, k) |
9 | 13 | local cmd = string.upper(k) |
10 | 14 | local f = function (self, ...) |
... | ... | @@ -38,14 +42,61 @@ function redisproxy:runScripts(name, ...) |
38 | 42 | end |
39 | 43 | |
40 | 44 | local meta = {__index = function (tab, name) return function (_, ...) tab[#tab+1]={name, ...} end end} |
41 | -function redisproxy:pipelining(block) | |
42 | - local ops = setmetatable({{"multi"}}, meta) | |
45 | +function redisproxy:pipelining(block, transaction) | |
46 | + if transaction == nil then transaction = true end -- 默认具有事务性 | |
47 | + if isUsePika then transaction = false end | |
48 | + | |
49 | + local ops = setmetatable({}, meta) | |
50 | + if transaction then | |
51 | + ops[#ops+1]={"multi"} | |
52 | + end | |
43 | 53 | block(ops) |
44 | - if #ops == 1 then return end | |
45 | - ops[#ops+1]={"exec"} | |
46 | - return self:pipeline(ops) | |
54 | + if transaction then | |
55 | + if #ops == 1 then return end | |
56 | + ops[#ops+1]={"exec"} | |
57 | + else | |
58 | + if #ops == 0 then return end | |
59 | + end | |
60 | + return self:pipeline(ops, transaction) | |
61 | +end | |
62 | + | |
63 | +if isUsePika then | |
64 | + | |
65 | +function redisproxy:insertEmail(params) | |
66 | + local roleId = params.roleId | |
67 | + local emailId = params.emailId | |
68 | + local createtime = params.createtime or skynet.timex() | |
69 | + local contentPms = params.contentPms or {} | |
70 | + local rewardPms = params.rewardPms or {} | |
71 | + local title = params.title or "" | |
72 | + local stitle = params.stitle or "" | |
73 | + local content = params.content or "" | |
74 | + local attachments = params.attachments or "" | |
75 | + | |
76 | + local id = self:HINCRBY(string.format("role:%d:autoincr", roleId), "email", 1) | |
77 | + self:LPUSH(string.format("role:%d:emailIds", roleId), id) | |
78 | + local deleteIds = self:LRANGE(string.format("role:%d:emailIds", roleId), EMAIL_LIMIT, -1) | |
79 | + self:pipelining(function(red) | |
80 | + for _, deleteId in ipairs(deleteIds) do | |
81 | + red:DEL(string.format("email:%d:%d", roleId, deleteId)) | |
82 | + end | |
83 | + red:LTRIM(string.format("role:%d:emailIds", roleId), 0, EMAIL_LIMIT - 1) | |
84 | + red:HMSET(string.format("email:%d:%d", roleId, id), | |
85 | + "id", tostring(id), | |
86 | + "emailId", emailId, | |
87 | + "status", "0", | |
88 | + "createtime", createtime, | |
89 | + "contentPms", MsgPack.pack(contentPms), | |
90 | + "rewardPms", MsgPack.pack(rewardPms), | |
91 | + "title", title, | |
92 | + "stitle", stitle, | |
93 | + "content", content, | |
94 | + "attachments", attachments) | |
95 | + end) | |
47 | 96 | end |
48 | 97 | |
98 | +else | |
99 | + | |
49 | 100 | function redisproxy:insertEmail(params) |
50 | 101 | local pms = { |
51 | 102 | roleId = params.roleId, |
... | ... | @@ -64,4 +115,7 @@ function redisproxy:insertEmail(params) |
64 | 115 | return true |
65 | 116 | end |
66 | 117 | |
118 | + | |
119 | +end | |
120 | + | |
67 | 121 | return redisproxy |
68 | 122 | \ No newline at end of file | ... | ... |