-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
344 lines (291 loc) · 9.31 KB
/
redis.go
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
package redis
import (
"strings"
"time"
"github.com/go-ini/ini"
"github.com/gomodule/redigo/redis"
)
// ConnPool redis连接池
type ConnPool struct {
redisPool *redis.Pool
}
// InitRedisPool 初始化
func InitRedisPool() *ConnPool {
cfg, err := ini.Load("./conf/config.ini")
if err != nil {
panic(err)
}
cfgSec := cfg.Section("redis")
host := cfgSec.Key("host").String() //redis地址
pwd := cfgSec.Key("pwd").String() //redis密码
dbIndex, _ := cfgSec.Key("db_index").Int() //数据库序号
maxIdle, _ := cfgSec.Key("max_idle").Int() //最大空闲连接数
maxActive, _ := cfgSec.Key("max_active").Int() //最大连接数
idleTimeout, _ := cfgSec.Key("idle_timeout").Int() //空闲连接超时(秒)
connTimeout, _ := cfgSec.Key("conn_timeout").Int() //连接、读写超时(秒)
redisAddrs := strings.Split(host, "|")
sntnl := &Sentinel{
Addrs: redisAddrs,
MasterName: "master1",
Dial: func(addr string) (redis.Conn, error) {
timeout := time.Duration(connTimeout) * time.Second
c, err := redis.DialTimeout("tcp", addr, timeout, timeout, timeout)
if err != nil {
return nil, err
}
return c, nil
},
}
rcp := &ConnPool{}
rcp.redisPool = &redis.Pool{
MaxIdle: maxIdle,
MaxActive: maxActive,
IdleTimeout: time.Duration(idleTimeout) * time.Second,
Wait: true,
Dial: func() (redis.Conn, error) {
// 不少于三个地址才启用sentinel
if len(redisAddrs) >= 3 {
masterAddr, err := sntnl.MasterAddr()
if err != nil {
return nil, err
}
host = masterAddr
}
c, err := redis.Dial("tcp", host,
redis.DialPassword(pwd),
redis.DialDatabase(dbIndex),
redis.DialConnectTimeout(time.Duration(connTimeout)*time.Second),
redis.DialReadTimeout(time.Duration(connTimeout)*time.Second),
redis.DialWriteTimeout(time.Duration(connTimeout)*time.Second))
if err != nil {
return nil, err
}
return c, nil
},
}
return rcp
}
/*############################## Key ##############################*/
// ExistsKey for key
func (rc *ConnPool) ExistsKey(key string) (bool, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Bool(conn.Do("EXISTS", key))
}
// DelKey for key
func (rc *ConnPool) DelKey(key string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("DEL", key)
}
// ExpireKey for key
func (rc *ConnPool) ExpireKey(key string, seconds int) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("EXPIRE", key, seconds)
}
/*############################## String ##############################*/
// Get for string
func (rc *ConnPool) Get(key string) (string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.String(conn.Do("GET", key))
}
// GetErrNil for string
func (rc *ConnPool) GetErrNil(key string) (string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
rep, err := redis.String(conn.Do("GET", key))
if err == redis.ErrNil {
return "", nil
}
return rep, err
}
// Set for string
func (rc *ConnPool) Set(key string, value string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("SET", key, value)
}
// SetExpire for string
func (rc *ConnPool) SetExpire(key string, value string, seconds int) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("SET", key, value, "EX", seconds)
}
// Incr for string
func (rc *ConnPool) Incr(key string) (int64, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int64(conn.Do("INCR", key))
}
// Decr for string
func (rc *ConnPool) Decr(key string) (int64, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int64(conn.Do("DECR", key))
}
/*############################## Hash ##############################*/
// Hset for Hash
func (rc *ConnPool) Hset(key string, field string, value string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("HSET", key, field, value)
}
// Hget for Hash
func (rc *ConnPool) Hget(key string, field string) (string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.String(conn.Do("HGET", key, field))
}
// Hmset for Hash
func (rc *ConnPool) Hmset(key string, fieldValue map[string]string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("HMSET", redis.Args{}.Add(key).AddFlat(fieldValue)...)
}
// Hmget for Hash
func (rc *ConnPool) Hmget(key string, field []string) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("HMGET", redis.Args{}.Add(key).AddFlat(field)...))
}
// Hlen for Hash
func (rc *ConnPool) Hlen(key string) (int, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int(conn.Do("HLEN", key))
}
// Hexists for Hash
func (rc *ConnPool) Hexists(key string, field string) (bool, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Bool(conn.Do("HEXISTS", key, field))
}
// Hdel for Hash
func (rc *ConnPool) Hdel(key string, field []string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("HDEL", key, field)
}
// Hgetall for Hash
func (rc *ConnPool) Hgetall(key string) (map[string]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.StringMap(conn.Do("HGETALL", key))
}
/*############################## List ##############################*/
// Lpush for List
func (rc *ConnPool) Lpush(key string, value []string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("LPUSH", redis.Args{}.Add(key).AddFlat(value)...)
}
// Rpop for List
func (rc *ConnPool) Rpop(key string) (string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.String(conn.Do("RPOP", key))
}
// Brpop for List
func (rc *ConnPool) Brpop(key string, timeoutSeconds int) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("BRPOP", key, timeoutSeconds))
}
// Llen for List
func (rc *ConnPool) Llen(key string) (int, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int(conn.Do("LLEN", key))
}
/*############################## Set ##############################*/
// Sadd for Set
func (rc *ConnPool) Sadd(key string, member []string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("SADD", redis.Args{}.Add(key).AddFlat(member)...)
}
// Smembers for Set
func (rc *ConnPool) Smembers(key string) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("SMEMBERS", key))
}
// Srem for Set
func (rc *ConnPool) Srem(key string, member []string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("SREM", key, member)
}
// Scard for Set
func (rc *ConnPool) Scard(key string) (int, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int(conn.Do("SCARD", key))
}
/*############################## SortedSet ##############################*/
// Zadd for SortedSet
func (rc *ConnPool) Zadd(key string, score int64, member string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("ZADD", key, score, member)
}
// Zrange for SortedSet
func (rc *ConnPool) Zrange(key string, start, stop int) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("ZRANGE", key, start, stop))
}
// Zrevrange for SortedSet
func (rc *ConnPool) Zrevrange(key string, start, stop int) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("ZREVRANGE", key, start, stop))
}
// Zrangebyscore for SortedSet
// all: -inf ~ +inf min ~ max
// min-max: (min ~ (max
func (rc *ConnPool) Zrangebyscore(key string, min, max string, offset, limit int) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("ZRANGEBYSCORE", key, min, max, "LIMIT", offset, limit))
}
// Zrevrangebyscore for SortedSet
func (rc *ConnPool) Zrevrangebyscore(key string, max, min string, offset, limit int) ([]string, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Strings(conn.Do("ZREVRANGEBYSCORE", key, max, min, "LIMIT", offset, limit))
}
// Zcard for SortedSet
func (rc *ConnPool) Zcard(key string) (int, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return redis.Int(conn.Do("ZCARD", key))
}
// Zrem for SortedSet
func (rc *ConnPool) Zrem(key string, member string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("ZREM", key, member)
}
// Zremrangebyrank for SortedSet
func (rc *ConnPool) Zremrangebyrank(key string, start, stop int) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("ZREMRANGEBYRANK", key, start, stop)
}
// Zremrangebyscore for SortedSet
func (rc *ConnPool) Zremrangebyscore(key string, min, max int64) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
return conn.Do("ZREMRANGEBYSCORE", key, min, max)
}
/*############################## Lua Script ##############################*/
// LuaScriptZset for SortedSet
func (rc *ConnPool) LuaScriptZset(script, key string, score int64, member string) (interface{}, error) {
conn := rc.redisPool.Get()
defer conn.Close()
lua := redis.NewScript(1, script)
return lua.Do(conn, key, score, member)
}