Spring Cacheable注解不缓存null值
2017-03-20 13:44
429 查看
今天,用Cacheable注解时,发现空值,也会被缓存下来。下次另一个系统如果更新了值,这边从缓存取,还是空值,会有问题。所以一方面,希望另一个系统更新时,能清缓存,另一方面不希望缓存中有太多垃圾数据。
参考http://stackoverflow.com/questions/12113725/how-do-i-tell-spring-cache-not-to-cache-null-value-in-cacheable-annotation的做法,对程序做修改
这样,当方法返回空值时,就不会被缓存起来。见unless解释:
大意是使用result的值,来决定是否来否定方法缓存。因此可以用来做条件判断
参考http://stackoverflow.com/questions/12113725/how-do-i-tell-spring-cache-not-to-cache-null-value-in-cacheable-annotation的做法,对程序做修改
@Cacheable(key = "''+#id", unless="#result == null") public XXXPO get(int id) { //get from db }
这样,当方法返回空值时,就不会被缓存起来。见unless解释:
/** * Spring Expression Language (SpEL) attribute used to veto method caching. * <p>Unlike {@link #condition()}, this expression is evaluated after the method * has been called and can therefore refer to the {@code result}. Default is "", * meaning that caching is never vetoed. * @since 3.2 */
大意是使用result的值,来决定是否来否定方法缓存。因此可以用来做条件判断
相关文章推荐
- Spring中缓存注解@Cacheable、@CachePut、@CacheEvict 注释介绍
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解@Cacheable的用法
- Spring 缓存注解@Cacheable的用法
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring 缓存注解(@Cacheable、@CachePut、@CacheEvict)使用
- SpringBoot使用Redis做缓存,@Cacheable、@CachePut、@CacheEvict等注解的使用
- Spring缓存注解@Cacheable,@CachePut , @CacheEvict使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用
- Redis缓存实践:自定义注解、缓存工具类、基于Spring注解@cacheable