DATAREDIS-588 - Polishing.
Simplify SET command usage with Jedis using various expiry and presence/absence flags to use consistently SetParams. Original Pull Request: #463
This commit is contained in:
committed by
Christoph Strobl
parent
f6e05740ff
commit
15fd1d38de
@@ -25,7 +25,6 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||
@@ -40,7 +39,6 @@ import org.springframework.data.redis.connection.lettuce.LettuceConverters;
|
||||
import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.data.redis.util.ByteUtils;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -134,39 +132,12 @@ class JedisClusterStringCommands implements RedisStringCommands {
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
Assert.notNull(option, "Option must not be null!");
|
||||
|
||||
if (expiration.isPersistent()) {
|
||||
SetParams setParams = JedisConverters.toSetCommandExPxArgument(expiration, JedisConverters.toSetCommandNxXxArgument(option));
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(SetOption.UPSERT, option)) {
|
||||
return set(key, value);
|
||||
} else {
|
||||
|
||||
// BinaryCluster does not support set with nxxx and binary key/value pairs.
|
||||
if (ObjectUtils.nullSafeEquals(SetOption.SET_IF_PRESENT, option)) {
|
||||
throw new UnsupportedOperationException("Jedis does not support SET XX without PX or EX on BinaryCluster.");
|
||||
}
|
||||
|
||||
return setNX(key, value);
|
||||
}
|
||||
} else {
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(SetOption.UPSERT, option)) {
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(TimeUnit.MILLISECONDS, expiration.getTimeUnit())) {
|
||||
return pSetEx(key, expiration.getExpirationTime(), value);
|
||||
} else {
|
||||
return setEx(key, expiration.getExpirationTime(), value);
|
||||
}
|
||||
} else {
|
||||
|
||||
SetParams params = JedisConverters.toSetCommandNxXxArgument(option);
|
||||
JedisConverters.toSetCommandExPxArgument(expiration, params);
|
||||
|
||||
try {
|
||||
return Converters.stringToBoolean(connection.getCluster().set(key, value, params));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
try {
|
||||
return Converters.stringToBoolean(connection.getCluster().set(key, value, setParams));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -533,11 +533,15 @@ abstract public class JedisConverters extends Converters {
|
||||
|
||||
SetParams paramsToUse = params == null ? SetParams.setParams() : params;
|
||||
|
||||
if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
|
||||
return paramsToUse.px(expiration.getExpirationTime());
|
||||
if (!expiration.isPersistent()) {
|
||||
if (expiration.getTimeUnit() == TimeUnit.MILLISECONDS) {
|
||||
return paramsToUse.px(expiration.getExpirationTime());
|
||||
}
|
||||
|
||||
return paramsToUse.ex((int) expiration.getExpirationTime());
|
||||
}
|
||||
|
||||
return paramsToUse.ex((int) expiration.getExpirationTime());
|
||||
return params;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -23,7 +23,6 @@ import redis.clients.jedis.params.SetParams;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.data.domain.Range;
|
||||
import org.springframework.data.redis.connection.BitFieldSubCommands;
|
||||
@@ -33,7 +32,6 @@ import org.springframework.data.redis.core.types.Expiration;
|
||||
import org.springframework.data.redis.util.ByteUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
|
||||
/**
|
||||
* @author Christoph Strobl
|
||||
@@ -159,79 +157,26 @@ class JedisStringCommands implements RedisStringCommands {
|
||||
Assert.notNull(expiration, "Expiration must not be null!");
|
||||
Assert.notNull(option, "Option must not be null!");
|
||||
|
||||
if (expiration.isPersistent()) {
|
||||
SetParams params = JedisConverters.toSetCommandExPxArgument(expiration, JedisConverters.toSetCommandNxXxArgument(option));
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(SetOption.UPSERT, option)) {
|
||||
return set(key, value);
|
||||
} else {
|
||||
try {
|
||||
if (isPipelined()) {
|
||||
|
||||
try {
|
||||
SetParams nxxx = JedisConverters.toSetCommandNxXxArgument(option);
|
||||
pipeline(connection.newJedisResult(connection.getRequiredPipeline().set(key, value, params),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
if (isQueueing()) {
|
||||
|
||||
if (isPipelined()) {
|
||||
|
||||
pipeline(connection.newJedisResult(connection.getRequiredPipeline().set(key, value, nxxx),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
if (isQueueing()) {
|
||||
|
||||
transaction(connection.newJedisResult(connection.getRequiredTransaction().set(key, value, nxxx),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
|
||||
return Converters.stringToBoolean(connection.getJedis().set(key, value, nxxx));
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
transaction(connection.newJedisResult(connection.getRequiredTransaction().set(key, value, params),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
|
||||
} else {
|
||||
return Converters.stringToBoolean(connection.getJedis().set(key, value, params));
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(SetOption.UPSERT, option)) {
|
||||
|
||||
if (ObjectUtils.nullSafeEquals(TimeUnit.MILLISECONDS, expiration.getTimeUnit())) {
|
||||
return pSetEx(key, expiration.getExpirationTime(), value);
|
||||
} else {
|
||||
return setEx(key, expiration.getExpirationTime(), value);
|
||||
}
|
||||
} else {
|
||||
|
||||
SetParams params = JedisConverters.toSetCommandNxXxArgument(option, null);
|
||||
JedisConverters.toSetCommandExPxArgument(expiration, params);
|
||||
|
||||
try {
|
||||
if (isPipelined()) {
|
||||
|
||||
if (expiration.getExpirationTime() > Integer.MAX_VALUE) {
|
||||
|
||||
throw new IllegalArgumentException(
|
||||
"Expiration.expirationTime must be less than Integer.MAX_VALUE for pipeline in Jedis.");
|
||||
}
|
||||
|
||||
pipeline(connection.newJedisResult(connection.getRequiredPipeline().set(key, value, params),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
if (isQueueing()) {
|
||||
|
||||
if (expiration.getExpirationTime() > Integer.MAX_VALUE) {
|
||||
throw new IllegalArgumentException(
|
||||
"Expiration.expirationTime must be less than Integer.MAX_VALUE for transactions in Jedis.");
|
||||
}
|
||||
|
||||
transaction(connection.newJedisResult(connection.getRequiredTransaction().set(key, value, params),
|
||||
Converters.stringToBooleanConverter(), () -> false));
|
||||
return null;
|
||||
}
|
||||
|
||||
return Converters.stringToBoolean(connection.getJedis().set(key, value, params));
|
||||
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw convertJedisAccessException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user