Search in sources :

Example 1 with CacheOperationInvoker

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);
    }
}
Also used : CacheOperationInvocationContext(org.springframework.cache.interceptor.CacheOperationInvocationContext) BasicOperation(org.springframework.cache.interceptor.BasicOperation) CacheOperationInvoker(org.springframework.cache.interceptor.CacheOperationInvoker) Nullable(org.springframework.lang.Nullable)

Example 2 with CacheOperationInvoker

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();
    }
}
Also used : Method(java.lang.reflect.Method) CacheOperationInvoker(org.springframework.cache.interceptor.CacheOperationInvoker) Nullable(org.springframework.lang.Nullable)

Example 3 with CacheOperationInvoker

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);
}
Also used : Method(java.lang.reflect.Method) CacheOperationInvoker(org.springframework.cache.interceptor.CacheOperationInvoker) Test(org.junit.jupiter.api.Test)

Example 4 with CacheOperationInvoker

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;
    }
}
Also used : CacheOperationInvoker(org.springframework.cache.interceptor.CacheOperationInvoker) Cache(org.springframework.cache.Cache) Nullable(org.springframework.lang.Nullable)

Aggregations

CacheOperationInvoker (org.springframework.cache.interceptor.CacheOperationInvoker)4 Nullable (org.springframework.lang.Nullable)3 Method (java.lang.reflect.Method)2 Test (org.junit.jupiter.api.Test)1 Cache (org.springframework.cache.Cache)1 BasicOperation (org.springframework.cache.interceptor.BasicOperation)1 CacheOperationInvocationContext (org.springframework.cache.interceptor.CacheOperationInvocationContext)1