DATAREDIS-1195 - Make putIfAbsent atomic when TTL is set.

Original pull request: #553.
This commit is contained in:
Andre Prata
2020-07-30 15:59:19 +01:00
committed by Mark Paluch
parent 406e030d92
commit c4a320cc3c

View File

@@ -45,6 +45,7 @@ import org.springframework.util.Assert;
*
* @author Christoph Strobl
* @author Mark Paluch
* @author André Prata
* @since 2.0
*/
class DefaultRedisCacheWriter implements RedisCacheWriter {
@@ -127,11 +128,15 @@ class DefaultRedisCacheWriter implements RedisCacheWriter {
}
try {
if (connection.setNX(key, value)) {
if (shouldExpireWithin(ttl)) {
connection.pExpire(key, ttl.toMillis());
}
boolean put;
if (shouldExpireWithin(ttl)) {
put = connection.set(key, value, Expiration.milliseconds(ttl.toMillis()), SetOption.ifAbsent());
} else {
put = connection.setNX(key, value);
}
if (put) {
return null;
}