use of org.springframework.cache.interceptor.CacheOperationInvoker in project spring-framework by spring-projects.
the class JCacheAspectSupport method execute.
@SuppressWarnings("unchecked")
@Nullable
private Object execute(CacheOperationInvocationContext<?> context, CacheOperationInvoker invoker) {
CacheOperationInvoker adapter = new CacheOperationInvokerAdapter(invoker);
BasicOperation operation = context.getOperation();
if (operation instanceof CacheResultOperation) {
Assert.state(this.cacheResultInterceptor != null, "No CacheResultInterceptor");
return this.cacheResultInterceptor.invoke((CacheOperationInvocationContext<CacheResultOperation>) context, adapter);
} else if (operation instanceof CachePutOperation) {
Assert.state(this.cachePutInterceptor != null, "No CachePutInterceptor");
return this.cachePutInterceptor.invoke((CacheOperationInvocationContext<CachePutOperation>) context, adapter);
} else if (operation instanceof CacheRemoveOperation) {
Assert.state(this.cacheRemoveEntryInterceptor != null, "No CacheRemoveEntryInterceptor");
return this.cacheRemoveEntryInterceptor.invoke((CacheOperationInvocationContext<CacheRemoveOperation>) context, adapter);
} else if (operation instanceof CacheRemoveAllOperation) {
Assert.state(this.cacheRemoveAllInterceptor != null, "No CacheRemoveAllInterceptor");
return this.cacheRemoveAllInterceptor.invoke((CacheOperationInvocationContext<CacheRemoveAllOperation>) context, adapter);
} else {
throw new IllegalArgumentException("Cannot handle " + operation);
}
}
use of org.springframework.cache.interceptor.CacheOperationInvoker in project spring-framework by spring-projects.
the class JCacheInterceptor method invoke.
@Override
@Nullable
public Object invoke(final MethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
CacheOperationInvoker aopAllianceInvoker = () -> {
try {
return invocation.proceed();
} catch (Throwable ex) {
throw new CacheOperationInvoker.ThrowableWrapper(ex);
}
};
Object target = invocation.getThis();
Assert.state(target != null, "Target must not be null");
try {
return execute(aopAllianceInvoker, target, method, invocation.getArguments());
} catch (CacheOperationInvoker.ThrowableWrapper th) {
throw th.getOriginal();
}
}
use of org.springframework.cache.interceptor.CacheOperationInvoker in project spring-framework by spring-projects.
the class JCacheInterceptorTests method cacheResultReturnsProperType.
@Test
public void cacheResultReturnsProperType() throws Throwable {
JCacheInterceptor interceptor = createInterceptor(createOperationSource(cacheManager, defaultCacheResolver, defaultExceptionCacheResolver, defaultKeyGenerator));
AnnotatedJCacheableService service = new AnnotatedJCacheableService(cacheManager.getCache("default"));
Method method = ReflectionUtils.findMethod(AnnotatedJCacheableService.class, "cache", String.class);
CacheOperationInvoker invoker = new DummyInvoker(0L);
Object execute = interceptor.execute(invoker, service, method, new Object[] { "myId" });
assertThat(execute).as("result cannot be null.").isNotNull();
assertThat(execute.getClass()).as("Wrong result type").isEqualTo(Long.class);
assertThat(execute).as("Wrong result").isEqualTo(0L);
}
use of org.springframework.cache.interceptor.CacheOperationInvoker in project spring-framework by spring-projects.
the class CacheResultInterceptor method invoke.
@Override
@Nullable
protected Object invoke(CacheOperationInvocationContext<CacheResultOperation> context, CacheOperationInvoker invoker) {
CacheResultOperation operation = context.getOperation();
Object cacheKey = generateKey(context);
Cache cache = resolveCache(context);
Cache exceptionCache = resolveExceptionCache(context);
if (!operation.isAlwaysInvoked()) {
Cache.ValueWrapper cachedValue = doGet(cache, cacheKey);
if (cachedValue != null) {
return cachedValue.get();
}
checkForCachedException(exceptionCache, cacheKey);
}
try {
Object invocationResult = invoker.invoke();
doPut(cache, cacheKey, invocationResult);
return invocationResult;
} catch (CacheOperationInvoker.ThrowableWrapper ex) {
Throwable original = ex.getOriginal();
cacheException(exceptionCache, operation.getExceptionTypeFilter(), cacheKey, original);
throw ex;
}
}
Aggregations