diff --git a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java index 92c39cd029..4d7d8564d2 100644 --- a/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java +++ b/spring-context/src/main/java/org/springframework/cache/interceptor/CacheAspectSupport.java @@ -379,7 +379,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT); Cache cache = context.getCaches().iterator().next(); try { - return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker)))); + return wrapCacheValue(method, handleSynchronizedGet(invoker, key, cache)); } catch (Cache.ValueRetrievalException ex) { // Directly propagate ThrowableWrapper from the invoker, @@ -436,6 +436,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker return returnValue; } + @Nullable + private Object handleSynchronizedGet(CacheOperationInvoker invoker, Object key, Cache cache) { + InvocationAwareResult invocationResult = new InvocationAwareResult(); + Object result = cache.get(key, () -> { + invocationResult.invoked = true; + if (logger.isTraceEnabled()) { + logger.trace("No cache entry for key '" + key + "' in cache " + cache.getName()); + } + return unwrapReturnValue(invokeOperation(invoker)); + }); + if (!invocationResult.invoked && logger.isTraceEnabled()) { + logger.trace("Cache entry for key '" + key + "' found in cache '" + cache.getName() + "'"); + } + return result; + } + @Nullable private Object wrapCacheValue(Method method, @Nullable Object cacheValue) { if (method.getReturnType() == Optional.class && @@ -869,4 +885,13 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker } } + /** + * Internal holder class for recording that a cache method was invoked. + */ + private static class InvocationAwareResult { + + boolean invoked; + + } + }