DATAREDIS-782 - Polishing.
Add setIfPresent(…) with expiry to reactive API. Add since tags, minor tweaks to javadoc and tests. Original pull request: #320.
This commit is contained in:
@@ -63,6 +63,7 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
|
||||
* @param timeout
|
||||
* @param unit must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
|
||||
*/
|
||||
@Nullable
|
||||
|
||||
@@ -28,7 +28,7 @@ class DefaultBoundValueOperations<K, V> extends DefaultBoundKeyOperations<K> imp
|
||||
private final ValueOperations<K, V> ops;
|
||||
|
||||
/**
|
||||
* Constructs a new <code>DefaultBoundValueOperations</code> instance.
|
||||
* Constructs a new {@link DefaultBoundValueOperations} instance.
|
||||
*
|
||||
* @param key
|
||||
* @param operations
|
||||
|
||||
@@ -86,10 +86,10 @@ 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)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#setIfAbsent(java.lang.Object, java.lang.Object, java.time.Duration)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Boolean> setIfAbsent(K key, V value, Duration timeout) {
|
||||
|
||||
Assert.notNull(key, "Key must not be null!");
|
||||
@@ -111,6 +111,19 @@ class DefaultReactiveValueOperations<K, V> implements ReactiveValueOperations<K,
|
||||
connection -> connection.set(rawKey(key), rawValue(value), Expiration.persistent(), SetOption.SET_IF_PRESENT));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#setIfPresent(java.lang.Object, java.lang.Object, java.time.Duration)
|
||||
*/
|
||||
@Override
|
||||
public Mono<Boolean> setIfPresent(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_PRESENT));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.redis.core.ReactiveValueOperations#multiSet(java.util.Map)
|
||||
*/
|
||||
|
||||
@@ -24,7 +24,7 @@ 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.connection.RedisStringCommands.SetOption;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
|
||||
/**
|
||||
@@ -267,12 +267,12 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
|
||||
*/
|
||||
@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);
|
||||
return execute(connection -> connection.set(rawKey, rawValue, expiration, SetOption.ifAbsent()), true);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -65,6 +65,7 @@ public interface ReactiveValueOperations<K, V> {
|
||||
* @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/set">Redis Documentation: SET</a>
|
||||
*/
|
||||
Mono<Boolean> setIfAbsent(K key, V value, Duration timeout);
|
||||
@@ -78,6 +79,17 @@ public interface ReactiveValueOperations<K, V> {
|
||||
*/
|
||||
Mono<Boolean> setIfPresent(K key, V value);
|
||||
|
||||
/**
|
||||
* 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}.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
|
||||
*/
|
||||
Mono<Boolean> setIfPresent(K key, V value, Duration timeout);
|
||||
|
||||
/**
|
||||
* Set multiple keys to multiple values using key-value pairs provided in {@code tuple}.
|
||||
*
|
||||
|
||||
@@ -68,9 +68,10 @@ public interface ValueOperations<K, V> {
|
||||
*
|
||||
* @param key must not be {@literal null}.
|
||||
* @param value
|
||||
* @param timeout
|
||||
* @param timeout must not be {@literal null}.
|
||||
* @param unit must not be {@literal null}.
|
||||
* @return {@literal null} when used in pipeline / transaction.
|
||||
* @since 2.1
|
||||
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
|
||||
*/
|
||||
@Nullable
|
||||
|
||||
@@ -18,7 +18,6 @@ package org.springframework.data.redis.core;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.Assume.*;
|
||||
|
||||
import reactor.core.publisher.Mono;
|
||||
import reactor.test.StepVerifier;
|
||||
|
||||
import java.nio.ByteBuffer;
|
||||
@@ -142,16 +141,18 @@ public class DefaultReactiveValueOperationsIntegrationTests<K, V> {
|
||||
K key = keyFactory.instance();
|
||||
V value = valueFactory.instance();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))).expectNext(true)
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofSeconds(5))).expectNext(true)
|
||||
.expectComplete().verify();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value)).expectNext(false).verifyComplete();
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))).expectNext(false)
|
||||
StepVerifier.create(valueOperations.setIfAbsent(key, value, Duration.ofSeconds(5))).expectNext(false)
|
||||
.verifyComplete();
|
||||
|
||||
Mono<Boolean> mono = valueOperations.setIfAbsent(key, value, Duration.ofMillis(500))
|
||||
.delaySubscription(Duration.ofMillis(500));
|
||||
StepVerifier.create(mono).expectNext(true).verifyComplete();
|
||||
StepVerifier.create(redisTemplate.getExpire(key)) //
|
||||
.assertNext(actual -> {
|
||||
|
||||
assertThat(actual).isBetween(Duration.ofMillis(1), Duration.ofSeconds(5));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-602, DATAREDIS-779
|
||||
@@ -170,6 +171,30 @@ public class DefaultReactiveValueOperationsIntegrationTests<K, V> {
|
||||
StepVerifier.create(valueOperations.get(key)).expectNext(laterValue).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-782
|
||||
public void setIfPresentWithExpiry() {
|
||||
|
||||
K key = keyFactory.instance();
|
||||
V value = valueFactory.instance();
|
||||
V laterValue = valueFactory.instance();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfPresent(key, value, Duration.ofSeconds(5))).expectNext(false)
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.set(key, value, Duration.ofSeconds(5))).expectNext(true).verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.setIfPresent(key, laterValue, Duration.ofSeconds(5))).expectNext(true)
|
||||
.verifyComplete();
|
||||
|
||||
StepVerifier.create(valueOperations.get(key)).expectNext(laterValue).verifyComplete();
|
||||
|
||||
StepVerifier.create(redisTemplate.getExpire(key)) //
|
||||
.assertNext(actual -> {
|
||||
|
||||
assertThat(actual).isBetween(Duration.ofMillis(1), Duration.ofSeconds(5));
|
||||
}).verifyComplete();
|
||||
}
|
||||
|
||||
@Test // DATAREDIS-602
|
||||
public void multiSet() {
|
||||
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.data.redis.core;
|
||||
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.junit.Assume.*;
|
||||
import static org.springframework.data.redis.SpinBarrier.*;
|
||||
@@ -47,6 +48,7 @@ import org.springframework.data.redis.RedisTestProfileValueSource;
|
||||
* @author Christoph Strobl
|
||||
* @author David Liu
|
||||
* @author Thomas Darimont
|
||||
* @author Jiahe Cai
|
||||
*/
|
||||
@RunWith(Parameterized.class)
|
||||
public class DefaultValueOperationsTests<K, V> {
|
||||
@@ -237,13 +239,15 @@ public class DefaultValueOperationsTests<K, V> {
|
||||
|
||||
@Test // DATAREDIS-782
|
||||
public void testSetIfAbsentWithExpiration() {
|
||||
final K key1 = keyFactory.instance();
|
||||
K key = keyFactory.instance();
|
||||
V value1 = valueFactory.instance();
|
||||
V value2 = valueFactory.instance();
|
||||
assertTrue(valueOps.setIfAbsent(key1, value1, 500, TimeUnit.MILLISECONDS));
|
||||
assertFalse(valueOps.setIfAbsent(key1, value2));
|
||||
assertFalse(valueOps.setIfAbsent(key1, value2, 500, TimeUnit.MILLISECONDS));
|
||||
waitFor(() -> (valueOps.setIfAbsent(key1, value1, 500, TimeUnit.MILLISECONDS)), 500);
|
||||
assertTrue(valueOps.setIfAbsent(key, value1, 5, TimeUnit.SECONDS));
|
||||
assertFalse(valueOps.setIfAbsent(key, value2));
|
||||
assertFalse(valueOps.setIfAbsent(key, value2, 5, TimeUnit.SECONDS));
|
||||
Long expire = redisTemplate.getExpire(key, TimeUnit.MILLISECONDS);
|
||||
assertThat(expire, is(lessThan(TimeUnit.SECONDS.toMillis(6))));
|
||||
assertThat(expire, is(greaterThan(TimeUnit.MILLISECONDS.toMillis(1))));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user