-
Notifications
You must be signed in to change notification settings - Fork 0
/
goredis_util.go
68 lines (60 loc) · 1.37 KB
/
goredis_util.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
package e2e
import (
"github.com/go-redis/redis"
uuid "github.com/satori/go.uuid"
"golang.org/x/sync/errgroup"
"time"
)
const defaultTimeOut = time.Second * 2
// GoRedis contains ClusterClient.
type GoRedis struct {
client *redis.ClusterClient
password string
}
// NewGoRedis return a new ClusterClient.
func NewGoRedis(addr, password string) *GoRedis {
return &GoRedis{
client: redis.NewClusterClient(&redis.ClusterOptions{
Addrs: []string{addr},
Password: password,
//MaxRetries: 5,
//
//PoolSize: 3,
//MinIdleConns: 1,
//PoolTimeout: defaultTimeOut,
//IdleTimeout: defaultTimeOut,
}),
password: password,
}
}
// StuffingData filled with (round * n)'s key.
func (g *GoRedis) StuffingData(round, n int) error {
var group errgroup.Group
for i := 0; i < round; i++ {
group.Go(func() error {
for j := 0; j < n; j++ {
key := uuid.NewV4().String()
if err := g.client.Set(key, key, 0).Err(); err != nil {
return err
}
}
return nil
})
}
if err := group.Wait(); err != nil {
return err
}
return nil
}
// DBSize return DBsize of all master nodes.
func (g *GoRedis) DBSize() (int64, error) {
return g.client.DBSize().Result()
}
// Password return redis password.
func (g *GoRedis) Password() string {
return g.password
}
// Close closes the cluster client.
func (g *GoRedis) Close() error {
return g.client.Close()
}