diff --git a/pkg/utils/config.go b/pkg/utils/config.go index 5c2cfae..b989221 100644 --- a/pkg/utils/config.go +++ b/pkg/utils/config.go @@ -40,7 +40,7 @@ func NewAutoConfig(src string) (KVConfig, error) { if pass == "" { pass = query.Get("password") } - db := query.Get("db") + db := strings.TrimPrefix(parse.Path, "/") if db == "" { db = "0" } diff --git a/pkg/utils/config_redis.go b/pkg/utils/config_redis.go index 78baee1..b20399a 100644 --- a/pkg/utils/config_redis.go +++ b/pkg/utils/config_redis.go @@ -5,6 +5,8 @@ import ( "fmt" "time" + "go.uber.org/zap" + "github.com/redis/go-redis/v9" ) @@ -17,13 +19,20 @@ func NewConfigRedis(ctx context.Context, addr string, password string, db int) ( if addr == "" { 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{ - ctx: ctx, - client: redis.NewClient(&redis.Options{ - Addr: addr, - Password: password, - DB: db, - }), + ctx: ctx, + client: client, }, nil }