-
Notifications
You must be signed in to change notification settings - Fork 0
/
redis.go
34 lines (28 loc) · 960 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
package config
import (
"github.com/go-redis/redis/v8"
"github.com/spf13/pflag"
redisPkg "github.com/hexiaopi/blog-service/pkg/redis"
)
var RedisEngine *redis.Client
type RedisConfig struct {
Addr string `mapstructure:"addr"`
UserName string `mapstructure:"username"`
PassWord string `mapstructure:"password"`
DB int `mapstructure:"db"`
}
func (o *RedisConfig) AddFlags(fs *pflag.FlagSet) {
pflag.StringVar(&o.Addr, "redis.addr", o.Addr, "Redis server addr")
pflag.StringVar(&o.UserName, "redis.username", o.UserName, "Redis server username")
pflag.StringVar(&o.PassWord, "redis.password", o.PassWord, "Redis server password")
pflag.IntVar(&o.DB, "redis.db", o.DB, "Redis server db")
}
func (o *RedisConfig) NewClient() (*redis.Client, error) {
return redisPkg.New(
redisPkg.WithAddr(o.Addr),
redisPkg.WithUserName(o.UserName),
redisPkg.WithPassWord(o.PassWord),
redisPkg.WithDB(o.DB),
redisPkg.WithTrace(Tracer),
)
}