/
redis.go
151 lines (140 loc) · 3.87 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package settings
import (
"errors"
"fmt"
"time"
mylog "github.com/captainlee1024/go-gateway/internal/proxy_service/log"
"github.com/garyburd/redigo/redis"
)
/*
// go-redis
var (
rdb *redis.Client
// rdbMap map[string]*redis.Client
)
// Init 初始化 redis 默认连接
func Init(name string) (err error) {
if settings.ConfRedisMap != nil && settings.ConfRedisMap.List != nil {
for confName, cfg := range settings.ConfRedisMap.List {
if name == confName {
rdb = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d",
cfg.Host,
cfg.Port,
),
Password: cfg.Password,
DB: cfg.DB,
PoolSize: cfg.PoolSize,
})
}
}
}
_, err = rdb.Ping().Result()
return
}
// Close 关闭 redis 连接
func Close() {
_ = rdb.Close()
}
*/
// redigo
// ConnFactory 获取连接
func ConnFactory(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))]
randHost := cfg.Host + fmt.Sprintf(":%d", cfg.Port)
// 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")
}
// LogDo 带有日志的 Do 方法
func LogDo(trace *mylog.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 {
mylog.Log.Error("redis", 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)
mylog.Log.Info("redis", 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
}
// ConfDo 通过配置 执行redis
func ConfDo(trace *mylog.TraceContext, name string, commandName string, args ...interface{}) (interface{}, error) {
c, err := ConnFactory(name)
if err != nil {
mylog.Log.Error("redis", 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 {
mylog.Log.Error("redis", 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)
mylog.Log.Info("redis", 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
}