Skip to content

Commit

Permalink
FIX: Read and return value from cache server when putIfAbsent has fai…
Browse files Browse the repository at this point in the history
…led if wantToGetException is false.
  • Loading branch information
uhm0311 committed Jun 19, 2024
1 parent f6894fd commit 7f6edfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

import org.springframework.beans.factory.InitializingBean;
import org.springframework.cache.support.AbstractValueAdaptingCache;
import org.springframework.cache.support.SimpleValueWrapper;
import org.springframework.util.Assert;
import org.springframework.util.DigestUtils;

Expand Down Expand Up @@ -215,7 +216,8 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
logger.debug("trying to add key: {}", arcusKey);

if (value == null) {
throw new IllegalArgumentException("arcus cannot add NULL value. key: " + arcusKey);
logger.info("arcus cannot putIfAbsent NULL value. key: {}", arcusKey);
return toValueWrapper(lookup(key));
}

try {
Expand All @@ -240,7 +242,7 @@ public ValueWrapper putIfAbsent(Object key, Object value) {
throw toRuntimeException(e);
}
logger.info("failed to putIfAbsent. error: {}, key: {}", e.getMessage(), arcusKey);
return null;
return toValueWrapper(lookup(key));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1306,12 +1306,14 @@ public void testPutIfAbsent_FrontCache_FutureException_ForceFrontCaching() {
}

@Test
@SuppressWarnings("deprecation")
public void testPutIfAbsent_FrontCache_Null() {
// given
IllegalArgumentException exception = null;
Exception exception = null;
arcusCache.setArcusFrontCache(arcusFrontCache);
arcusCache.setExpireSeconds(EXPIRE_SECONDS);
arcusCache.setFrontExpireSeconds(FRONT_EXPIRE_SECONDS);
arcusCache.setWantToGetException(true);
when(arcusClientPool.add(arcusKey, EXPIRE_SECONDS, VALUE))
.thenReturn(createOperationFuture(true));
when(arcusClientPool.asyncGet(arcusKey))
Expand All @@ -1320,18 +1322,18 @@ public void testPutIfAbsent_FrontCache_Null() {
// when
try {
arcusCache.putIfAbsent(ARCUS_STRING_KEY, null);
} catch (IllegalArgumentException e) {
} catch (Exception e) {
exception = e;
}

// then
verify(arcusClientPool, never())
.add(arcusKey, EXPIRE_SECONDS, VALUE);
verify(arcusClientPool, never())
verify(arcusClientPool, times(1))
.asyncGet(arcusKey);
verify(arcusFrontCache, never())
verify(arcusFrontCache, times(1))
.set(arcusKey, VALUE, FRONT_EXPIRE_SECONDS);
assertNotNull(exception);
assertNull(exception);
}

private static GetFuture<Object> createGetFuture(
Expand Down

0 comments on commit 7f6edfb

Please sign in to comment.