io.quarkus.cache.runtime.CacheInvalidateInterceptor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of quarkus-cache Show documentation
Show all versions of quarkus-cache Show documentation
Enable application data caching in CDI beans
package io.quarkus.cache.runtime;
import java.util.List;
import java.util.function.Function;
import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import org.jboss.logging.Logger;
import io.quarkus.cache.Cache;
import io.quarkus.cache.CacheException;
import io.quarkus.cache.CacheInvalidate;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
@CacheInvalidate(cacheName = "") // The `cacheName` attribute is @Nonbinding.
@Interceptor
@Priority(CacheInterceptor.BASE_PRIORITY + 1)
public class CacheInvalidateInterceptor extends CacheInterceptor {
private static final Logger LOGGER = Logger.getLogger(CacheInvalidateInterceptor.class);
private static final String INTERCEPTOR_BINDINGS_ERROR_MSG = "The Quarkus cache extension is not working properly (CacheInvalidate interceptor bindings retrieval failed), please create a GitHub issue in the Quarkus repository to help the maintainers fix this bug";
@AroundInvoke
public Object intercept(InvocationContext invocationContext) throws Exception {
CacheInterceptionContext interceptionContext = getInterceptionContext(invocationContext,
CacheInvalidate.class, true);
if (interceptionContext.getInterceptorBindings().isEmpty()) {
// This should never happen.
LOGGER.warn(INTERCEPTOR_BINDINGS_ERROR_MSG);
return invocationContext.proceed();
}
ReturnType returnType = determineReturnType(invocationContext.getMethod().getReturnType());
if (returnType == ReturnType.NonAsync) {
return invalidateBlocking(invocationContext, interceptionContext);
} else {
return invalidateNonBlocking(invocationContext, interceptionContext, returnType);
}
}
private Object invalidateNonBlocking(InvocationContext invocationContext,
CacheInterceptionContext interceptionContext,
ReturnType returnType) {
LOGGER.trace("Invalidating cache entries in a non-blocking way");
var uni = Multi.createFrom().iterable(interceptionContext.getInterceptorBindings())
.onItem().transformToUniAndMerge(new Function>() {
@Override
public Uni apply(CacheInvalidate binding) {
return invalidate(binding, interceptionContext.getCacheKeyParameterPositions(), invocationContext);
}
})
.onItem().ignoreAsUni()
.onItem().transformToUni(new Function
© 2015 - 2025 Weber Informatics LLC | Privacy Policy