From 12d97be97f321b50a3179509db6312ffd889dd4e Mon Sep 17 00:00:00 2001 From: Ehsan Alemzadeh Date: Wed, 24 Apr 2024 10:09:00 +0330 Subject: [PATCH] Use extended `SET` instead of deprecated setEx, pSetEx and setNX commands. Replace usage of deprecated commands setEx, pSetEx and setNX in DefaultValueOperations by set command with additional SetOption arguments Closes #2897 Original pull request: #2900 --- .../redis/core/DefaultValueOperations.java | 35 ++----------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java b/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java index 9938d5a0e..c3d3e00e5 100644 --- a/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java +++ b/src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java @@ -22,8 +22,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; - -import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.BitFieldSubCommands; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.RedisStringCommands.SetOption; @@ -37,6 +35,7 @@ import org.springframework.lang.Nullable; * @author Jennifer Hickey * @author Christoph Strobl * @author Jiahe Cai + * @author Ehsan Alemzadeh */ class DefaultValueOperations extends AbstractOperations implements ValueOperations { @@ -250,35 +249,7 @@ class DefaultValueOperations extends AbstractOperations implements V byte[] rawKey = rawKey(key); byte[] rawValue = rawValue(value); - execute(new RedisCallback() { - - @Override - public Object doInRedis(RedisConnection connection) throws DataAccessException { - - potentiallyUsePsetEx(connection); - return null; - } - - public void potentiallyUsePsetEx(RedisConnection connection) { - - if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) { - connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue); - } - } - - private boolean failsafeInvokePsetEx(RedisConnection connection) { - - boolean failed = false; - try { - connection.pSetEx(rawKey, timeout, rawValue); - } catch (UnsupportedOperationException ignore) { - // in case the connection does not support pSetEx return false to allow fallback to other operation. - failed = true; - } - return !failed; - } - - }); + execute(connection -> connection.set(rawKey, rawValue, Expiration.from(timeout, unit), SetOption.upsert())); } @Override @@ -286,7 +257,7 @@ class DefaultValueOperations extends AbstractOperations implements V byte[] rawKey = rawKey(key); byte[] rawValue = rawValue(value); - return execute(connection -> connection.setNX(rawKey, rawValue)); + return execute(connection -> connection.set(rawKey, rawValue, Expiration.persistent(), SetOption.ifAbsent())); } @Override