Skip to content

Hopefully the rangeByScoreWithScores method of ZSetOperations supports Range and Limit parameters #3139

Closed as not planned
@luozongle01

Description

@luozongle01

Hi, I encountered a problem at work. I want to calculate the difference from my previous or next score. I need to filter out the situations where I have the same score.

Since the rangeByScoreWithScores method in ZSetOperations currently doesn't have Range and Limit parameters, this is how I currently implement it.

public Double getDifferenceWithPreviousRank(String key, Long userId) {
    Double score = redisTemplate.opsForZSet().score(key, userId.toString());

    if (score == null) {
        return 0D;
    }

    RedisConnection connection = redisTemplate.getRequiredConnectionFactory().getConnection();
    byte[] serializeKey = redisTemplate.getStringSerializer().serialize(key);
    Objects.requireNonNull(serializeKey);

    Set<Tuple> resultSet = connection.zSetCommands()
            .zRangeByScoreWithScores(serializeKey, Range.rightUnbounded(Range.Bound.exclusive(score)),
                    new Limit().offset(0).count(1));

    Tuple result = CollectionUtil.getFirst(resultSet);
    if (result == null || result.getScore() == null) {
        return 0D;
    }

    return result.getScore() - score;
}

So I would like to ask if it is possible to add a Range and Limit parameter to rangeByScoreWithScores, after all, this is more convenient to use.

If possible, I would be happy to contribute a PR.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions