-
Notifications
You must be signed in to change notification settings - Fork 702
/
redis_cmds.go
576 lines (492 loc) · 13.9 KB
/
redis_cmds.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
// Copyright 2020 Douyu
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package redis
import (
"time"
"github.com/go-redis/redis"
)
// Get 从redis获取string
func (r *Redis) Get(key string) string {
var mes string
strObj := r.Client.Get(key)
if err := strObj.Err(); err != nil {
mes = ""
} else {
mes = strObj.Val()
}
return mes
}
// GetRaw ...
func (r *Redis) GetRaw(key string) ([]byte, error) {
c, err := r.Client.Get(key).Bytes()
if err != nil && err != redis.Nil {
return []byte{}, err
}
return c, nil
}
// MGet ...
func (r *Redis) MGet(keys ...string) ([]string, error) {
sliceObj := r.Client.MGet(keys...)
if err := sliceObj.Err(); err != nil && err != redis.Nil {
return []string{}, err
}
tmp := sliceObj.Val()
strSlice := make([]string, 0, len(tmp))
for _, v := range tmp {
if v != nil {
strSlice = append(strSlice, v.(string))
} else {
strSlice = append(strSlice, "")
}
}
return strSlice, nil
}
// MGets ...
func (r *Redis) MGets(keys []string) ([]interface{}, error) {
ret, err := r.Client.MGet(keys...).Result()
if err != nil && err != redis.Nil {
return []interface{}{}, err
}
return ret, nil
}
// Set 设置redis的string
func (r *Redis) Set(key string, value interface{}, expire time.Duration) bool {
err := r.Client.Set(key, value, expire).Err()
return err == nil
}
// HGetAll 从redis获取hash的所有键值对
func (r *Redis) HGetAll(key string) map[string]string {
hashObj := r.Client.HGetAll(key)
hash := hashObj.Val()
return hash
}
// HGet 从redis获取hash单个值
func (r *Redis) HGet(key string, fields string) (string, error) {
strObj := r.Client.HGet(key, fields)
err := strObj.Err()
if err != nil && err != redis.Nil {
return "", err
}
if err == redis.Nil {
return "", nil
}
return strObj.Val(), nil
}
// HMGetMap 批量获取hash值,返回map
func (r *Redis) HMGetMap(key string, fields []string) map[string]string {
if len(fields) == 0 {
return make(map[string]string)
}
sliceObj := r.Client.HMGet(key, fields...)
if err := sliceObj.Err(); err != nil && err != redis.Nil {
return make(map[string]string)
}
tmp := sliceObj.Val()
hashRet := make(map[string]string, len(tmp))
var tmpTagID string
for k, v := range tmp {
tmpTagID = fields[k]
if v != nil {
hashRet[tmpTagID] = v.(string)
} else {
hashRet[tmpTagID] = ""
}
}
return hashRet
}
// HMSet 设置redis的hash
func (r *Redis) HMSet(key string, hash map[string]interface{}, expire time.Duration) bool {
if len(hash) > 0 {
err := r.Client.HMSet(key, hash).Err()
if err != nil {
return false
}
if expire > 0 {
r.Client.Expire(key, expire)
}
return true
}
return false
}
// HSet hset
func (r *Redis) HSet(key string, field string, value interface{}) bool {
err := r.Client.HSet(key, field, value).Err()
return err == nil
}
// HDel ...
func (r *Redis) HDel(key string, field ...string) bool {
IntObj := r.Client.HDel(key, field...)
err := IntObj.Err()
return err == nil
}
// SetWithErr ...
func (r *Redis) SetWithErr(key string, value interface{}, expire time.Duration) error {
err := r.Client.Set(key, value, expire).Err()
return err
}
// SetNx 设置redis的string 如果键已存在
func (r *Redis) SetNx(key string, value interface{}, expiration time.Duration) bool {
result, err := r.Client.SetNX(key, value, expiration).Result()
if err != nil {
return false
}
return result
}
// SetNxWithErr 设置redis的string 如果键已存在
func (r *Redis) SetNxWithErr(key string, value interface{}, expiration time.Duration) (bool, error) {
result, err := r.Client.SetNX(key, value, expiration).Result()
return result, err
}
// Incr redis自增
func (r *Redis) Incr(key string) bool {
err := r.Client.Incr(key).Err()
return err == nil
}
// IncrWithErr ...
func (r *Redis) IncrWithErr(key string) (int64, error) {
ret, err := r.Client.Incr(key).Result()
return ret, err
}
// IncrBy 将 key 所储存的值加上增量 increment 。
func (r *Redis) IncrBy(key string, increment int64) (int64, error) {
intObj := r.Client.IncrBy(key, increment)
if err := intObj.Err(); err != nil {
return 0, err
}
return intObj.Val(), nil
}
// Decr redis自减
func (r *Redis) Decr(key string) bool {
err := r.Client.Decr(key).Err()
return err == nil
}
// Scan ...
func (r *Redis) Scan(cursor uint64, match string, count int64) ([]string, error) {
result, _, err := r.Client.Scan(cursor, match, count).Result()
return result, err
}
// Type ...
func (r *Redis) Type(key string) (string, error) {
statusObj := r.Client.Type(key)
if err := statusObj.Err(); err != nil {
return "", err
}
return statusObj.Val(), nil
}
// ZRevRange 倒序获取有序集合的部分数据
func (r *Redis) ZRevRange(key string, start, stop int64) ([]string, error) {
strSliceObj := r.Client.ZRevRange(key, start, stop)
if err := strSliceObj.Err(); err != nil && err != redis.Nil {
return []string{}, err
}
return strSliceObj.Val(), nil
}
// ZRevRangeWithScores ...
func (r *Redis) ZRevRangeWithScores(key string, start, stop int64) ([]redis.Z, error) {
zSliceObj := r.Client.ZRevRangeWithScores(key, start, stop)
if err := zSliceObj.Err(); err != nil && err != redis.Nil {
return []redis.Z{}, err
}
return zSliceObj.Val(), nil
}
// ZRange ...
func (r *Redis) ZRange(key string, start, stop int64) ([]string, error) {
strSliceObj := r.Client.ZRange(key, start, stop)
if err := strSliceObj.Err(); err != nil && err != redis.Nil {
return []string{}, err
}
return strSliceObj.Val(), nil
}
// ZRevRank ...
func (r *Redis) ZRevRank(key string, member string) (int64, error) {
intObj := r.Client.ZRevRank(key, member)
if err := intObj.Err(); err != nil && err != redis.Nil {
return 0, err
}
return intObj.Val(), nil
}
// ZRevRangeByScore ...
func (r *Redis) ZRevRangeByScore(key string, opt redis.ZRangeBy) ([]string, error) {
res, err := r.Client.ZRevRangeByScore(key, opt).Result()
if err != nil && err != redis.Nil {
return []string{}, err
}
return res, nil
}
// ZRevRangeByScoreWithScores ...
func (r *Redis) ZRevRangeByScoreWithScores(key string, opt redis.ZRangeBy) ([]redis.Z, error) {
res, err := r.Client.ZRevRangeByScoreWithScores(key, opt).Result()
if err != nil && err != redis.Nil {
return []redis.Z{}, err
}
return res, nil
}
// HMGet 批量获取hash值
func (r *Redis) HMGet(key string, fileds []string) []string {
sliceObj := r.Client.HMGet(key, fileds...)
if err := sliceObj.Err(); err != nil && err != redis.Nil {
return []string{}
}
tmp := sliceObj.Val()
strSlice := make([]string, 0, len(tmp))
for _, v := range tmp {
if v != nil {
strSlice = append(strSlice, v.(string))
} else {
strSlice = append(strSlice, "")
}
}
return strSlice
}
// ZCard 获取有序集合的基数
func (r *Redis) ZCard(key string) (int64, error) {
IntObj := r.Client.ZCard(key)
if err := IntObj.Err(); err != nil {
return 0, err
}
return IntObj.Val(), nil
}
// ZScore 获取有序集合成员 member 的 score 值
func (r *Redis) ZScore(key string, member string) (float64, error) {
FloatObj := r.Client.ZScore(key, member)
err := FloatObj.Err()
if err != nil && err != redis.Nil {
return 0, err
}
return FloatObj.Val(), err
}
// ZAdd 将一个或多个 member 元素及其 score 值加入到有序集 key 当中
func (r *Redis) ZAdd(key string, members ...redis.Z) (int64, error) {
IntObj := r.Client.ZAdd(key, members...)
if err := IntObj.Err(); err != nil && err != redis.Nil {
return 0, err
}
return IntObj.Val(), nil
}
// ZCount 返回有序集 key 中, score 值在 min 和 max 之间(默认包括 score 值等于 min 或 max )的成员的数量。
func (r *Redis) ZCount(key string, min, max string) (int64, error) {
IntObj := r.Client.ZCount(key, min, max)
if err := IntObj.Err(); err != nil && err != redis.Nil {
return 0, err
}
return IntObj.Val(), nil
}
// Del redis删除
func (r *Redis) Del(key string) int64 {
result, err := r.Client.Del(key).Result()
if err != nil {
return 0
}
return result
}
// DelWithErr ...
func (r *Redis) DelWithErr(key string) (int64, error) {
result, err := r.Client.Del(key).Result()
return result, err
}
// HIncrBy 哈希field自增
func (r *Redis) HIncrBy(key string, field string, incr int) int64 {
result, err := r.Client.HIncrBy(key, field, int64(incr)).Result()
if err != nil {
return 0
}
return result
}
// HIncrByWithErr 哈希field自增并且返回错误
func (r *Redis) HIncrByWithErr(key string, field string, incr int) (int64, error) {
return r.Client.HIncrBy(key, field, int64(incr)).Result()
}
// Exists 键是否存在
func (r *Redis) Exists(key string) bool {
result, err := r.Client.Exists(key).Result()
if err != nil {
return false
}
return result == 1
}
// ExistsWithErr ...
func (r *Redis) ExistsWithErr(key string) (bool, error) {
result, err := r.Client.Exists(key).Result()
if err != nil {
return false, err
}
return result == 1, nil
}
// LPush 将一个或多个值 value 插入到列表 key 的表头
func (r *Redis) LPush(key string, values ...interface{}) (int64, error) {
IntObj := r.Client.LPush(key, values...)
if err := IntObj.Err(); err != nil {
return 0, err
}
return IntObj.Val(), nil
}
// RPush 将一个或多个值 value 插入到列表 key 的表尾(最右边)。
func (r *Redis) RPush(key string, values ...interface{}) (int64, error) {
IntObj := r.Client.RPush(key, values...)
if err := IntObj.Err(); err != nil {
return 0, err
}
return IntObj.Val(), nil
}
// RPop 移除并返回列表 key 的尾元素。
func (r *Redis) RPop(key string) (string, error) {
strObj := r.Client.RPop(key)
if err := strObj.Err(); err != nil {
return "", err
}
return strObj.Val(), nil
}
// LRange 获取列表指定范围内的元素
func (r *Redis) LRange(key string, start, stop int64) ([]string, error) {
result, err := r.Client.LRange(key, start, stop).Result()
if err != nil {
return []string{}, err
}
return result, nil
}
// LLen ...
func (r *Redis) LLen(key string) int64 {
IntObj := r.Client.LLen(key)
if err := IntObj.Err(); err != nil {
return 0
}
return IntObj.Val()
}
// LLenWithErr ...
func (r *Redis) LLenWithErr(key string) (int64, error) {
ret, err := r.Client.LLen(key).Result()
return ret, err
}
// LRem ...
func (r *Redis) LRem(key string, count int64, value interface{}) int64 {
IntObj := r.Client.LRem(key, count, value)
if err := IntObj.Err(); err != nil {
return 0
}
return IntObj.Val()
}
// LIndex ...
func (r *Redis) LIndex(key string, idx int64) (string, error) {
ret, err := r.Client.LIndex(key, idx).Result()
return ret, err
}
// LTrim ...
func (r *Redis) LTrim(key string, start, stop int64) (string, error) {
ret, err := r.Client.LTrim(key, start, stop).Result()
return ret, err
}
// ZRemRangeByRank 移除有序集合中给定的排名区间的所有成员
func (r *Redis) ZRemRangeByRank(key string, start, stop int64) (int64, error) {
result, err := r.Client.ZRemRangeByRank(key, start, stop).Result()
if err != nil {
return 0, err
}
return result, nil
}
// Expire 设置过期时间
func (r *Redis) Expire(key string, expiration time.Duration) (bool, error) {
result, err := r.Client.Expire(key, expiration).Result()
if err != nil {
return false, err
}
return result, err
}
// ZRem 从zset中移除变量
func (r *Redis) ZRem(key string, members ...interface{}) (int64, error) {
result, err := r.Client.ZRem(key, members...).Result()
if err != nil {
return 0, err
}
return result, nil
}
// SAdd 向set中添加成员
func (r *Redis) SAdd(key string, member ...interface{}) (int64, error) {
intObj := r.Client.SAdd(key, member...)
if err := intObj.Err(); err != nil {
return 0, err
}
return intObj.Val(), nil
}
// SMembers 返回set的全部成员
func (r *Redis) SMembers(key string) ([]string, error) {
strSliceObj := r.Client.SMembers(key)
if err := strSliceObj.Err(); err != nil {
return []string{}, err
}
return strSliceObj.Val(), nil
}
// SIsMember ...
func (r *Redis) SIsMember(key string, member interface{}) (bool, error) {
boolObj := r.Client.SIsMember(key, member)
if err := boolObj.Err(); err != nil {
return false, err
}
return boolObj.Val(), nil
}
// HKeys 获取hash的所有域
func (r *Redis) HKeys(key string) []string {
strObj := r.Client.HKeys(key)
if err := strObj.Err(); err != nil && err != redis.Nil {
return []string{}
}
return strObj.Val()
}
// HLen 获取hash的长度
func (r *Redis) HLen(key string) int64 {
intObj := r.Client.HLen(key)
if err := intObj.Err(); err != nil && err != redis.Nil {
return 0
}
return intObj.Val()
}
// GeoAdd 写入地理位置
func (r *Redis) GeoAdd(key string, location *redis.GeoLocation) (int64, error) {
res, err := r.Client.GeoAdd(key, location).Result()
if err != nil {
return 0, err
}
return res, nil
}
// GeoRadius 根据经纬度查询列表
func (r *Redis) GeoRadius(key string, longitude, latitude float64, query *redis.GeoRadiusQuery) ([]redis.GeoLocation, error) {
res, err := r.Client.GeoRadius(key, longitude, latitude, query).Result()
if err != nil {
return []redis.GeoLocation{}, err
}
return res, nil
}
// TTL 查询过期时间
func (r *Redis) TTL(key string) (int64, error) {
if result, err := r.Client.TTL(key).Result(); err != nil {
return 0, err
} else {
return int64(result.Seconds()), nil
}
}
// Close closes the cluster client, releasing any open resources.
//
// It is rare to Close a ClusterClient, as the ClusterClient is meant
// to be long-lived and shared between many goroutines.
func (r *Redis) Close() (err error) {
err = nil
if r.Client != nil {
if r.Cluster() != nil {
err = r.Cluster().Close()
}
if r.Stub() != nil {
err = r.Stub().Close()
}
}
return err
}