Affects: 6.1.1


Project is using spring-boot:3.2.0 and spring-boot-starter-data-redis-reactive:3.2.0, and the whole project is implemented in a reactive manner using webFlux. For service methods I want to cache the result by adding the following annotation: @Cacheable(cacheNames = "Something", keyGenerator = "userKeyGenerator", unless = "#result == null") The default keyGenerator works fine, which can return method parameters etc. However, for some methods I would like to add user credentials to the cache key as well, for example:

public class UserKeyGenerator extends SimpleKeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        Object[] args = Arrays.copyOf(params, params.length + 1);
        Mono<SecurityContext> context = ReactiveSecurityContext.getContext();
        return context.map(context -> {
            args[params.length] = context.getAuthentication().getCredentials();
            return generateKey(args);
        });
    }
}

Ideally what I would like to achieve is some cache keys following the format: "Something::SimpleKey [...(method params), userCredentials]", just adding another element at the end of the cache and the remaining part which contains method params is the same as default one. The problem is the returned key is "Something::MonoMapFuseable"* which basically makes the key itself not explainable. Does customized keyGenerator support Mono/Flux type?

Comment From: jhoeller

I'm afraid this is not supported by design since our asynchronous cache contract for the common cache providers out there requires an immediately available key even for an asynchronous operation. In other words, only the value retrieval is asynchronous, not the key determination.

Generally speaking, our declarative caching support for reactive types is largely best-effort. For custom scenarios it is often recommendable to perform the caching in your reactive pipeline directly, not using @Cacheable at all.