-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathredisOperator.go
More file actions
70 lines (65 loc) · 1.36 KB
/
redisOperator.go
File metadata and controls
70 lines (65 loc) · 1.36 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
package optredis
import (
"fmt"
"github.com/go-redis/redis"
)
var errRedisClientEmpty = fmt.Errorf("redist clinet is empty")
func ExistsKey(redisCli *redis.Client, key string) (bool, error) {
if redisCli == nil {
return false, errRedisClientEmpty
}
count, err := redisCli.Exists(key).Result()
if err != nil {
return false, err
}
if count == 0 {
return false, nil
}
return true, nil
}
// scan keys instead redisCli.Keys()
// redisCli *redis.Client
// match string
// maxCount int64
// return
// error scan error
// []string removed repeated key
func RedisScanKeysMatch(redisCli *redis.Client, match string, maxCount int64) ([]string, error) {
if redisCli == nil {
return nil, errRedisClientEmpty
}
var cursor uint64
var scanFull []string
for {
keys, cursor, err := redisCli.Scan(cursor, match, maxCount).Result()
if err != nil {
return nil, err
}
if len(keys) > 0 {
for _, v := range keys {
scanFull = append(scanFull, v)
}
}
if cursor == 0 {
break
}
}
scanRes := removeRepeatedElementString(scanFull)
return scanRes, nil
}
func removeRepeatedElementString(arr []string) (newArr []string) {
newArr = make([]string, 0)
for i := 0; i < len(arr); i++ {
repeat := false
for j := i + 1; j < len(arr); j++ {
if arr[i] == arr[j] {
repeat = true
break
}
}
if !repeat {
newArr = append(newArr, arr[i])
}
}
return
}