DATAREDIS-786 - Polishing.

Update Javadoc and add setIfPresent to BoundValueOperations.

Original Pull Request: #334
This commit is contained in:
Christoph Strobl
2018-04-26 13:45:21 +02:00
parent b0873d29b6
commit f1be9b736d
5 changed files with 138 additions and 15 deletions

View File

@@ -25,6 +25,7 @@ import org.springframework.lang.Nullable;
* @author Costin Leau
* @author Mark Paluch
* @author Jiahe Cai
* @author Christoph Strobl
*/
public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@@ -69,6 +70,32 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
@Nullable
Boolean setIfAbsent(V value, long timeout, TimeUnit unit);
/**
* Set the bound key to hold the string {@code value} if {@code key} is present.
*
* @param value must not be {@literal null}.
* @return command result indicating if the key has been set.
* @throws IllegalArgumentException if {@code value} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
*/
@Nullable
Boolean setIfPresent(V value);
/**
* 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 the key expiration timeout.
* @param unit must not be {@literal null}.
* @return command result indicating if the key has been set.
* @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
Boolean setIfPresent(V value, long timeout, TimeUnit unit);
/**
* Get the value of the bound key.
*
@@ -90,7 +117,6 @@ public interface BoundValueOperations<K, V> extends BoundKeyOperations<K> {
/**
* 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>

View File

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

View File

@@ -36,7 +36,7 @@ public interface ValueOperations<K, V> {
* Set {@code value} for {@code key}.
*
* @param key must not be {@literal null}.
* @param value
* @param value must not be {@literal null}.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
*/
void set(K key, V value);
@@ -45,8 +45,8 @@ 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 timeout
* @param value must not be {@literal null}.
* @param timeout the key expiration timeout.
* @param unit must not be {@literal null}.
* @see <a href="http://redis.io/commands/setex">Redis Documentation: SETEX</a>
*/
@@ -56,7 +56,7 @@ public interface ValueOperations<K, V> {
* Set {@code key} to hold the string {@code value} if {@code key} is absent.
*
* @param key must not be {@literal null}.
* @param value
* @param value must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @see <a href="http://redis.io/commands/setnx">Redis Documentation: SETNX</a>
*/
@@ -67,8 +67,8 @@ 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 timeout must not be {@literal null}.
* @param value must not be {@literal null}.
* @param timeout the key expiration timeout.
* @param unit must not be {@literal null}.
* @return {@literal null} when used in pipeline / transaction.
* @since 2.1
@@ -81,9 +81,11 @@ public interface ValueOperations<K, V> {
* Set {@code key} to hold the string {@code value} if {@code key} is present.
*
* @param key must not be {@literal null}.
* @param value
* @since 2.1
* @param value must not be {@literal null}.
* @return command result indicating if the key has been set.
* @throws IllegalArgumentException if either {@code key} or {@code value} is not present.
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
* @since 2.1
*/
@Nullable
Boolean setIfPresent(K key, V value);
@@ -92,10 +94,13 @@ 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 timeout must not be {@literal null}.
* @since 2.1
* @param value must not be {@literal null}.
* @param timeout the key expiration timeout.
* @param unit must not be {@literal null}.
* @return command result indicating if the key has been set.
* @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
*/
@Nullable
Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit);

View File

@@ -0,0 +1,67 @@
/*
* Copyright 2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.core;
import static org.mockito.Mockito.*;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
/**
* Unit tests for {@link DefaultBoundValueOperations}
*
* @author Christoph Strobl
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultBoundValueOperationsUnitTests {
DefaultBoundValueOperations<String, Object> boundValueOps;
@Mock ValueOperations<String, Object> valueOps;
static final String KEY = "key-1";
static final Object VALUE = "value";
@Before
public void setUp() {
RedisOperations<String, Object> redisOps = mock(RedisOperations.class);
when(redisOps.opsForValue()).thenReturn(valueOps);
boundValueOps = new DefaultBoundValueOperations<>(KEY, redisOps);
}
@Test // DATAREDIS-786
public void setIfPresentShouldDelegateCorrectly() {
boundValueOps.setIfPresent(VALUE);
verify(valueOps).setIfPresent(eq(KEY), eq(VALUE));
}
@Test // DATAREDIS-786
public void setIfPresentWithTimeoutShouldDelegateCorrectly() {
boundValueOps.setIfPresent(VALUE, 10, TimeUnit.SECONDS);
verify(valueOps).setIfPresent(eq(KEY), eq(VALUE), eq(10L), eq(TimeUnit.SECONDS));
}
}

View File

@@ -345,13 +345,12 @@ public class DefaultValueOperationsTests<K, V> {
}
@Test // DATAREDIS-786
public void testSetIfPresent() {
public void setIfPresentReturnsTrueWhenKeyExists() {
K key = keyFactory.instance();
V value1 = valueFactory.instance();
V value2 = valueFactory.instance();
assertFalse(valueOps.setIfPresent(key, value1));
valueOps.set(key, value1);
assertTrue(valueOps.setIfPresent(key, value2));
@@ -359,7 +358,12 @@ public class DefaultValueOperationsTests<K, V> {
}
@Test // DATAREDIS-786
public void testSetIfPresentWithExpiration() {
public void setIfPresentReturnsFalseWhenKeyDoesNotExist() {
assertFalse(valueOps.setIfPresent(keyFactory.instance(), valueFactory.instance()));
}
@Test // DATAREDIS-786
public void setIfPresentShouldSetExpirationCorrectly() {
K key = keyFactory.instance();
V value1 = valueFactory.instance();