Skip to content

Commit

Permalink
DATAREDIS-784 - Add support for reactive increment and decrement comm…
Browse files Browse the repository at this point in the history
…ands to ReactiveValueOperations.

We now support reactive execution of the following increment and decrement commands through ReactiveValueOperations:

- INCR
- INCRBY
- INCRBYFLOAT
- DECR
- DECRBY

Original pull request: spring-projects#322.
  • Loading branch information
SystemOutPrint authored and mp911de committed Mar 14, 2018
1 parent 452feb9 commit 8857f96
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,61 @@ public Mono<Boolean> delete(K key) {

return template.createMono(connection -> connection.keyCommands().del(rawKey(key))).map(l -> l != 0);
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#increment(java.lang.Object)
*/
@Override
public Mono<Long> increment(K key) {

Assert.notNull(key, "Key must not be null!");

return incrementBy(key, 1L);
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#incrementBy(java.lang.Object, long)
*/
@Override
public Mono<Long> incrementBy(K key, long delta) {

Assert.notNull(key, "Key must not be null!");

return template.createMono(connection -> connection.numberCommands().incrBy(rawKey(key), delta));
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#incrementBy(java.lang.Object, double)
*/
@Override
public Mono<Double> incrementBy(K key, double delta) {

Assert.notNull(key, "Key must not be null!");

return template.createMono(connection -> connection.numberCommands().incrBy(rawKey(key), delta));
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#decrement(java.lang.Object)
*/
@Override
public Mono<Long> decrement(K key) {

Assert.notNull(key, "Key must not be null!");

return incrementBy(key, -1L);
}

/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#decrementBy(java.lang.Object, long)
*/
@Override
public Mono<Long> decrementBy(K key, long delta) {

Assert.notNull(key, "Key must not be null!");

return incrementBy(key, -delta);
}

private <T> Mono<T> createMono(Function<ReactiveStringCommands, Publisher<T>> function) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,44 @@ public interface ReactiveValueOperations<K, V> {
* @param key must not be {@literal null}.
*/
Mono<Boolean> delete(K key);

/**
* Increments the number stored at {@code key} by one.
*
* @param key must not be {@literal null}.
* @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a>
*/
Mono<Long> increment(K key);

/**
* Increments the number stored at {@code key} by {@code delta}.
* @param key must not be {@literal null}.
* @param delta
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
*/
Mono<Long> incrementBy(K key, long delta);

/**
* Increment the string representing a floating point number stored at {@code key} by {@code delta}.
* @param key must not be {@literal null}.
* @param delta
* @see <a href="http://redis.io/commands/incrbyfloat">Redis Documentation: INCRBYFLOAT</a>
*/
Mono<Double> incrementBy(K key, double delta);

/**
* Decrements the number stored at {@code key} by one.
*
* @param key must not be {@literal null}.
* @see <a href="http://redis.io/commands/decr">Redis Documentation: DECR</a>
*/
Mono<Long> decrement(K key);

/**
* Decrements the number stored at {@code key} by {@code delta}.
* @param key must not be {@literal null}.
* @param delta
* @see <a href="http://redis.io/commands/decrby">Redis Documentation: DECRBY</a>
*/
Mono<Long> decrementBy(K key, long delta);
}
Original file line number Diff line number Diff line change
Expand Up @@ -384,4 +384,60 @@ public void delete() {

StepVerifier.create(valueOperations.size(key)).expectNext(0L).verifyComplete();
}

@Test // DATAREDIS-784
public void increment() {

K key = keyFactory.instance();

StepVerifier.create(valueOperations.increment(key)).expectNext(1L).verifyComplete();

StepVerifier.create(valueOperations.increment(key)).expectNext(2L).verifyComplete();
}

@Test // DATAREDIS-784
public void incrementByLongDelta() {

K key = keyFactory.instance();

StepVerifier.create(valueOperations.incrementBy(key, 2L)).expectNext(2L).verifyComplete();

StepVerifier.create(valueOperations.incrementBy(key, -3L)).expectNext(-1L).verifyComplete();

StepVerifier.create(valueOperations.incrementBy(key, 1L)).expectNext(0L).verifyComplete();
}

@Test // DATAREDIS-784
public void incrementByFloatDelta() {

K key = keyFactory.instance();

StepVerifier.create(valueOperations.incrementBy(key, 0.1)).expectNext(0.1).verifyComplete();

StepVerifier.create(valueOperations.incrementBy(key, -0.3)).expectNext(-0.2).verifyComplete();

StepVerifier.create(valueOperations.incrementBy(key, 0.2)).expectNext(0.0).verifyComplete();
}

@Test // DATAREDIS-784
public void decrement() {

K key = keyFactory.instance();

StepVerifier.create(valueOperations.decrement(key)).expectNext(-1L).verifyComplete();

StepVerifier.create(valueOperations.decrement(key)).expectNext(-2L).verifyComplete();
}

@Test // DATAREDIS-784
public void decrementByLongDelta() {

K key = keyFactory.instance();

StepVerifier.create(valueOperations.decrementBy(key, 2L)).expectNext(-2L).verifyComplete();

StepVerifier.create(valueOperations.decrementBy(key, -3L)).expectNext(1L).verifyComplete();

StepVerifier.create(valueOperations.decrementBy(key, 1L)).expectNext(0L).verifyComplete();
}
}

0 comments on commit 8857f96

Please sign in to comment.