DATAREDIS-784 - Add support for reactive increment and decrement commands to ReactiveValueOperations.
We now support reactive execution of the following increment and decrement commands through ReactiveValueOperations: - INCR - INCRBY - INCRBYFLOAT - DECR - DECRBY Original pull request: #322.
This commit is contained in:
committed by
Mark Paluch
parent
452feb9420
commit
8857f96b9f
@@ -273,6 +273,61 @@ class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K,
|
||||
|
||||
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) {
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -384,4 +384,60 @@ public class DefaultReactiveValueOperationsIntegrationTests<K, V> {
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user