优化 redis 配置方式
This commit is contained in:
@@ -40,7 +40,7 @@ func NewAutoConfig(src string) (KVConfig, error) {
|
|||||||
if pass == "" {
|
if pass == "" {
|
||||||
pass = query.Get("password")
|
pass = query.Get("password")
|
||||||
}
|
}
|
||||||
db := query.Get("db")
|
db := strings.TrimPrefix(parse.Path, "/")
|
||||||
if db == "" {
|
if db == "" {
|
||||||
db = "0"
|
db = "0"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"go.uber.org/zap"
|
||||||
|
|
||||||
"github.com/redis/go-redis/v9"
|
"github.com/redis/go-redis/v9"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -17,13 +19,20 @@ func NewConfigRedis(ctx context.Context, addr string, password string, db int) (
|
|||||||
if addr == "" {
|
if addr == "" {
|
||||||
return nil, fmt.Errorf("addr is empty")
|
return nil, fmt.Errorf("addr is empty")
|
||||||
}
|
}
|
||||||
|
zap.L().Debug("connect redis", zap.String("addr", addr), zap.Int("db", db))
|
||||||
|
client := redis.NewClient(&redis.Options{
|
||||||
|
Addr: addr,
|
||||||
|
Password: password,
|
||||||
|
DB: db,
|
||||||
|
})
|
||||||
|
_, err := client.Ping(ctx).Result()
|
||||||
|
if err != nil {
|
||||||
|
_ = client.Close()
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
return &ConfigRedis{
|
return &ConfigRedis{
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
client: redis.NewClient(&redis.Options{
|
client: client,
|
||||||
Addr: addr,
|
|
||||||
Password: password,
|
|
||||||
DB: db,
|
|
||||||
}),
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user