// rediscluster
package spiderutil

import (
	"fmt"
	"log"

	"github.com/jettyu/goredis"
)

var clu goredis.RedisCluster

func InitRedisCluster(redishosts []string, max_idle, max_active int, debug bool) {
	clu = goredis.NewRedisCluster(redishosts, max_idle, max_active, debug)
}

func GetStr(key string) string {
	var result string
	ret, err := clu.Do("GET", key)
	if err == nil {
		if r, ok := ret.([]byte); ok {
			result = string(r)
		}
	} else {
		log.Println(err.Error())
	}
	return result
}

func Put(key string, val interface{}) bool {
	b := false
	_, err := clu.Do("SET", key, val)
	if err == nil {
		b = true
	} else {
		log.Println(err.Error())
	}
	return b
}

func Del(key string) bool {
	b := false
	_, err := clu.Do("DEL", key)
	if err == nil {
		b = true
	} else {
		log.Println(err.Error())
	}
	return b
}

func IsExit(key string) bool {
	repl, err := clu.Do("exists", key)
	if err == nil {
		return fmt.Sprint(repl) == "1"
	} else {
		return false
	}
}
