考虑一个可以插入和检索对象并使用 Spring 缓存抽象的服务类,我如何以返回 Optional 的方式注释方法?
class MyServiceImpl implements MyService {
private static final String CACHE_NAME = "itemCache";
@Override
@Cacheable(CACHE_NAME)
public Optional<Item> findById(Long id) {
// access the repository to retrieve the item
}
@Override
@CachePut(cacheNames = CACHE_NAME, key = "#item.id")
public Item insertItem(Item item) {
...
}
}
在上面的例子中,一个ClassCastException
被抛出是因为insertItem
在缓存中放入了一个Item
实例,并且findById
需要一个可能包含 Item
实例的 Optional
。
最佳答案
只是对评论的跟进,以给出明确的答案。我们从 Spring Framework 4.3 RC2 开始做
关于java - 使用 Spring 缓存处理 Java 8 Optional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36844270/