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 55ef261705
commit 2db93e969e

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 {
@@ -153,12 +154,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) {
statistics.incPuts(name);
return null;
}