package cache

import (
	"time"
)

func (p *RedisCache) Keys(key string) ([]string, error) {
	if p.isCluster {
		return p.cluster.Keys(key).Result()
	}

	return p.client.Keys(key).Result()
}

func (p *RedisCache) Expire(key string, seconds int64) (bool, error) {
	if p.isCluster {
		return p.cluster.Expire(key, time.Duration(seconds)*time.Second).Result()
	}

	return p.client.Expire(key, time.Duration(seconds)*time.Second).Result()
}

func (p *RedisCache) Exists(keys ...string) (int64, error) {
	if p.isCluster {
		n := int64(0)
		for _, key := range keys {
			v, err := p.cluster.Exists(key).Result()
			if err != nil {
				return n, err
			}

			n += v
		}

		return n, nil
	}

	return p.client.Exists(keys...).Result()
}

func (p *RedisCache) Del(keys ...string) (int64, error) {
	if p.isCluster {
		n := int64(0)
		for _, key := range keys {
			v, err := p.cluster.Del(key).Result()
			if err != nil {
				return n, err
			}

			n += v
		}

		return n, nil
	}

	return p.client.Del(keys...).Result()
}

func (p *RedisCache) IncrBy(key string, value int64) (int64, error) {
	if p.isCluster {
		return p.cluster.IncrBy(key, value).Result()
	}

	return p.client.IncrBy(key, value).Result()
}

func (p *RedisCache) Rename(key string, newkey string) (string, error) {
	if p.isCluster {
		return p.cluster.Rename(key, newkey).Result()
	}

	return p.client.Rename(key, newkey).Result()
}
