-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathservice.go
53 lines (45 loc) · 1.09 KB
/
service.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
/*
* @Description:
* @Author: gphper
* @Date: 2021-09-21 14:45:08
*/
package service
import (
"context"
"grm/common"
"grm/global"
"grm/glog"
"net"
"time"
"github.com/go-redis/redis"
)
// 生成redis客户端
func NewRedisClient(conf global.RedisService) (*redis.Client, error) {
ctx := context.Background()
optConf := &redis.Options{
Addr: conf.Config.Addr,
Password: conf.Config.Password,
DialTimeout: 5 * time.Second,
}
if conf.UseSsh {
cli, err := common.GetSSHClient(conf.SSHConfig.SshUsername, conf.SSHConfig.SshPassword, net.JoinHostPort(conf.SSHConfig.SshHost, conf.SSHConfig.SshPort))
if nil != err {
return nil, err
}
optConf.DialTimeout = -1
optConf.WriteTimeout = -1
optConf.ReadTimeout = -1
optConf.Dialer = func(ctx context.Context, network, addr string) (net.Conn, error) {
return cli.Dial(network, addr)
}
}
client := redis.NewClient(optConf)
client.AddHook(common.RedisLog{
Logger: glog.NewLogger(conf.RedisService + "/redis.log"),
})
_, err := client.Ping(ctx).Result()
if err != nil {
return nil, err
}
return client, nil
}