package com.wechat.global.base.dao;

import java.io.Serializable;
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: 2020-04-21 11:36
 */
@Repository
public class RedisSetDao<V extends Serializable> {

	@Autowired
	protected RedisTemplate<String, V> redisTemplate;
	

	public Boolean isMember(String infoKey, V infoValue) {
		return redisTemplate.opsForSet().isMember(infoKey, infoValue);
	}

	public Long insert(String infoKey, V infoValue) {
		Long add = redisTemplate.opsForSet().add(infoKey, infoValue);
		return add;
	}

	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiry 过期时长（X秒后过期） 单位：秒
	 */
	public Long insertSecond(String infoKey, V infoValue, Integer expirySecond) {
		Long add = redisTemplate.opsForSet().add(infoKey, infoValue);
		// 设置失效时间
		redisTemplate.expire(infoKey, expirySecond, TimeUnit.SECONDS);
		return add;
	}

	/**
	 * 
	 * @param infoKey
	 * @param infoValue
	 * @param expiry 过期时长 单位：天
	 * @return 
	 */
	public Long insertDay(String infoKey, V infoValue, Integer expiryDay) {
		Long add = redisTemplate.opsForSet().add(infoKey, infoValue);
		// 设置失效时间
		redisTemplate.expire(infoKey, expiryDay, TimeUnit.DAYS);
		return add;
	}
	

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

    public Long getSize(String infoKey) {
        return redisTemplate.opsForSet().size(infoKey);
     }
    
}
