package redis

import (
	"fmt"
	"gitlab.fjmaimaimai.com/allied-creation/allied-creation-manufacture/pkg/constant"

	"github.com/go-redis/redis"
)

var clientRedis *redis.Client

func InitRedis() *redis.Client {
	options := redis.Options{
		Network:            "tcp",
		Addr:               fmt.Sprintf("%s:%s", constant.REDIS_HOST, constant.REDIS_PORT),
		Dialer:             nil,
		OnConnect:          nil,
		Password:           "",
		DB:                 0,
		MaxRetries:         0,
		MinRetryBackoff:    0,
		MaxRetryBackoff:    0,
		DialTimeout:        0,
		ReadTimeout:        0,
		WriteTimeout:       0,
		PoolSize:           0,
		MinIdleConns:       0,
		MaxConnAge:         0,
		PoolTimeout:        0,
		IdleTimeout:        0,
		IdleCheckFrequency: 0,
		TLSConfig:          nil,
	}
	// 新建一个client
	clientRedis = redis.NewClient(&options)
	pong, err := clientRedis.Ping().Result()
	if err != nil {
		e := fmt.Errorf("redis 连接失败,%s,%w", pong, err)
		panic(e)
	}
	return clientRedis
}

func GetRedis() *redis.Client {
	return clientRedis
}