-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathredis-cluster.go
72 lines (57 loc) · 1.35 KB
/
redis-cluster.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
// +build cluster
package redis
import (
"log"
"strings"
"sync"
"time"
"brooce/config"
"github.com/go-redis/redis"
)
var redisClient *redis.ClusterClient
var once sync.Once
func Get() *redis.ClusterClient {
once.Do(func() {
threads := len(config.Threads) + 10
redisClient = redis.NewClusterClient(&redis.ClusterOptions{
Addrs: strings.Split(config.Config.Redis.Host, ","),
Password: config.Config.Redis.Password,
MaxRetries: 10,
PoolSize: threads,
DialTimeout: 5 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 5 * time.Second,
PoolTimeout: 1 * time.Second,
})
for {
err := redisClient.Ping().Err()
if err == nil {
break
}
log.Println("Can't reach redis at", config.Config.Redis.Host, "-- are your redis addr and password right?", err)
time.Sleep(5 * time.Second)
}
})
return redisClient
}
func FlushList(src, dst string) (err error) {
redisClient := Get()
for err == nil {
_, err = redisClient.RPopLPush(src, dst).Result()
}
if err == redis.Nil {
err = nil
}
return
}
func ScanKeys(match string) (keys []string, err error) {
redisClient := Get()
err = redisClient.ForEachMaster(func(client *redis.Client) error {
iter := client.Scan(0, match, 10000).Iterator()
for iter.Next() {
keys = append(keys, iter.Val())
}
return iter.Err()
})
return
}