package app

import (
	"context"
	"github.com/go-redis/redis/v8"
	"gofiber.study.skcks.cn/common/config"
	"gofiber.study.skcks.cn/common/logger"
	"gofiber.study.skcks.cn/common/utils"
	"gofiber.study.skcks.cn/global"
)

func reloadRedis(c *config.BasicConfig) {
	if global.Redis != nil {
		_ = global.Redis.Close()
	}

	r := redis.NewClient(&redis.Options{
		Addr:     c.Redis.Addr,
		Password: c.Redis.Pass,
		DB:       c.Redis.DB,
	})

	ctx := context.Background()
	_, err := r.Ping(ctx).Result()
	if err != nil {
		logger.Log.Fatalln("[x] [Redis] 连接失败: %s", err)
	}

	utils.MainAppExec(func() {
		logger.Log.Infoln("[√] [Redis] 连接成功")
	})

	global.Redis = r
}
