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

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

import java.util.Set;

/**
 * @author reghao
 * @date 2022-09-04 00:11:16
 */
@Component
public class RedisSortedSet {
    private final ZSetOperations<String, String> zsetOps;

    public RedisSortedSet(RedisTemplate<String, String> redisTemplate) {
        this.zsetOps = redisTemplate.opsForZSet();
    }

    public void add(String key, String object, long score) {
        zsetOps.add(key, object, score);
    }

    public long count(String key) {
        Long total = zsetOps.count(key, 0, System.currentTimeMillis());
        return total != null ? total : 0;
    }

    public Set<String> get(String key, long start, long end) {
        return zsetOps.range(key, start, end);
    }

    public Set<String> zrevrange(String key, long start, long end) {
        return zsetOps.reverseRange(key, start, end);
    }
}
