Skip to content

Commit

Permalink
Log cache hit and cache miss for synchronized access
Browse files Browse the repository at this point in the history
Closes gh-25248
  • Loading branch information
snicoll committed Sep 2, 2020
1 parent 7bd6b8d commit cdfdc34
Showing 1 changed file with 26 additions and 1 deletion.
Expand Up @@ -379,7 +379,7 @@ private Object execute(final CacheOperationInvoker invoker, Method method, Cache
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,
Expand Down Expand Up @@ -436,6 +436,22 @@ private Object execute(final CacheOperationInvoker invoker, Method method, Cache
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 &&
Expand Down Expand Up @@ -869,4 +885,13 @@ public int compareTo(CacheOperationCacheKey other) {
}
}

/**
* Internal holder class for recording that a cache method was invoked.
*/
private static class InvocationAwareResult {

boolean invoked;

}

}

0 comments on commit cdfdc34

Please sign in to comment.