如下面代码所示,@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);
    }

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐