package utils

import (
	"WorldEpcho/src/config"
	"github.com/go-redis/redis"
	"log"
	"strconv"
	"time"
)

type RedisStore struct {
	client *redis.Client
}

var RediStore RedisStore

func InitRedisStore() *RedisStore {
	config := config.Conf
	Redis_DB, _ := strconv.Atoi(config.RedisDB)

	client := redis.NewClient(&redis.Options{
		Addr:     config.RedisServer + ":" + config.RedisPort,
		Password: config.RedisPwd,
		DB:       Redis_DB,
	})

	RediStore = RedisStore{client: client}

	//base64Captcha.SetCustomStore(&RediStore)

	return &RediStore
}

// set
func (rs *RedisStore) Set(id string, value string) {
	err := rs.client.Set(id, value, time.Minute*10).Err()
	if err != nil {
		log.Println(err)
	}
}

// get
func (rs *RedisStore) Get(id string, clear bool) string {
	val, err := rs.client.Get(id).Result()
	if err != nil {
		log.Println(err)
		return ""
	}
	if clear {
		err := rs.client.Del(id).Err()
		if err != nil {
			log.Println(err)
			return ""
		}
	}
	return val
}
