From ee34f81c181479ce3ee996d76f9ed8306541d596 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Wed, 13 Nov 2024 12:25:20 +0300 Subject: [PATCH] Fixed - fixed possible race-condition in RMapCache.computeIfAbsent() method --- .../src/main/java/org/redisson/RedissonMapCache.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/redisson/src/main/java/org/redisson/RedissonMapCache.java b/redisson/src/main/java/org/redisson/RedissonMapCache.java index 5e6cc93f1..cfeb1dc2e 100644 --- a/redisson/src/main/java/org/redisson/RedissonMapCache.java +++ b/redisson/src/main/java/org/redisson/RedissonMapCache.java @@ -146,7 +146,10 @@ public class RedissonMapCache extends RedissonMap implements RMapCac if (value == null) { V newValue = mappingFunction.apply(key); if (newValue != null) { - fastPut(key, newValue, ttl.toMillis(), TimeUnit.MILLISECONDS); + V r = putIfAbsent(key, newValue, ttl.toMillis(), TimeUnit.MILLISECONDS); + if (r != null) { + return r; + } return newValue; } return null; @@ -177,7 +180,12 @@ public class RedissonMapCache extends RedissonMap implements RMapCac return CompletableFuture.supplyAsync(() -> mappingFunction.apply(key), getServiceManager().getExecutor()) .thenCompose(newValue -> { if (newValue != null) { - return fastPutAsync(key, newValue, ttl.toMillis(), TimeUnit.MILLISECONDS).thenApply(rr -> newValue); + return putIfAbsentAsync(key, newValue, ttl.toMillis(), TimeUnit.MILLISECONDS).thenApply(rr -> { + if (rr != null) { + return rr; + } + return newValue; + }); } return CompletableFuture.completedFuture(null); });