forked from bitleak/lmstfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
37 lines (34 loc) · 976 Bytes
/
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
package helper
import (
"strings"
"github.com/bitleak/lmstfy/config"
"github.com/bitleak/lmstfy/engine/redis/hooks"
"github.com/go-redis/redis/v8"
)
// NewRedisClient wrap the standalone and sentinel client
func NewRedisClient(conf *config.RedisConf, opt *redis.Options) (client *redis.Client) {
if opt == nil {
opt = &redis.Options{}
}
opt.Addr = conf.Addr
opt.Password = conf.Password
opt.PoolSize = conf.PoolSize
opt.DB = conf.DB
if conf.IsSentinel() {
client = redis.NewFailoverClient(&redis.FailoverOptions{
MasterName: conf.MasterName,
SentinelAddrs: strings.Split(opt.Addr, ","),
Password: opt.Password,
PoolSize: opt.PoolSize,
ReadTimeout: opt.ReadTimeout,
WriteTimeout: opt.WriteTimeout,
MinIdleConns: opt.MinIdleConns,
DB: opt.DB,
})
client.AddHook(hooks.NewMetricsHook(client))
return client
}
client = redis.NewClient(opt)
client.AddHook(hooks.NewMetricsHook(client))
return client
}