forked from ccfos/nightingale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis.go
137 lines (117 loc) · 2.81 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
package storage
import (
"context"
"errors"
"fmt"
"os"
"strings"
"github.com/ccfos/nightingale/v6/pkg/tlsx"
"github.com/redis/go-redis/v9"
"github.com/toolkits/pkg/logger"
)
type RedisConfig struct {
Address string
Username string
Password string
DB int
UseTLS bool
tlsx.ClientConfig
RedisType string
MasterName string
SentinelUsername string
SentinelPassword string
}
type Redis redis.Cmdable
func NewRedis(cfg RedisConfig) (Redis, error) {
var redisClient Redis
switch cfg.RedisType {
case "standalone", "":
redisOptions := &redis.Options{
Addr: cfg.Address,
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
}
if cfg.UseTLS {
tlsConfig, err := cfg.TLSConfig()
if err != nil {
fmt.Println("failed to init redis tls config:", err)
os.Exit(1)
}
redisOptions.TLSConfig = tlsConfig
}
redisClient = redis.NewClient(redisOptions)
case "cluster":
redisOptions := &redis.ClusterOptions{
Addrs: strings.Split(cfg.Address, ","),
Username: cfg.Username,
Password: cfg.Password,
}
if cfg.UseTLS {
tlsConfig, err := cfg.TLSConfig()
if err != nil {
fmt.Println("failed to init redis tls config:", err)
os.Exit(1)
}
redisOptions.TLSConfig = tlsConfig
}
redisClient = redis.NewClusterClient(redisOptions)
case "sentinel":
redisOptions := &redis.FailoverOptions{
MasterName: cfg.MasterName,
SentinelAddrs: strings.Split(cfg.Address, ","),
Username: cfg.Username,
Password: cfg.Password,
DB: cfg.DB,
SentinelUsername: cfg.SentinelUsername,
SentinelPassword: cfg.SentinelPassword,
}
if cfg.UseTLS {
tlsConfig, err := cfg.TLSConfig()
if err != nil {
fmt.Println("failed to init redis tls config:", err)
os.Exit(1)
}
redisOptions.TLSConfig = tlsConfig
}
redisClient = redis.NewFailoverClient(redisOptions)
default:
fmt.Println("failed to init redis , redis type is illegal:", cfg.RedisType)
os.Exit(1)
}
err := redisClient.Ping(context.Background()).Err()
if err != nil {
fmt.Println("failed to ping redis:", err)
os.Exit(1)
}
return redisClient, nil
}
func MGet(ctx context.Context, r Redis, keys []string) [][]byte {
var vals [][]byte
pipe := r.Pipeline()
for _, key := range keys {
pipe.Get(ctx, key)
}
cmds, _ := pipe.Exec(ctx)
for i, key := range keys {
cmd := cmds[i]
if errors.Is(cmd.Err(), redis.Nil) {
continue
}
if cmd.Err() != nil {
logger.Errorf("failed to get key: %s, err: %s", key, cmd.Err())
continue
}
val := []byte(cmd.(*redis.StringCmd).Val())
vals = append(vals, val)
}
return vals
}
func MSet(ctx context.Context, r Redis, m map[string]interface{}) error {
pipe := r.Pipeline()
for k, v := range m {
pipe.Set(ctx, k, v, 0)
}
_, err := pipe.Exec(ctx)
return err
}