-
Notifications
You must be signed in to change notification settings - Fork 2
/
standalone.go
59 lines (53 loc) · 1.29 KB
/
standalone.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
package goredis
import (
"time"
"github.com/gomodule/redigo/redis"
)
type StandaloneClient struct {
cli *redis.Pool
dbName string
}
func NewStandaloneClient(dbName string, addr string, option *Option) *StandaloneClient {
cli := &StandaloneClient{}
cli.Init(dbName, addr, option)
return cli
}
func (this *StandaloneClient) Init(dbName string, addr string, option *Option) {
this.dbName = dbName
this.cli = &redis.Pool{
MaxIdle: option.PoolMaxIdle,
MaxActive: option.PoolMaxActive,
Wait: option.PoolWait,
IdleTimeout: option.PoolIdleTimeout,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", addr)
if err != nil {
return nil, err
}
if option.Password != "" {
if _, err := c.Do("AUTH", option.Password); err != nil {
c.Close()
return nil, err
}
}
if _, err := c.Do("SELECT", option.DBIndex); err != nil {
c.Close()
return nil, err
}
return c, nil
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func (this *StandaloneClient) Do(commandName string, args ...interface{}) (reply interface{}, err error) {
conn := this.cli.Get()
if conn.Err() == nil {
defer conn.Close()
return conn.Do(commandName, args...)
} else {
return nil, conn.Err()
}
}