Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>4.0.0-SNAPSHOT</version>
<version>4.0.0-GH-3256-SNAPSHOT</version>

<name>Spring Data Redis</name>
<description>Spring Data module for Redis</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ScanOptions;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;
import org.springframework.util.Assert;

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

@Override
public Double incrementScore(E value, Number delta) {

Assert.notNull(delta, "Delta must not be null");
return boundZSetOps.incrementScore(value, delta.doubleValue());
}

@Override
public void clear() {
boundZSetOps.removeRange(0, -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.springframework.data.redis.connection.zset.Tuple;
import org.springframework.data.redis.core.BoundZSetOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.data.redis.core.ZSetOperations.TypedTuple;

/**
Expand Down Expand Up @@ -564,6 +565,19 @@ default boolean addIfAbsent(E e) {
*/
Double score(Object o);

/**
* Increment the score of element with {@code value} in sorted set by {@code delta}.
*
* @param value the value.
* @param delta the delta to add. Can be negative. Must not be {@literal null}.
* @return the new score after increment, or {@literal null} when used in pipeline / transaction.
* @throws IllegalArgumentException if delta is {@literal null}.
* @see ZSetOperations#incrementScore(Object, Object, double)
* @see <a href="https://redis.io/commands/zincrby">Redis Documentation: ZINCRBY</a>
* @since 4.1
*/
Double incrementScore(E value, Number delta);

/**
* Returns the rank (position) of the given element in the set, in ascending order. Returns null if the element is not
* contained by the set.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,28 @@ void testScore() {
assertThat(zSet.score(t3)).isEqualTo(Double.valueOf(5));
}

@Test // GH-3256
void testIncrementScore() {

T value1 = getT();
T value2 = getT();

// Add element with initial score
zSet.add(value1, 2.5);

// Increment score
assertThat(zSet.incrementScore(value1, 3.2)).isEqualTo(Double.valueOf(5.7));
assertThat(zSet.score(value1)).isEqualTo(Double.valueOf(5.7));

// Test with negative delta (decrement)
assertThat(zSet.incrementScore(value1, -1.5)).isEqualTo(Double.valueOf(4.2));
assertThat(zSet.score(value1)).isEqualTo(Double.valueOf(4.2));

// Test incrementing non-existent element (should create it with delta as score)
assertThat(zSet.incrementScore(value2, 10.0)).isEqualTo(Double.valueOf(10.0));
assertThat(zSet.score(value2)).isEqualTo(Double.valueOf(10.0));
}

@Test
void testDefaultScore() {
assertThat(zSet.getDefaultScore()).isCloseTo(1, Offset.offset(0d));
Expand Down