Skip to content

Commit cdfdc34

Browse files
committed
Log cache hit and cache miss for synchronized access
Closes gh-25248
1 parent 7bd6b8d commit cdfdc34

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ private Object execute(final CacheOperationInvoker invoker, Method method, Cache
379379
Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
380380
Cache cache = context.getCaches().iterator().next();
381381
try {
382-
return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker))));
382+
return wrapCacheValue(method, handleSynchronizedGet(invoker, key, cache));
383383
}
384384
catch (Cache.ValueRetrievalException ex) {
385385
// Directly propagate ThrowableWrapper from the invoker,
@@ -436,6 +436,22 @@ private Object execute(final CacheOperationInvoker invoker, Method method, Cache
436436
return returnValue;
437437
}
438438

439+
@Nullable
440+
private Object handleSynchronizedGet(CacheOperationInvoker invoker, Object key, Cache cache) {
441+
InvocationAwareResult invocationResult = new InvocationAwareResult();
442+
Object result = cache.get(key, () -> {
443+
invocationResult.invoked = true;
444+
if (logger.isTraceEnabled()) {
445+
logger.trace("No cache entry for key '" + key + "' in cache " + cache.getName());
446+
}
447+
return unwrapReturnValue(invokeOperation(invoker));
448+
});
449+
if (!invocationResult.invoked && logger.isTraceEnabled()) {
450+
logger.trace("Cache entry for key '" + key + "' found in cache '" + cache.getName() + "'");
451+
}
452+
return result;
453+
}
454+
439455
@Nullable
440456
private Object wrapCacheValue(Method method, @Nullable Object cacheValue) {
441457
if (method.getReturnType() == Optional.class &&
@@ -869,4 +885,13 @@ public int compareTo(CacheOperationCacheKey other) {
869885
}
870886
}
871887

888+
/**
889+
* Internal holder class for recording that a cache method was invoked.
890+
*/
891+
private static class InvocationAwareResult {
892+
893+
boolean invoked;
894+
895+
}
896+
872897
}

0 commit comments

Comments
 (0)