问题描述
我需要缓存一些异步计算的结果。详细地,为了克服这个问题,我试图使用Spring 4.3缓存和异步计算功能。
I have the need to cache some the results of some asynchronous computations. In detail, to overcome this issue, I am trying to use Spring 4.3 cache and asynchronous computation features.
作为示例,让我们看下面的代码:
As an example, let's take the following code:
@Service
class AsyncService {
@Async
@Cacheable("users")
CompletableFuture<User> findById(String usedId) {
// Some code that retrieves the user relative to id userId
return CompletableFuture.completedFuture(user);
}
}
有可能吗?我的意思是,Spring的缓存抽象将正确处理 CompletableFuture< User>
类型的对象吗?我知道具有类似的功能,但我无法理解Spring是否在正确配置的情况下使用了它。
Is it possible? I mean, will the caching abstraction of Spring handle correctly the objects of type CompletableFuture<User>
? I know that Caffeine Cache has something like that, but I can't understand if Spring uses it if properly configured.
编辑:我对 User
对象本身不感兴趣,但是对表示计算的 CompletableFuture
感兴趣。
EDIT: I am not interested in the User
object itself, but in the CompletableFuture
that represents the computation.
推荐答案
按照, ListenableFuture
( CompletableFuture
)不受支持。
这篇关于Spring @Cacheable和@Async批注的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!