DATAREDIS-784 - Polishing.
Rename incrementBy and decrementBy to increment respective decrement. Use INCR and DECR for increment() and decrement() instead of INCRBY 1/INCRYBY -1. Rearrange method order to align with method order in ReactiveValueOperations. Tweak Javadoc, add since tags. Introduce increment() and decrement(…) (as of INCR/DECR) to ValueOperations and BoundValueOperations and use ValueOperations.increment()/decrement() from RedisAtomicInteger. ValueOperations used INCRBY and DECRBY to increment and now we use the native command if methods without delta are invoked. Original pull request: #322.
This commit is contained in:
@@ -117,6 +117,7 @@ public interface ReactiveNumberCommands {
|
||||
* @param value must not be {@literal null}.
|
||||
* @return
|
||||
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
|
||||
* @see <a href="http://redis.io/commands/incrbyfloat">Redis Documentation: INCRBYFLOAT</a>
|
||||
*/
|
||||
default <T extends Number> Mono<T> incrBy(ByteBuffer key, T value) {
|
||||
|
||||
@@ -132,6 +133,7 @@ public interface ReactiveNumberCommands {
|
||||
* @param commands must not be {@literal null}.
|
||||
* @return
|
||||
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
|
||||
* @see <a href="http://redis.io/commands/incrbyfloat">Redis Documentation: INCRBYFLOAT</a>
|
||||
*/
|
||||
<T extends Number> Flux<NumericResponse<ReactiveNumberCommands.IncrByCommand<T>, T>> incrBy(
|
||||
Publisher<ReactiveNumberCommands.IncrByCommand<T>> commands);
|
||||
|
||||
@@ -75,7 +75,7 @@ class LettuceReactiveNumberCommands implements ReactiveNumberCommands {
|
||||
|
||||
T incrBy = command.getValue();
|
||||
|
||||
Mono<? extends Number> result = null;
|
||||
Mono<? extends Number> result;
|
||||
if (incrBy instanceof Double || incrBy instanceof Float) {
|
||||
result = cmd.incrbyfloat(command.getKey(), incrBy.doubleValue());
|
||||
} else {
|
||||
|
||||
@@ -87,12 +87,23 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
@Nullable
|
||||
V getAndSet(V value);
|
||||
|
||||
/**
|
||||
* Increment an integer value stored as string value under the bound key by one.
|
||||
*
|
||||
* @param delta
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long increment();
|
||||
|
||||
/**
|
||||
* Increment an integer value stored as string value under the bound key by {@code delta}.
|
||||
*
|
||||
* @param delta
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a>
|
||||
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long increment(long delta);
|
||||
@@ -107,6 +118,27 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
@Nullable
|
||||
Double increment(double delta);
|
||||
|
||||
/**
|
||||
* Decrement an integer value stored as string value under the bound key by one.
|
||||
*
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/decr">Redis Documentation: DECR</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long decrement();
|
||||
|
||||
/**
|
||||
* Decrement an integer value stored as string value under the bound key by {@code delta}.
|
||||
*
|
||||
* @param delta
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/decrby">Redis Documentation: DECRBY</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long decrement(long delta);
|
||||
|
||||
/**
|
||||
* Append a {@code value} to the bound key.
|
||||
*
|
||||
|
||||
@@ -22,6 +22,7 @@ import org.springframework.data.redis.connection.DataType;
|
||||
/**
|
||||
* @author Costin Leau
|
||||
* @author Jiahe Cai
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundValueOperations<K, V> {
|
||||
|
||||
@@ -57,6 +58,15 @@ class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> imp
|
||||
return ops.getAndSet(getKey(), value);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#increment()
|
||||
*/
|
||||
@Override
|
||||
public Long increment() {
|
||||
return ops.increment(getKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#increment(long)
|
||||
@@ -75,6 +85,24 @@ class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> imp
|
||||
return ops.increment(getKey(), delta);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#decrement()
|
||||
*/
|
||||
@Override
|
||||
public Long decrement() {
|
||||
return ops.decrement(getKey());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#decrement(double)
|
||||
*/
|
||||
@Override
|
||||
public Long decrement(long delta) {
|
||||
return ops.decrement(getKey(), delta);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.BoundValueOperations#append(java.lang.String)
|
||||
|
||||
@@ -194,6 +194,61 @@ class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K,
|
||||
.map(this::deserializeValues));
|
||||
}
|
||||
|
||||
/* (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 template.createMono(connection -> connection.numberCommands().incr(rawKey(key)));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#increment(java.lang.Object, long)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Long> increment(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#increment(java.lang.Object, double)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Double> increment(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 template.createMono(connection -> connection.numberCommands().decr(rawKey(key)));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#decrement(java.lang.Object, long)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Long> decrement(K key, long delta) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
|
||||
return template.createMono(connection -> connection.numberCommands().decrBy(rawKey(key), delta));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#append(java.lang.Object, java.lang.String)
|
||||
*/
|
||||
@@ -273,61 +328,6 @@ 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) {
|
||||
|
||||
|
||||
@@ -74,6 +74,17 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
}, true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#increment(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Long increment(K key) {
|
||||
|
||||
byte[] rawKey = rawKey(key);
|
||||
return execute(connection -> connection.incr(rawKey), true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#increment(java.lang.Object, long)
|
||||
@@ -96,6 +107,28 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
return execute(connection -> connection.incrBy(rawKey, delta), true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#decrement(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public Long decrement(K key) {
|
||||
|
||||
byte[] rawKey = rawKey(key);
|
||||
return execute(connection -> connection.decr(rawKey), true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#increment(java.lang.Object, long)
|
||||
*/
|
||||
@Override
|
||||
public Long decrement(K key, long delta) {
|
||||
|
||||
byte[] rawKey = rawKey(key);
|
||||
return execute(connection -> connection.decrBy(rawKey, delta), true);
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ValueOperations#append(java.lang.Object, java.lang.String)
|
||||
|
||||
@@ -131,6 +131,54 @@ public interface ReactiveValueOperations<K, V> {
|
||||
*/
|
||||
Mono<List<V>> multiGet(Collection<K> keys);
|
||||
|
||||
/**
|
||||
* Increments the number stored at {@code key} by one.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @since 2.1
|
||||
* @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
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
|
||||
*/
|
||||
Mono<Long> increment(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
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/incrbyfloat">Redis Documentation: INCRBYFLOAT</a>
|
||||
*/
|
||||
Mono<Double> increment(K key, double delta);
|
||||
|
||||
/**
|
||||
* Decrements the number stored at {@code key} by one.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @since 2.1
|
||||
* @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
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/decrby">Redis Documentation: DECRBY</a>
|
||||
*/
|
||||
Mono<Long> decrement(K key, long delta);
|
||||
|
||||
/**
|
||||
* Append a {@code value} to {@code key}.
|
||||
*
|
||||
@@ -183,7 +231,7 @@ public interface ReactiveValueOperations<K, V> {
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param offset
|
||||
* @see <a href="http://redis.io/commands/setbit">Redis Documentation: GETBIT</a>
|
||||
* @see <a href="http://redis.io/commands/getbit">Redis Documentation: GETBIT</a>
|
||||
*/
|
||||
Mono<Boolean> getBit(K key, long offset);
|
||||
|
||||
@@ -193,44 +241,4 @@ 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);
|
||||
}
|
||||
|
||||
@@ -126,13 +126,24 @@ public interface ValueOperations<K, V> {
|
||||
@Nullable
|
||||
List<V> multiGet(Collection<K> keys);
|
||||
|
||||
/**
|
||||
* Increment an integer value stored as string value under {@code key} by one.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long increment(K key);
|
||||
|
||||
/**
|
||||
* Increment an integer value stored as string value under {@code key} by {@code delta}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param delta
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @see <a href="http://redis.io/commands/incr">Redis Documentation: INCR</a>
|
||||
* @see <a href="http://redis.io/commands/incrby">Redis Documentation: INCRBY</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long increment(K key, long delta);
|
||||
@@ -148,6 +159,29 @@ public interface ValueOperations<K, V> {
|
||||
@Nullable
|
||||
Double increment(K key, double delta);
|
||||
|
||||
/**
|
||||
* Decrement an integer value stored as string value under {@code key} by one.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/decr">Redis Documentation: DECR</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long decrement(K key);
|
||||
|
||||
/**
|
||||
* Decrement an integer value stored as string value under {@code key} by {@code delta}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param delta
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/decrby">Redis Documentation: DECRBY</a>
|
||||
*/
|
||||
@Nullable
|
||||
Long decrement(K key, long delta);
|
||||
|
||||
/**
|
||||
* Append a {@code value} to {@code key}.
|
||||
*
|
||||
|
||||
@@ -30,7 +30,6 @@ import org.springframework.data.redis.core.SessionCallback;
|
||||
import org.springframework.data.redis.core.ValueOperations;
|
||||
import org.springframework.data.redis.serializer.GenericToStringSerializer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.data.redis.serializer.StringRedisSerializer;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -245,7 +244,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
* @return the updated value.
|
||||
*/
|
||||
public int incrementAndGet() {
|
||||
return operations.increment(key, 1).intValue();
|
||||
return operations.increment(key).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,7 +253,7 @@ public class RedisAtomicInteger extends Number implements Serializable, BoundKey
|
||||
* @return the updated value.
|
||||
*/
|
||||
public int decrementAndGet() {
|
||||
return operations.increment(key, -1).intValue();
|
||||
return operations.decrement(key).intValue();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user