-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
346 lines (278 loc) · 7.43 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
345
346
package redis
import (
"context"
"time"
"github.com/go-redis/redis/v8"
)
type Option struct {
// with port
Host string `env:""`
Password string `env:""`
Db int `env:""`
PoolSize int `env:""`
}
func (c *Option) SetDefaults() {
if c.PoolSize == 0 {
c.PoolSize = 10
}
}
type Redis struct {
ctx context.Context
Pool *redis.Client
}
func Init(ctx context.Context, c *Option) *Redis {
r := &Redis{}
r.ctx = ctx
c.SetDefaults()
r.Pool = redis.NewClient(&redis.Options{
Addr: c.Host,
Password: c.Password, // no password set
DB: c.Db, // use default DB
PoolSize: c.PoolSize,
PoolTimeout: time.Duration(time.Second * 5),
IdleTimeout: time.Duration(time.Second * 2),
IdleCheckFrequency: time.Duration(time.Second * 5),
})
err := r.Pool.Info(ctx).Err()
if err != nil {
panic(err)
}
return r
}
func (r *Redis) Info(key string) (value []byte, err error) {
value, err = r.Pool.Info(r.ctx, key).Bytes()
if err == redis.Nil {
return nil, err
} else if err != nil {
return nil, err
}
return
}
/*
1 exist 0 not exist
*/
func (r *Redis) IsExist(key string) bool {
ex, _ := r.Pool.Exists(r.ctx, key).Result()
return ex > 0
}
func (r *Redis) SAdd(key string, values ...interface{}) (err error) {
return r.Pool.SAdd(r.ctx, key, values).Err()
}
func (r *Redis) SMember(key string) (members []string, err error) {
return r.Pool.SMembers(r.ctx, key).Result()
}
func (r *Redis) SRem(key string, value string) (err error) {
return r.Pool.SRem(r.ctx, key, value).Err()
}
func (r *Redis) AddSetKey(key string, value []byte, expire int) (err error) {
if expire <= 0 {
err = r.Pool.Set(r.ctx, key, value, time.Duration(0)).Err()
} else {
err = r.Pool.Set(r.ctx, key, value, time.Duration(expire)*time.Second).Err()
}
if err != nil {
return err
}
return nil
}
func (r *Redis) SetKeepTTL(key string, value []byte) (err error) {
err = r.Pool.Set(r.ctx, key, value, redis.KeepTTL).Err()
if err != nil {
return err
}
return nil
}
func (r *Redis) Set(key string, value interface{}, expire time.Duration) (err error) {
return r.Pool.Set(r.ctx, key, value, expire).Err()
}
func (r *Redis) Get(key string) interface{} {
value, err := r.Pool.Get(r.ctx, key).Result()
if err != nil {
return nil
}
return value
}
func (r *Redis) GetByte(key string) (value []byte, err error) {
value, err = r.Pool.Get(r.ctx, key).Bytes()
if err != nil {
return nil, err
}
return
}
func (r *Redis) MGet(key ...string) (values []interface{}, err error) {
values, err = r.Pool.MGet(r.ctx, key...).Result()
if err != nil {
return nil, err
}
return
}
func (r *Redis) HGetAll(key string) (map[string]string, error) {
values, err := r.Pool.HGetAll(r.ctx, key).Result()
if err != nil {
return nil, err
}
return values, nil
}
func (r *Redis) HSet(key string, values map[string]interface{}) error {
return r.Pool.HSet(r.ctx, key, values).Err()
}
func (r *Redis) HGet(key, field string) ([]byte, error) {
return r.Pool.HGet(r.ctx, key, field).Bytes()
}
func (r *Redis) HExist(key, field string) (bool, error) {
return r.Pool.HExists(r.ctx, key, field).Result()
}
func (r *Redis) HMGet(key string, fields ...string) ([]interface{}, error) {
return r.Pool.HMGet(r.ctx, key, fields...).Result()
}
func (r *Redis) HKeys(key string) ([]string, error) {
return r.Pool.HKeys(r.ctx, key).Result()
}
func (r *Redis) Delete(key string) (err error) {
return r.Pool.Del(r.ctx, key).Err()
}
func (r *Redis) KeyList(key string, count int64) (keys []string, err error) {
cursor := uint64(0)
for {
Loop:
keyList, nextIndex, err := r.Scan(key, cursor, count)
if err != nil {
return nil, err
}
for index := range keyList {
keys = append(keys, keyList[index])
}
if nextIndex == 0 {
return keys, nil
}
cursor = nextIndex
goto Loop
}
}
func (r *Redis) Scan(key string, cursor uint64, count int64) ([]string, uint64, error) {
return r.Pool.Scan(r.ctx, cursor, key, count).Result()
}
func (r *Redis) SetNX(key string, value []byte, expire int) (success bool, err error) {
if expire <= 0 {
success, err = r.Pool.SetNX(r.ctx, key, value, time.Duration(0)).Result()
} else {
success, err = r.Pool.SetNX(r.ctx, key, value, time.Duration(expire)*time.Second).Result()
}
if err != nil {
return false, err
}
return success, nil
}
func (r *Redis) SetEX(key string, value []byte, expire int) (err error) {
if expire <= 0 {
err = r.Pool.SetEX(r.ctx, key, value, time.Duration(-1)).Err()
} else {
err = r.Pool.SetEX(r.ctx, key, value, time.Duration(expire)*time.Second).Err()
}
if err != nil {
return err
}
return nil
}
func (r *Redis) Lock(key string, nowTime int64, expiresIn time.Duration) (bool, error) {
success, err := r.Pool.SetNX(r.ctx, key, nowTime, expiresIn).Result()
if err != nil {
return false, err
}
return success, nil
}
func (r *Redis) Incr(key string) (int64, error) {
v, err := r.Pool.Incr(r.ctx, key).Result()
if err != nil {
return v, err
}
return v, nil
}
func (r *Redis) Expire(key string, expireTime time.Duration) error {
return r.Pool.Expire(r.ctx, key, expireTime).Err()
}
func (r *Redis) GetInt64(key string) (value int64, err error) {
value, err = r.Pool.Get(r.ctx, key).Int64()
return
}
func (r *Redis) SetInt64(key string, value int64, expire int64) error {
return r.Pool.Set(r.ctx, key, value, time.Duration(expire)*time.Second).Err()
}
func (r *Redis) TTL(key string) (time.Duration, error) {
return r.Pool.TTL(r.ctx, key).Result()
}
func (r *Redis) AddMessage(stream string, data []byte) error {
err := r.Pool.XAdd(r.ctx, &redis.XAddArgs{
Stream: stream,
ID: "",
Values: map[string]interface{}{
"data": data,
},
}).Err()
return err
}
func (r *Redis) CreateGroup(stream string, group string) error {
groups, err := r.Pool.XInfoGroups(r.ctx, stream).Result()
if err != nil {
if err.Error() == "ERR no such key" {
return r.Pool.XGroupCreateMkStream(r.ctx, stream, group, "0").Err()
}
return err
}
var found bool
for _, g := range groups {
if g.Name == group {
found = true
}
}
if found {
return nil
}
return r.Pool.XGroupCreateMkStream(r.ctx, stream, group, "0").Err()
}
func (r *Redis) ReadGroup(stream string, group string, consumerID string, count int64) ([]redis.XMessage, error) {
streams, err := r.Pool.XReadGroup(r.ctx, &redis.XReadGroupArgs{
Group: group,
Consumer: consumerID,
Streams: []string{stream, ">"},
Count: count,
}).Result()
if err != nil {
return nil, err
}
msgs := make([]redis.XMessage, 0)
for _, s := range streams {
for _, msg := range s.Messages {
msgs = append(msgs, msg)
}
}
return msgs, nil
}
func (r *Redis) ReadGroupBlock(stream string, group string, consumerID string, count int64, block time.Duration) ([]redis.XMessage, error) {
streams, err := r.Pool.XReadGroup(r.ctx, &redis.XReadGroupArgs{
Group: group,
Consumer: consumerID,
Streams: []string{stream, ">"},
Count: count,
Block: block,
}).Result()
if err != nil && err != redis.Nil {
return nil, err
}
if err == redis.Nil {
return nil, nil
}
msgs := make([]redis.XMessage, 0)
for _, s := range streams {
for _, msg := range s.Messages {
msgs = append(msgs, msg)
}
}
return msgs, nil
}
func (r *Redis) DeleteMessage(stream string, ids ...string) error {
return r.Pool.XDel(r.ctx, stream, ids...).Err()
}
func (r *Redis) AckMessage(stream string, group string, ids ...string) error {
return r.Pool.XAck(r.ctx, stream, group, ids...).Err()
}