Skip to content

Commit c61b24b

Browse files
Add incrementScore to RedisZSet.
1 parent d8bdb54 commit c61b24b

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/main/java/org/springframework/data/redis/support/collections/DefaultRedisZSet.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.data.redis.core.RedisOperations;
3131
import org.springframework.data.redis.core.ScanOptions;
3232
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
33+
import org.springframework.util.Assert;
3334

3435
/**
3536
* Default implementation for {@link RedisZSet}. Note that the collection support works only with normal,
@@ -322,6 +323,13 @@ public boolean addIfAbsent(E e, double score) {
322323
return result;
323324
}
324325

326+
@Override
327+
public Double incrementScore(E value, Number delta) {
328+
329+
Assert.notNull(delta, "Delta must not be null");
330+
return boundZSetOps.incrementScore(value, delta.doubleValue());
331+
}
332+
325333
@Override
326334
public void clear() {
327335
boundZSetOps.removeRange(0, -1);

src/main/java/org/springframework/data/redis/support/collections/RedisZSet.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.redis.connection.zset.Tuple;
2929
import org.springframework.data.redis.core.BoundZSetOperations;
3030
import org.springframework.data.redis.core.RedisOperations;
31+
import org.springframework.data.redis.core.ZSetOperations;
3132
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
3233

3334
/**
@@ -564,6 +565,19 @@ default boolean addIfAbsent(E e) {
564565
*/
565566
Double score(Object o);
566567

568+
/**
569+
* Increment the score of element with {@code value} in sorted set by {@code delta}.
570+
*
571+
* @param value the value.
572+
* @param delta the delta to add. Can be negative. Must not be {@literal null}.
573+
* @return the new score after increment, or {@literal null} when used in pipeline / transaction.
574+
* @throws IllegalArgumentException if delta is {@literal null}.
575+
* @see ZSetOperations#incrementScore(Object, Object, double)
576+
* @see <a href="https://redis.io/commands/zincrby">Redis Documentation: ZINCRBY</a>
577+
* @since 4.1
578+
*/
579+
Double incrementScore(E value, Number delta);
580+
567581
/**
568582
* Returns the rank (position) of the given element in the set, in ascending order. Returns null if the element is not
569583
* contained by the set.

src/test/java/org/springframework/data/redis/support/collections/AbstractRedisZSetTestIntegration.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,28 @@ void testScore() {
297297
assertThat(zSet.score(t3)).isEqualTo(Double.valueOf(5));
298298
}
299299

300+
@Test // GH-3256
301+
void testIncrementScore() {
302+
303+
T value1 = getT();
304+
T value2 = getT();
305+
306+
// Add element with initial score
307+
zSet.add(value1, 2.5);
308+
309+
// Increment score
310+
assertThat(zSet.incrementScore(value1, 3.2)).isEqualTo(Double.valueOf(5.7));
311+
assertThat(zSet.score(value1)).isEqualTo(Double.valueOf(5.7));
312+
313+
// Test with negative delta (decrement)
314+
assertThat(zSet.incrementScore(value1, -1.5)).isEqualTo(Double.valueOf(4.2));
315+
assertThat(zSet.score(value1)).isEqualTo(Double.valueOf(4.2));
316+
317+
// Test incrementing non-existent element (should create it with delta as score)
318+
assertThat(zSet.incrementScore(value2, 10.0)).isEqualTo(Double.valueOf(10.0));
319+
assertThat(zSet.score(value2)).isEqualTo(Double.valueOf(10.0));
320+
}
321+
300322
@Test
301323
void testDefaultScore() {
302324
assertThat(zSet.getDefaultScore()).isCloseTo(1, Offset.offset(0d));

0 commit comments

Comments
 (0)