Skip to content

Commit 4a71ca2

Browse files
author
Andre Prata
committed
DATAREDIS-1195 - Make putIfAbsent atomic when TTL is set
1 parent 55ef261 commit 4a71ca2

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

src/main/java/org/springframework/data/redis/cache/DefaultRedisCacheWriter.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*
4646
* @author Christoph Strobl
4747
* @author Mark Paluch
48+
* @author André Prata
4849
* @since 2.0
4950
*/
5051
class DefaultRedisCacheWriter implements RedisCacheWriter {
@@ -153,12 +154,15 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
153154
}
154155

155156
try {
156-
if (connection.setNX(key, value)) {
157157

158-
if (shouldExpireWithin(ttl)) {
159-
connection.pExpire(key, ttl.toMillis());
160-
}
158+
boolean put;
159+
if (shouldExpireWithin(ttl)) {
160+
put = connection.set(key, value, Expiration.milliseconds(ttl.toMillis()), SetOption.ifAbsent());
161+
} else {
162+
put = connection.setNX(key, value);
163+
}
161164

165+
if (put) {
162166
statistics.incPuts(name);
163167
return null;
164168
}

0 commit comments

Comments
 (0)