'Spring Data Redis using '+inf' to specify unlimited bounds of ZSetOperations.rangeByScore
Using the Redis CLI you can query a sorted set by range with an unlimited upper bound:
zrangebyscore my_key 0 +inf
Represented by the +inf as well as an unlimited lower bound: -inf. This retrieves the range from 0 to the last member.
Spring Data Redis provides an interface to this Redis command through this method signature:
Set<V> rangeByScore(K key, double min, double max, long offset, long count);
and
Set<V> rangeByScore(K key, double min, double max);
These only allow you to supply a double as the bounds.
How do you specify +inf or -inf to the bounds of ZRANGEBYSCORE using Spring Data Redis ?
The only mention of it is in the changelog so it's supported in some way but I can't find any mention of it elsewhere.
Solution 1:[1]
Maybe you could use Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY to represent -inf and +inf, I've tested on my machine, which works well(with spring-boot-starter-data-redis version=2.2.4.RELEASE)
(They are both double values)
Solution 2:[2]
In more recent versions you can specify the range with Bound.unbounded().
Flux<V> rangeByScore(K key, Range<Double> range, Limit limit)
For example:
Range.of(Bound.inclusive(start), Bound.unbounded())
See Range class for other factory methods like leftUnbounded / rightUnbounded.
I used this with a redis version below 6.2. With 6.2+ the zrange semantics changed.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nguy?n V?n Phong |
| Solution 2 | mio |
