-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
rueidis.go
55 lines (45 loc) · 1.17 KB
/
rueidis.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
package datastore
import (
"context"
"crypto/tls"
"net"
"time"
"github.com/nekomeowww/insights-bot/internal/configs"
"github.com/redis/rueidis"
"go.uber.org/fx"
)
type NewRedisParams struct {
fx.In
Configs *configs.Config
}
type Redis struct {
rueidis.Client
}
func NewRedis() func(NewRedisParams) (*Redis, error) {
return func(params NewRedisParams) (*Redis, error) {
var tlsConfig *tls.Config
if params.Configs.Redis.TLSEnabled {
tlsConfig = &tls.Config{
MinVersion: tls.VersionTLS12,
}
}
client, err := rueidis.NewClient(rueidis.ClientOption{
TLSConfig: tlsConfig,
Username: params.Configs.Redis.Username,
Password: params.Configs.Redis.Password,
InitAddress: []string{net.JoinHostPort(params.Configs.Redis.Host, params.Configs.Redis.Port)},
SelectDB: int(params.Configs.Redis.DB),
DisableCache: !params.Configs.Redis.ClientCacheEnabled,
})
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
err = client.Do(ctx, client.B().Ping().Build()).Error()
if err != nil {
return nil, err
}
return &Redis{Client: client}, nil
}
}