Cache operation invocation hook point
This commit adds a invokeOperation protected method in case one needs a hook point in the way the underlying cache method is invoked, and how exceptions that might be thrown by that invocation are handled. Issue: SPR-11540
This commit is contained in:
@@ -284,6 +284,20 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
||||
return invoker.invoke();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the underlying operation (typically in case of cache miss) and return
|
||||
* the result of the invocation. If an exception occurs it will be wrapped in
|
||||
* a {@link CacheOperationInvoker.ThrowableWrapper}: the exception can be handled
|
||||
* or modified but it <em>must</em> be wrapped in a
|
||||
* {@link CacheOperationInvoker.ThrowableWrapper} as well.
|
||||
* @param invoker the invoker handling the operation being cached
|
||||
* @return the result of the invocation
|
||||
* @see CacheOperationInvoker#invoke()
|
||||
*/
|
||||
protected Object invokeOperation(CacheOperationInvoker invoker) {
|
||||
return invoker.invoke();
|
||||
}
|
||||
|
||||
private Class<?> getTargetClass(Object target) {
|
||||
Class<?> targetClass = AopProxyUtils.ultimateTargetClass(target);
|
||||
if (targetClass == null && target != null) {
|
||||
@@ -314,7 +328,7 @@ public abstract class CacheAspectSupport extends AbstractCacheInvoker
|
||||
|
||||
// Invoke the method if don't have a cache hit
|
||||
if (result == null) {
|
||||
result = new SimpleValueWrapper(invoker.invoke());
|
||||
result = new SimpleValueWrapper(invokeOperation(invoker));
|
||||
}
|
||||
|
||||
// Collect any explicit @CachePuts
|
||||
|
||||
@@ -19,9 +19,10 @@ package org.springframework.cache.interceptor;
|
||||
/**
|
||||
* Abstract the invocation of a cache operation.
|
||||
*
|
||||
* <p>Provide a special exception that can be used to indicate that the
|
||||
* underlying invocation has thrown a checked exception, allowing the
|
||||
* callers to threat these in a different manner if necessary.
|
||||
* <p>Does not provide a way to transmit checked exceptions but
|
||||
* provide a special exception that should be used to wrap any
|
||||
* exception that was thrown by the underlying invocation. Callers
|
||||
* are expected to handle this issue type specifically.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @since 4.1
|
||||
@@ -29,11 +30,11 @@ package org.springframework.cache.interceptor;
|
||||
public interface CacheOperationInvoker {
|
||||
|
||||
/**
|
||||
* Invoke the cache operation defined by this instance. Can throw a
|
||||
* {@link ThrowableWrapper} if that operation wants to explicitly
|
||||
* indicate that a checked exception has occurred.
|
||||
* Invoke the cache operation defined by this instance. Wraps any
|
||||
* exception that is thrown during the invocation in a
|
||||
* {@link ThrowableWrapper}.
|
||||
* @return the result of the operation
|
||||
* @throws ThrowableWrapper if a checked exception has been thrown
|
||||
* @throws ThrowableWrapper if an error occurred while invoking the operation
|
||||
*/
|
||||
Object invoke() throws ThrowableWrapper;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user