package cn.reghao.tnb.content.app.util.redis.ds;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Set;

/**
 * Redis set 操作
 *
 * @author reghao
 * @date 2021-03-07 17:40:50
 */
@Component
public class RedisSet {
    private final SetOperations<String, Object> setOps;

    public RedisSet(RedisTemplate<String, Object> redisTemplate) {
        this.setOps = redisTemplate.opsForSet();
    }

    public void sadd(String key, Object value) {
        setOps.add(key, value);
    }

    public void smove(String srcKey, Object value, String dstKey) {
        setOps.move(srcKey, value, dstKey);
    }

    public void sadd(String key, Object... values) {
        setOps.add(key, values);
    }

    public Object spop(String key) {
        return setOps.pop(key);
    }

    public List<Object> spop(String key, long count) {
        return setOps.pop(key, count);
    }

    public Set<Object> smembers(String key) {
        return setOps.members(key);
    }

    public boolean sismember(String key, Object value) {
        Boolean ret = setOps.isMember(key, value);
        return ret != null && ret;
    }

    public Set<Object> sunion(List<String> keys) {
        return setOps.union(keys);
    }

    public long scard(String key) {
        Long size = setOps.size(key);
        return size == null ? 0 : size;
    }

    public void remove(String key, Object value) {
        setOps.remove(key, value);
    }
}
