Skip to content

Commit 4ca455f

Browse files
优化 redis 配置方式
1 parent 93a26f1 commit 4ca455f

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

pkg/utils/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func NewAutoConfig(src string) (KVConfig, error) {
4040
if pass == "" {
4141
pass = query.Get("password")
4242
}
43-
db := query.Get("db")
43+
db := strings.TrimPrefix(parse.Path, "/")
4444
if db == "" {
4545
db = "0"
4646
}

pkg/utils/config_redis.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"time"
77

8+
"go.uber.org/zap"
9+
810
"github.com/redis/go-redis/v9"
911
)
1012

@@ -17,13 +19,20 @@ func NewConfigRedis(ctx context.Context, addr string, password string, db int) (
1719
if addr == "" {
1820
return nil, fmt.Errorf("addr is empty")
1921
}
22+
zap.L().Debug("connect redis", zap.String("addr", addr), zap.Int("db", db))
23+
client := redis.NewClient(&redis.Options{
24+
Addr: addr,
25+
Password: password,
26+
DB: db,
27+
})
28+
_, err := client.Ping(ctx).Result()
29+
if err != nil {
30+
_ = client.Close()
31+
return nil, err
32+
}
2033
return &ConfigRedis{
21-
ctx: ctx,
22-
client: redis.NewClient(&redis.Options{
23-
Addr: addr,
24-
Password: password,
25-
DB: db,
26-
}),
34+
ctx: ctx,
35+
client: client,
2736
}, nil
2837
}
2938

0 commit comments

Comments
 (0)