package redis

import (
	"context"
	"fmt"
	"github.com/redis/go-redis/v9"
)

var (
	cli *redis.Client
	Nil = redis.Nil
)

func Init(host string, port int, password string, db int) error {
	cli = redis.NewClient(&redis.Options{
		Addr:     fmt.Sprintf("%s:%d", host, port),
		Password: password,
		DB:       db,
	})

	_, err := cli.Ping(context.Background()).Result()
	return err
}

func Client() *redis.Client {
	return cli
}

func Close() error {
	if cli != nil {
		return cli.Close()
	}
	return nil
}
