Skip to content

Commit

Permalink
REFACTOR: use put method when get synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
oliviarla authored and jhpark816 committed Jul 8, 2024
1 parent 2bc30d4 commit a62d14b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 27 deletions.
28 changes: 10 additions & 18 deletions src/main/java/com/navercorp/arcus/spring/cache/ArcusCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -157,32 +157,24 @@ public <T> T get(Object key, Callable<T> valueLoader) {
@Nullable
@SuppressWarnings("unchecked")
private <T> T getSynchronized(Object key, Callable<T> valueLoader) {
String arcusKey = createArcusKey(key);
try {
acquireWriteLockOnKey(arcusKey);
acquireWriteLockOnKey(key);
ValueWrapper result = super.get(key);
return result != null ? (T) result.get() : loadValue(arcusKey, valueLoader);
return result != null ? (T) result.get() : loadValue(key, valueLoader);
} finally {
releaseWriteLockOnKey(arcusKey);
releaseWriteLockOnKey(key);
}
}

private <T> T loadValue(String arcusKey, Callable<T> valueLoader) {
private <T> T loadValue(Object key, Callable<T> valueLoader) {
T value;
try {
value = valueLoader.call();
} catch (Exception e) {
throw new ValueRetrievalException(arcusKey, valueLoader, e);
throw new ValueRetrievalException(key, valueLoader, e);
}

try {
putValue(arcusKey, value);
} catch (Exception e) {
if (wantToGetException) {
throw toRuntimeException(e);
}
logger.info("failed to put value got from valueLoader. error: {}, key: {}", e.getMessage(), arcusKey);
}
put(key, value);

return value;
}
Expand Down Expand Up @@ -432,12 +424,12 @@ public boolean getForceFrontCaching() {
return forceFrontCaching;
}

private void acquireWriteLockOnKey(String arcusKey) {
keyLockProvider.getLockForKey(arcusKey).writeLock().lock();
private void acquireWriteLockOnKey(Object key) {
keyLockProvider.getLockForKey(key).writeLock().lock();
}

private void releaseWriteLockOnKey(String arcusKey) {
keyLockProvider.getLockForKey(arcusKey).writeLock().unlock();
private void releaseWriteLockOnKey(Object key) {
keyLockProvider.getLockForKey(key).writeLock().unlock();
}

private RuntimeException toRuntimeException(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,7 @@ public void testGetValueLoader_Get_CacheHit() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -817,7 +817,7 @@ public void testGetValueLoader_Get_SecondCacheHit() throws Exception {
.thenReturn(createOperationFutureException());
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -845,7 +845,7 @@ public void testGetValueLoader_Get_CacheMiss() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -941,7 +941,7 @@ public void testGetValueLoader_Get_SecondException() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -975,7 +975,7 @@ public void testGetValueLoader_Get_SecondFutureException() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -1009,7 +1009,7 @@ public void testGetValueLoader_Put_Exception() throws Exception {
.thenThrow(new TestException());
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -1043,7 +1043,7 @@ public void testGetValueLoader_Put_FutureException() throws Exception {
.thenReturn(createOperationFutureException());
when(valueLoader.call())
.thenReturn(VALUE);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -1075,7 +1075,7 @@ public void testGetValueLoader_ValueLoader_Null() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenReturn(null);
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down Expand Up @@ -1103,7 +1103,7 @@ public void testGetValueLoader_ValueLoader_Exception() throws Exception {
.thenReturn(createOperationFuture(true));
when(valueLoader.call())
.thenThrow(new TestException());
when(keyLockProvider.getLockForKey(arcusKey))
when(keyLockProvider.getLockForKey(ARCUS_STRING_KEY))
.thenReturn(readWriteLock);
when(readWriteLock.writeLock())
.thenReturn(lock);
Expand Down

0 comments on commit a62d14b

Please sign in to comment.