DATAREDIS-815 - Polishing.

Update Javadoc, add tests and missing methods for BoundValueOperations.

Original Pull Request: #334
This commit is contained in:
Christoph Strobl
2018-04-26 14:58:14 +02:00
parent e5b14193d4
commit 8215f68d9f
5 changed files with 160 additions and 22 deletions

View File

@@ -15,9 +15,11 @@
*/
package org.springframework.data.redis.core;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
/**
* Value (or String in Redis terminology) operations bound to a certain key.
@@ -47,6 +49,26 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
*/
void set(V value, long timeout, TimeUnit unit);
/**
* Set the {@code value} and expiration {@code timeout} for the bound key.
*
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
* @since 2.1
*/
default void set(V value, Duration timeout) {
Assert.notNull(timeout, "Timeout must not be null!");
if (TimeoutUtils.hasMillis(timeout)) {
set(value, timeout.toMillis(), TimeUnit.MILLISECONDS);
} else {
set(value, timeout.getSeconds(), TimeUnit.SECONDS);
}
}
/**
* Set the bound key to hold the string {@code value} if the bound key is absent.
*
@@ -70,6 +92,28 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Boolean setIfAbsent(V value, long timeout, TimeUnit unit);
/**
* Set bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is absent.
*
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
*/
@Nullable
default Boolean setIfAbsent(V value, Duration timeout) {
Assert.notNull(timeout, "Timeout must not be null!");
if (TimeoutUtils.hasMillis(timeout)) {
return setIfAbsent(value, timeout.toMillis(), TimeUnit.MILLISECONDS);
}
return setIfAbsent(value, timeout.getSeconds(), TimeUnit.SECONDS);
}
/**
* Set the bound key to hold the string {@code value} if {@code key} is present.
*
@@ -96,6 +140,28 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Boolean setIfPresent(V value, long timeout, TimeUnit unit);
/**
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if {@code key} is present.
*
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @throws IllegalArgumentException if either {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
*/
@Nullable
default Boolean setIfPresent(V value, Duration timeout) {
Assert.notNull(timeout, "Timeout must not be null!");
if (TimeoutUtils.hasMillis(timeout)) {
return setIfPresent(value, timeout.toMillis(), TimeUnit.MILLISECONDS);
}
return setIfPresent(value, timeout.getSeconds(), TimeUnit.SECONDS);
}
/**
* Get the value of the bound key.
*

View File

@@ -23,15 +23,18 @@ import java.util.concurrent.TimeUnit;
*
* @author Jennifer Hickey
* @author Mark Paluch
* @author Christoph Strobl
*/
abstract public class TimeoutUtils {
/**
* @param duration the actual {@link Duration} to inspect.
* Check if a given Duration can be represented in {@code sec} or requires {@code msec} representation.
*
* @param duration the actual {@link Duration} to inspect. Never {@literal null}.
* @return {@literal true} if the {@link Duration} contains millisecond information.
* @since 2.1
*/
static boolean hasMillis(Duration duration) {
public static boolean hasMillis(Duration duration) {
return duration.toMillis() % 1000 != 0;
}

View File

@@ -58,10 +58,11 @@ public interface ValueOperations<K, V> {
* Set the {@code value} and expiration {@code timeout} for {@code key}.
*
* @param key must not be {@literal null}.
* @param value
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @since 2.1
* @throws IllegalArgumentException if either {@code key}, {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
* @since 2.1
*/
default void set(K key, V value, Duration timeout) {
@@ -103,11 +104,12 @@ public interface ValueOperations<K, V> {
* Set {@code key} to hold the string {@code value} and expiration {@code timeout} if {@code key} is absent.
*
* @param key must not be {@literal null}.
* @param value
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @throws IllegalArgumentException if either {@code key}, {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
*/
@Nullable
default Boolean setIfAbsent(K key, V value, Duration timeout) {
@@ -153,11 +155,12 @@ public interface ValueOperations<K, V> {
* Set {@code key} to hold the string {@code value} and expiration {@code timeout} if {@code key} is present.
*
* @param key must not be {@literal null}.
* @param value
* @param value must not be {@literal null}.
* @param timeout must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @throws IllegalArgumentException if either {@code key}, {@code value} or {@code timeout} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
*/
@Nullable
default Boolean setIfPresent(K key, V value, Duration timeout) {