Merge branch '5.2.x'

This commit is contained in:
Stephane Nicoll 2020-09-02 11:29:25 +02:00
commit a06deac5db
1 changed files with 26 additions and 1 deletions

View File

@ -379,7 +379,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT); Object key = generateKey(context, CacheOperationExpressionEvaluator.NO_RESULT);
Cache cache = context.getCaches().iterator().next(); Cache cache = context.getCaches().iterator().next();
try { try {
return wrapCacheValue(method, cache.get(key, () -> unwrapReturnValue(invokeOperation(invoker)))); return wrapCacheValue(method, handleSynchronizedGet(invoker, key, cache));
} }
catch (Cache.ValueRetrievalException ex) { catch (Cache.ValueRetrievalException ex) {
// Directly propagate ThrowableWrapper from the invoker, // Directly propagate ThrowableWrapper from the invoker,
@ -436,6 +436,22 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
return returnValue; 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 @Nullable
private Object wrapCacheValue(Method method, @Nullable Object cacheValue) { private Object wrapCacheValue(Method method, @Nullable Object cacheValue) {
if (method.getReturnType() == Optional.class && 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;
}
} }