DATAREDIS-786 - Add ValueOperations.setIfPresent(…).

We now expose setIfPresent(…) through ValueOperations.

Original Pull Request: #334
This commit is contained in:
Mark Paluch
2018-04-24 11:48:05 +02:00
committed by Christoph Strobl
parent 16f7a445ba
commit 2c11a1c402
3 changed files with 85 additions and 0 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
import org.springframework.data.redis.core.types.Expiration;
import org.springframework.lang.Nullable;
/**
* Default implementation of {@link ValueOperations}.
@@ -308,6 +309,35 @@ class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements V
return execute(connection -> connection.set(rawKey, rawValue, expiration, SetOption.ifAbsent()), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ValueOperations#setIfPresent(java.lang.Object, java.lang.Object)
*/
@Nullable
@Override
public Boolean setIfPresent(K key, V value) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
return execute(connection -> connection.set(rawKey, rawValue, Expiration.persistent(), SetOption.ifPresent()), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ValueOperations#setIfPresent(java.lang.Object, java.lang.Object, long, java.util.concurrent.TimeUnit)
*/
@Nullable
@Override
public Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit) {
byte[] rawKey = rawKey(key);
byte[] rawValue = rawValue(value);
Expiration expiration = Expiration.from(timeout, unit);
return execute(connection -> connection.set(rawKey, rawValue, expiration, SetOption.ifPresent()), true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.redis.core.ValueOperations#set(java.lang.Object, java.lang.Object, long)

View File

@@ -77,6 +77,29 @@ 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} if {@code key} is present.
*
* @param key must not be {@literal null}.
* @param value
* @since 2.1
* @see <a href="http://redis.io/commands/set">Redis Documentation: SET</a>
*/
@Nullable
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>
*/
@Nullable
Boolean setIfPresent(K key, V value, long timeout, TimeUnit unit);
/**
* Set multiple keys to multiple values using key-value pairs provided in {@code tuple}.
*