package store import ( "context" "fmt" "time" "github.com/redis/go-redis/v9" ) // NewValkey 建立 Valkey/Redis 客户端。 // Valkey 与 Redis 协议完全兼容,go-redis 无需区分。 func NewValkey(ctx context.Context, addr, password string, db int) (*redis.Client, error) { cli := redis.NewClient(&redis.Options{ Addr: addr, Password: password, DB: db, PoolSize: 100, MinIdleConns: 10, DialTimeout: 3 * time.Second, ReadTimeout: 2 * time.Second, WriteTimeout: 2 * time.Second, PoolTimeout: 3 * time.Second, MaxRetries: 2, }) pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) defer cancel() if err := cli.Ping(pingCtx).Err(); err != nil { _ = cli.Close() return nil, fmt.Errorf("连接 Valkey 失败: %w", err) } return cli, nil }