我正在尝试将我的项目迁移到,并且我不知道如何处理缓存。
我原来的方法是这样的
@Transactional
@CacheResult(cacheName = "subject-cache")
public Subject getSubject(@CacheKey String subjectId) throws Exception {
return subjectRepository.findByIdentifier(subjectId);
}
主题由缓存键"subjectId“加载,如果可用,则从缓存中加载。
迁移到穆特尼会像这样
@CacheResult(cacheName = "subject-cache")
public Uni<Subject> getSubject(@CacheKey String subjectId) {
return subjectRepository.findByIdentifier(subjectId);
}
但是,将Uni对象存储在缓存中是不对的。
还有将缓存作为bean注入的选项,但是,回退函数不支持返回Uni:
@Inject
@CacheName("subject-cache")
Cache cache;
//does not work, cache.get function requires return type Subject, not Uni<Subject>
public Uni<Subject> getSubject(String subjectId) {
return cache.get(subjectId, s -> subjectRepository.findByIdentifier(subjectId));
}
//This works, needs blocking call to repo, to return response wrapped in new Uni
public Uni<Subject> getSubject(String subjectId) {
return cache.get(subjectId, s -> subjectRepository.findByIdentifier(subjectId).await().indefinitely());
}
@CacheResult注解能否与Uni / Multi一起使用,并且一切都能正确地在引擎盖下处理?
发布于 2022-01-28 07:57:56
在返回@CacheResult
的方法上使用Uni
的示例实际上应该有效。实现将自动“剥离”Uni
类型,并且只将Subject
存储在缓存中。
https://stackoverflow.com/questions/70890176
复制相似问题