Skip to content

Commit f19f336

Browse files
Andre Pratamp911de
authored andcommitted
DATAREDIS-1195 - Make putIfAbsent atomic when TTL is set.
Original pull request: #553.
1 parent a456798 commit f19f336

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

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

Lines changed: 9 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 {
@@ -127,11 +128,15 @@ public byte[] putIfAbsent(String name, byte[] key, byte[] value, @Nullable Durat
127128
}
128129

129130
try {
130-
if (connection.setNX(key, value)) {
131131

132-
if (shouldExpireWithin(ttl)) {
133-
connection.pExpire(key, ttl.toMillis());
134-
}
132+
boolean put;
133+
if (shouldExpireWithin(ttl)) {
134+
put = connection.set(key, value, Expiration.milliseconds(ttl.toMillis()), SetOption.ifAbsent());
135+
} else {
136+
put = connection.setNX(key, value);
137+
}
138+
139+
if (put) {
135140
return null;
136141
}
137142

0 commit comments

Comments
 (0)