-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredispool.lua
More file actions
186 lines (140 loc) · 3.17 KB
/
redispool.lua
File metadata and controls
186 lines (140 loc) · 3.17 KB
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
local skynet = require "skynet"
require "skynet.manager"
local redis = require "redis"
local CMD = {}
local pool = {}
local maxconn
local function getconn(uid)
local db
if not uid or maxconn == 1 then
db = pool[1]
else
db = pool[uid % (maxconn - 1) + 2]
end
return db
end
function CMD.start()
maxconn = tonumber(skynet.getenv("redis_maxinst")) or 2
for i = 1, maxconn do
local db = redis.connect{
host = skynet.getenv("redis_host" .. i),
port = skynet.getenv("redis_port" .. i),
db = 0,
auth = skynet.getenv("redis_auth" .. i),
}
if db then
db:flushdb() --测试期,清理redis数据
table.insert(pool, db)
else
skynet.error("redis connect error")
end
end
end
function CMD.set(uid, key, value)
local db = getconn(uid)
local retsult = db:set(key,value)
return retsult
end
function CMD.get(uid, key)
local db = getconn(uid)
local retsult = db:get(key)
return retsult
end
function CMD.hmset(uid, key, t)
local data = {}
for k, v in pairs(t) do
table.insert(data, k)
table.insert(data, v)
end
local db = getconn(uid)
local result = db:hmset(key, table.unpack(data))
return result
end
function CMD.hmget(uid, key, ...)
if not key then return end
local db = getconn(uid)
local result = db:hmget(key, ...)
return result
end
function CMD.hset(uid, key, filed, value)
local db = getconn(uid)
local result = db:hset(key,filed,value)
return result
end
function CMD.hget(uid, key, filed)
local db = getconn(uid)
local result = db:hget(key, filed)
return result
end
function CMD.hgetall(uid, key)
local db = getconn(uid)
local result = db:hgetall(key)
return result
end
function CMD.zadd(uid, key, score, member)
local db = getconn(uid)
local result = db:zadd(key, score, member)
return result
end
function CMD.keys(uid, key)
local db = getconn(uid)
local result = db:keys(key)
return result
end
function CMD.zrange(uid, key, from, to)
local db = getconn(uid)
local result = db:zrange(key, from, to)
return result
end
function CMD.zrevrange(uid, key, from, to ,scores)
local result
local db = getconn(uid)
if not scores then
result = db:zrevrange(key,from,to)
else
result = db:zrevrange(key,from,to,scores)
end
return result
end
function CMD.zrank(uid, key, member)
local db = getconn(uid)
local result = db:zrank(key,member)
return result
end
function CMD.zrevrank(uid, key, member)
local db = getconn(uid)
local result = db:zrevrank(key,member)
return result
end
function CMD.zscore(uid, key, score)
local db = getconn(uid)
local result = db:zscore(key,score)
return result
end
function CMD.zcount(uid, key, from, to)
local db = getconn(uid)
local result = db:zcount(key,from,to)
return result
end
function CMD.zcard(uid, key)
local db = getconn(uid)
local result = db:zcard(key)
return result
end
function CMD.incr(uid, key)
local db = getconn(uid)
local result = db:incr(key)
return result
end
function CMD.del(uid, key)
local db = getconn(uid)
local result = db:del(key)
return result
end
skynet.start(function()
skynet.dispatch("lua", function(session, source, cmd, ...)
local f = assert(CMD[cmd], cmd .. "not found")
skynet.retpack(f(...))
end)
skynet.register(SERVICE_NAME)
end)