-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
executable file
·110 lines (104 loc) · 2.96 KB
/
redis.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package lib
import (
"errors"
"fmt"
"math/rand"
"time"
"github.com/garyburd/redigo/redis"
)
func RedisConnFactory(name string) (redis.Conn, error) {
if ConfRedisMap != nil && ConfRedisMap.List != nil {
for confName, cfg := range ConfRedisMap.List {
if name == confName {
randHost := cfg.ProxyList[rand.Intn(len(cfg.ProxyList))]
if cfg.ConnTimeout == 0 {
cfg.ConnTimeout = 50
}
if cfg.ReadTimeout == 0 {
cfg.ReadTimeout = 100
}
if cfg.WriteTimeout == 0 {
cfg.WriteTimeout = 100
}
c, err := redis.Dial(
"tcp",
randHost,
redis.DialConnectTimeout(time.Duration(cfg.ConnTimeout)*time.Millisecond),
redis.DialReadTimeout(time.Duration(cfg.ReadTimeout)*time.Millisecond),
redis.DialWriteTimeout(time.Duration(cfg.WriteTimeout)*time.Millisecond))
if err != nil {
return nil, err
}
if cfg.Password != "" {
if _, err := c.Do("AUTH", cfg.Password); err != nil {
c.Close()
return nil, err
}
}
if cfg.Db != 0 {
if _, err := c.Do("SELECT", cfg.Db); err != nil {
c.Close()
return nil, err
}
}
return c, nil
}
}
}
return nil, errors.New("create redis conn fail")
}
func RedisLogDo(trace *TraceContext, c redis.Conn, commandName string, args ...interface{}) (interface{}, error) {
startExecTime := time.Now()
reply, err := c.Do(commandName, args...)
endExecTime := time.Now()
if err != nil {
Log.TagError(trace, "_com_redis_failure", map[string]interface{}{
"method": commandName,
"err": err,
"bind": args,
"proc_time": fmt.Sprintf("%fs", endExecTime.Sub(startExecTime).Seconds()),
})
} else {
replyStr, _ := redis.String(reply, nil)
Log.TagInfo(trace, "_com_redis_success", map[string]interface{}{
"method": commandName,
"bind": args,
"reply": replyStr,
"proc_time": fmt.Sprintf("%fs", endExecTime.Sub(startExecTime).Seconds()),
})
}
return reply, err
}
//通过配置 执行redis
func RedisConfDo(trace *TraceContext, name string, commandName string, args ...interface{}) (interface{}, error) {
c, err := RedisConnFactory(name)
if err != nil {
Log.TagError(trace, "_com_redis_failure", map[string]interface{}{
"method": commandName,
"err": errors.New("RedisConnFactory_error:" + name),
"bind": args,
})
return nil, err
}
defer c.Close()
startExecTime := time.Now()
reply, err := c.Do(commandName, args...)
endExecTime := time.Now()
if err != nil {
Log.TagError(trace, "_com_redis_failure", map[string]interface{}{
"method": commandName,
"err": err,
"bind": args,
"proc_time": fmt.Sprintf("%fs", endExecTime.Sub(startExecTime).Seconds()),
})
} else {
replyStr, _ := redis.String(reply, nil)
Log.TagInfo(trace, "_com_redis_success", map[string]interface{}{
"method": commandName,
"bind": args,
"reply": replyStr,
"proc_time": fmt.Sprintf("%fs", endExecTime.Sub(startExecTime).Seconds()),
})
}
return reply, err
}