package com.yxcheng.token.common.cache;


import com.yxcheng.token.utils.SerializeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * RedisService不支持泛型，因此添加了一个新类支持泛型
 * @author breeze
 *
 * @param <K>
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Service
public class RedisServiceGeneric<K,V> {

	private Logger LOG = LoggerFactory.getLogger(RedisServiceGeneric.class);

	@Autowired
	private List<RedisTemplate<K,V>> redisTemplateList;
	
	@Autowired
	private RedisTemplate<K,V> redisTemplate;
	
	
	/**
	 * 获取有序集合的大小
	 * @param key
	 * @return
	 */
	public Long zkSize(K key){
		return  redisTemplate.opsForZSet().size(key);
	}
	
	
	/**
	 * zk移除元素
	 * @param key
	 * @param start
	 * @param end
	 */
	public Long zkRemoveRange(K key, long start, long end){
		return redisTemplate.opsForZSet().removeRange(key, start, end);
	}
	
	
	/**
	 * zk插入元素
	 * @param key
	 * @param value
	 * @param score
	 * @return
	 */
	public Boolean zkAdd(K key, V value, double score){
		return redisTemplate.opsForZSet().add(key, value, score);
	}
	
	
	/**
	 * zk获取元素
	 * @param key
	 * @param start
	 * @param end
	 * @return
	 */
	public Set<V> zkRange(K key, long start, long end){
		return redisTemplate.opsForZSet().range(key, start, end);
	}
	
	/**
	 * 判断是否存在key
	 * @param key
	 * @return
	 */
	public Boolean hasKey(K key){
		return redisTemplate.hasKey(key);
	}
		
}
