package com.wechat.global.base.dao;

import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;


/**
 * @author jadyn.wu
 * @date: 2019-12-26 17:31
 */
@Repository
public class RedisZSetDao {

	@Autowired
	protected RedisTemplate<String, Object> redisTemplate;

/*	public String select(String infoKey) {
		return redisTemplate.opsForValue().get(infoKey);
	}
*/
	public void insert(String infoKey, Object infoValue, double score) {
		redisTemplate.opsForZSet().add(infoKey, infoValue, score);
	}

	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiry 过期时长（X秒后过期） 单位：秒
	 */
	public void insert(String infoKey, String infoValue, Integer expirySecond) {
		redisTemplate.opsForValue().set(infoKey, infoValue);
		// 设置失效时间
		redisTemplate.expire(infoKey, expirySecond, TimeUnit.SECONDS);
	}

	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiry 过期时长 单位：天
	 */
	public void insertDay(String infoKey, String infoValue, Integer expiryDay) {
		redisTemplate.opsForValue().set(infoKey, infoValue);
		// 设置失效时间
		redisTemplate.expire(infoKey, expiryDay, TimeUnit.DAYS);
	}
	
	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiry 过期时长 单位：毫秒
	 */
   public void insertExpMs(String infoKey, String infoValue, long expiryMilliseSecond) {
        redisTemplate.opsForValue().set(infoKey, infoValue);
        // 设置失效时间
        redisTemplate.expire(infoKey, expiryMilliseSecond, TimeUnit.MILLISECONDS);
    }
   
	
	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiryTime 具体的过期时间（到X时间就过期）
	 */
	public void insert(String infoKey, String infoValue, Date expiryTime) {
		redisTemplate.opsForValue().set(infoKey, infoValue);
		// 设置失效时间
		redisTemplate.expireAt(infoKey, expiryTime);
	}


	public void delete(String infoKey) {
		redisTemplate.delete(infoKey);
	}
	
   public Boolean hasKey(String infoKey) {
       return redisTemplate.hasKey(infoKey);
    }

}
