diff --git a/src/main/java/org/springframework/data/redis/core/DefaultReactiveValueOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultReactiveValueOperations.java index 9fcc84fac..a50a247fc 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultReactiveValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultReactiveValueOperations.java @@ -273,6 +273,61 @@ class DefaultReactiveValueOperations implements ReactiveValueOperations 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 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 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 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 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 decrementBy(K key, long delta) { + + Assert.notNull(key, "Key must not be null!"); + + return incrementBy(key, -delta); + } private Mono createMono(Function> function) { diff --git a/src/main/java/org/springframework/data/redis/core/ReactiveValueOperations.java b/src/main/java/org/springframework/data/redis/core/ReactiveValueOperations.java index a33653e42..416d12d3e 100644 --- a/src/main/java/org/springframework/data/redis/core/ReactiveValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/ReactiveValueOperations.java @@ -193,4 +193,44 @@ public interface ReactiveValueOperations { * @param key must not be {@literal null}. */ Mono delete(K key); + + /** + * Increments the number stored at {@code key} by one. + * + * @param key must not be {@literal null}. + * @see Redis Documentation: INCR + */ + Mono increment(K key); + + /** + * Increments the number stored at {@code key} by {@code delta}. + * @param key must not be {@literal null}. + * @param delta + * @see Redis Documentation: INCRBY + */ + Mono 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 Redis Documentation: INCRBYFLOAT + */ + Mono incrementBy(K key, double delta); + + /** + * Decrements the number stored at {@code key} by one. + * + * @param key must not be {@literal null}. + * @see Redis Documentation: DECR + */ + Mono decrement(K key); + + /** + * Decrements the number stored at {@code key} by {@code delta}. + * @param key must not be {@literal null}. + * @param delta + * @see Redis Documentation: DECRBY + */ + Mono decrementBy(K key, long delta); } diff --git a/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java b/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java index 86f04d6e6..6fe76c079 100644 --- a/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java +++ b/src/test/java/org/springframework/data/redis/core/DefaultReactiveValueOperationsIntegrationTests.java @@ -384,4 +384,60 @@ public class DefaultReactiveValueOperationsIntegrationTests { 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(); + } }