如下面代码所示,@Cacheable注解的unless属性已经为我们提供好了解决方案。
unless = "#result == null" 的意思就是,当返回值为null时,就不缓存
@Cacheable(cacheNames = {"single_book"},key = "#root.targetClass+'.'+#root.methodName+'.'+#p0",
unless = "#result == null")
public Book getBook(Long id){
return bookMapper.selectBookById(id);
}