DATAREDIS-782 - Add support for SET key value NX EX max-lock-time.

We now expose ValueOperations.setIfAbsent(…) to set values when absent along their expiry.

Original pull request: #320.
This commit is contained in:
Jiahe Cai (佳何 蔡)
2018-03-09 17:54:53 +08:00
committed by Mark Paluch
parent 567fa43e60
commit a2ed8e1d9b
8 changed files with 109 additions and 0 deletions

View File

@@ -24,6 +24,7 @@ import org.springframework.lang.Nullable;
*
* @author Costin Leau
* @author Mark Paluch
* @author Jiahe Cai
*/
public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@@ -55,6 +56,18 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Boolean setIfAbsent(V value);
/**
* Set the bound key to hold the string {@code value} and expiration {@code timeout} if the bound key is absent.
*
* @param value must not be {@literal null}.
* @param timeout
* @param unit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
*/
@Nullable
Boolean setIfAbsent(V value, long timeout, TimeUnit unit);
/**
* Get the value of the bound key.
*

View File

@@ -21,6 +21,7 @@ import org.springframework.data.redis.connection.DataType;
/**
* @author Costin Leau
* @author Jiahe Cai
*/
class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> implements BoundValueOperations<K, V> {
@@ -119,6 +120,15 @@ class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> imp
return ops.setIfAbsent(getKey(), value);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundValueOperations#setIfAbsent(java.lang.Object, long, java.util.concurrent.TimeUnit)
*/
@Override
public Boolean setIfAbsent(V value, long timeout, TimeUnit unit) {
return ops.setIfAbsent(getKey(), value, timeout, unit);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.BoundValueOperations#set(java.lang.Object, long)

View File

@@ -41,6 +41,7 @@ import org.springframework.util.Assert;
*
* @author Mark Paluch
* @author Christoph Strobl
* @author Jiahe Cai
* @since 2.0
*/
@RequiredArgsConstructor
@@ -85,6 +86,19 @@ class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K,
connection -> connection.set(rawKey(key), rawValue(value), Expiration.persistent(), SetOption.SET_IF_ABSENT));
}
@Override
/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#setIfAbsent(java.lang.Object, java.lang.Object, long, java.util.concurrent.TimeUnit)
*/
public Mono<Boolean> setIfAbsent(K key, V value, Duration timeout) {
Assert.notNull(key, "Key must not be null!");
Assert.notNull(timeout, "Duration must not be null!");
return createMono(
connection -> connection.set(rawKey(key), rawValue(value), Expiration.from(timeout), SetOption.SET_IF_ABSENT));
}
/* (non-Javadoc)
* @see org.springframework.data.redis.core.ReactiveValueOperations#setIfPresent(java.lang.Object, java.lang.Object)
*/

View File

@@ -24,6 +24,8 @@ import java.util.concurrent.TimeUnit;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands;
import org.springframework.data.redis.core.types.Expiration;
/**
* Default implementation of {@link ValueOperations}.
@@ -31,6 +33,7 @@ import org.springframework.data.redis.connection.RedisConnection;
* @author Costin Leau
* @author Jennifer Hickey
* @author Christoph Strobl
* @author Jiahe Cai
*/
class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements ValueOperations<K, V> {
@@ -258,6 +261,20 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
return execute(connection -> connection.setNX(rawKey, rawValue), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ValueOperations#setIfAbsent(java.lang.Object, java.lang.Object, long, java.util.concurrent.TimeUnit)
*/
@Override
public Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
Expiration expiration = Expiration.from(timeout, unit);
RedisStringCommands.SetOption setOption = RedisStringCommands.SetOption.ifAbsent();
return execute(connection -> connection.set(rawKey, rawValue, expiration, setOption), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ValueOperations#set(java.lang.Object, java.lang.Object, long)

View File

@@ -26,6 +26,7 @@ import java.util.Map;
* Reactive Redis operations for simple (or in Redis terminology 'string') values.
*
* @author Mark Paluch
* @author Jiahe Cai
* @since 2.0
*/
public interface ReactiveValueOperations<K, V> {
@@ -58,6 +59,16 @@ public interface ReactiveValueOperations<K, V> {
*/
Mono<Boolean> setIfAbsent(K key, V value);
/**
* 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}.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
*/
Mono<Boolean> setIfAbsent(K key, V value, Duration timeout);
/**
* Set {@code key} to hold the string {@code value} if {@code key} is present.
*

View File

@@ -28,6 +28,7 @@ import org.springframework.lang.Nullable;
* @author Costin Leau
* @author Christoph Strobl
* @author Mark Paluch
* @author Jiahe Cai
*/
public interface ValueOperations<K, V> {
@@ -62,6 +63,19 @@ public interface ValueOperations<K, V> {
@Nullable
Boolean setIfAbsent(K key, V value);
/**
* 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
* @param unit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
*/
@Nullable
Boolean setIfAbsent(K key, V value, long timeout, TimeUnit unit);
/**
* Set multiple keys to multiple values using key-value pairs provided in {@code tuple}.
*