DATAREDIS-815 - Add method variants accepting/returning java.time.Duration.
Original Pull Request: #334
This commit is contained in:
committed by
Christoph Strobl
parent
f1be9b736d
commit
e5b14193d4
@@ -15,15 +15,26 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Helper class featuring methods for calculating Redis timeouts
|
||||
*
|
||||
* @author Jennifer Hickey
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
abstract public class TimeoutUtils {
|
||||
|
||||
/**
|
||||
* @param duration the actual {@link Duration} to inspect.
|
||||
* @return {@literal true} if the {@link Duration} contains millisecond information.
|
||||
* @since 2.1
|
||||
*/
|
||||
static boolean hasMillis(Duration duration) {
|
||||
return duration.toMillis() % 1000 != 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given timeout to seconds.
|
||||
* <p>
|
||||
|
||||
@@ -15,12 +15,14 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Redis operations for simple (or in Redis terminology 'string') values.
|
||||
@@ -52,6 +54,26 @@ public interface ValueOperations<K, V> {
|
||||
*/
|
||||
void set(K key, V value, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* Set the {@code value} and expiration {@code timeout} for {@code key}.
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param value
|
||||
* @param timeout must not be {@literal null}.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
|
||||
*/
|
||||
default void set(K key, V value, Duration timeout) {
|
||||
|
||||
Assert.notNull(timeout, "Timeout must not be null!");
|
||||
|
||||
if (TimeoutUtils.hasMillis(timeout)) {
|
||||
set(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS);
|
||||
} else {
|
||||
set(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set {@code key} to hold the string {@code value} if {@code key} is absent.
|
||||
*
|
||||
@@ -77,6 +99,28 @@ public interface ValueOperations<K, V> {
|
||||
@Nullable
|
||||
Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* 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 timeout must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @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) {
|
||||
|
||||
Assert.notNull(timeout, "Timeout must not be null!");
|
||||
|
||||
if (TimeoutUtils.hasMillis(timeout)) {
|
||||
return setIfAbsent(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
return setIfAbsent(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set {@code key} to hold the string {@code value} if {@code key} is present.
|
||||
*
|
||||
@@ -105,6 +149,28 @@ public interface ValueOperations<K, V> {
|
||||
@Nullable
|
||||
Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit);
|
||||
|
||||
/**
|
||||
* 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 timeout must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @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) {
|
||||
|
||||
Assert.notNull(timeout, "Timeout must not be null!");
|
||||
|
||||
if (TimeoutUtils.hasMillis(timeout)) {
|
||||
return setIfPresent(key, value, timeout.toMillis(), TimeUnit.MILLISECONDS);
|
||||
}
|
||||
|
||||
return setIfPresent(key, value, timeout.getSeconds(), TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple keys to multiple values using key-value pairs provided in {@code tuple}.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user