Skip to content

Commit 12d97be

Browse files
ehsanalemzadehmp911de
authored andcommitted
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
1 parent 83cb3d8 commit 12d97be

File tree

1 file changed

+3
-32
lines changed

1 file changed

+3
-32
lines changed

src/main/java/org/springframework/data/redis/core/DefaultValueOperations.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.concurrent.TimeUnit;
25-
26-
import org.springframework.dao.DataAccessException;
2725
import org.springframework.data.redis.connection.BitFieldSubCommands;
2826
import org.springframework.data.redis.connection.RedisConnection;
2927
import org.springframework.data.redis.connection.RedisStringCommands.SetOption;
@@ -37,6 +35,7 @@
3735
* @author Jennifer Hickey
3836
* @author Christoph Strobl
3937
* @author Jiahe Cai
38+
* @author Ehsan Alemzadeh
4039
*/
4140
class DefaultValueOperations<K, V> extends AbstractOperations<K, V> implements ValueOperations<K, V> {
4241

@@ -250,43 +249,15 @@ public void set(K key, V value, long timeout, TimeUnit unit) {
250249
byte[] rawKey = rawKey(key);
251250
byte[] rawValue = rawValue(value);
252251

253-
execute(new RedisCallback<Object>() {
254-
255-
@Override
256-
public Object doInRedis(RedisConnection connection) throws DataAccessException {
257-
258-
potentiallyUsePsetEx(connection);
259-
return null;
260-
}
261-
262-
public void potentiallyUsePsetEx(RedisConnection connection) {
263-
264-
if (!TimeUnit.MILLISECONDS.equals(unit) || !failsafeInvokePsetEx(connection)) {
265-
connection.setEx(rawKey, TimeoutUtils.toSeconds(timeout, unit), rawValue);
266-
}
267-
}
268-
269-
private boolean failsafeInvokePsetEx(RedisConnection connection) {
270-
271-
boolean failed = false;
272-
try {
273-
connection.pSetEx(rawKey, timeout, rawValue);
274-
} catch (UnsupportedOperationException ignore) {
275-
// in case the connection does not support pSetEx return false to allow fallback to other operation.
276-
failed = true;
277-
}
278-
return !failed;
279-
}
280-
281-
});
252+
execute(connection -> connection.set(rawKey, rawValue, Expiration.from(timeout, unit), SetOption.upsert()));
282253
}
283254

284255
@Override
285256
public Boolean setIfAbsent(K key, V value) {
286257

287258
byte[] rawKey = rawKey(key);
288259
byte[] rawValue = rawValue(value);
289-
return execute(connection -> connection.setNX(rawKey, rawValue));
260+
return execute(connection -> connection.set(rawKey, rawValue, Expiration.persistent(), SetOption.ifAbsent()));
290261
}
291262

292263
@Override

0 commit comments

Comments
 (0)