Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
luozongle01 opened this issue Apr 27, 2025 · 2 comments
Assignees
Labels
status: duplicate A duplicate of another issue

Comments

@luozongle01
Copy link

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.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Apr 27, 2025
@mp911de mp911de self-assigned this Apr 28, 2025
@mp911de
Copy link
Member

mp911de commented Apr 28, 2025

See also #796 and #938

@mp911de mp911de added status: duplicate A duplicate of another issue and removed status: waiting-for-triage An issue we've not yet triaged labels May 5, 2025
@mp911de
Copy link
Member

mp911de commented May 5, 2025

This is a duplicate of the older #1034 ticket.

@mp911de mp911de closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2025
kssumin added a commit to kssumin/spring-data-redis that referenced this issue May 8, 2025
This commit adds support for the Range and Limit parameters to the
rangeByScoreWithScores and reverseRangeByScoreWithScores methods
in ZSetOperations interface.

The implementation follows the same pattern already used for other
methods like rangeByLex, providing a more flexible and consistent API
for working with Redis sorted sets.

New methods:
- rangeByScoreWithScores(K key, Range<Double> range)
- rangeByScoreWithScores(K key, Range<Double> range, Limit limit)
- reverseRangeByScoreWithScores(K key, Range<Double> range)
- reverseRangeByScoreWithScores(K key, Range<Double> range, Limit limit)

Unit and integration tests added to verify functionality.

Fixes spring-projects#3139
Related to spring-projects#796 and spring-projects#938

Signed-off-by: [Your Full Name] <[email protected]>

Signed-off-by: kssumin <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: duplicate A duplicate of another issue
Projects
None yet
Development

No branches or pull requests

3 participants