-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_cluster.go
62 lines (53 loc) · 1.41 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
package letsgo
import (
"fmt"
"log"
"time"
"github.com/gomodule/redigo/redis" //redigo
"github.com/mna/redisc" //redis cluster base on redigo
)
//Lredisc 结构体
type Lredisc struct {
Redisc *redisc.Cluster
}
//newLredisc 返回一个Lredis结构体指针
func newLredisc() *Lredisc {
return &Lredisc{}
}
//Init 连接redis cluster集群
func (c *Lredisc) Init(serverlist []string, options []redis.DialOption) error {
c.Redisc = &redisc.Cluster{
StartupNodes: serverlist,
DialOptions: options,
CreatePool: RedisCreatePool,
}
if err := c.Redisc.Refresh(); err != nil {
return err
}
return nil
}
//GetConn 得到一个redis.Conn 如果Retry为true返回一个redisc retryConn否则是一个普通的redisc conn
func (c *Lredisc) GetConn(Retry bool) redis.Conn {
rediscconn := c.Redisc.Get()
if Retry == true {
retryConn, err := redisc.RetryConn(rediscconn, 3, 100*time.Millisecond)
if err != nil {
log.Fatal("RetryConn failed:", err)
}
return retryConn
} else {
return rediscconn
}
}
//DoOnce 映射redisc.Do 方法
func (c *Lredisc) DoOnce(commandName string, args ...interface{}) (reply interface{}, err error) {
redisconn := c.Redisc.Get()
defer redisconn.Close()
if redisconn.Err() != nil {
return nil, fmt.Errorf("Lredisc:err while conn: %s", redisconn.Err().Error())
}
return redisconn.Do(commandName, args...)
}
func (c *Lredisc) Close() error {
return c.Redisc.Close()
}