29 lines
517 B
Go
29 lines
517 B
Go
package redisx
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
type Config struct {
|
|
Host string `json:"host"`
|
|
Port int `json:"port"`
|
|
}
|
|
|
|
func Connect(ctx context.Context, cfg Config) (*redis.Client, error) {
|
|
client := redis.NewClient(&redis.Options{Addr: fmt.Sprintf("%s:%d", cfg.Host, defaultInt(cfg.Port, 6379))})
|
|
if err := client.Ping(ctx).Err(); err != nil {
|
|
return client, err
|
|
}
|
|
return client, nil
|
|
}
|
|
|
|
func defaultInt(v, fallback int) int {
|
|
if v == 0 {
|
|
return fallback
|
|
}
|
|
return v
|
|
}
|