美文网首页
SpringBoot中@CacheEvict在同一个类中的方法调

SpringBoot中@CacheEvict在同一个类中的方法调

作者: 树蜂 | 来源:发表于2019-03-05 19:54 被阅读0次

在使用Spring @CacheEvict注解的时候,要注意,如果类A的方法f()被标注了@Cacheable注解,那么当类A的其他方法,例如:f2(),去直接调用f()的时候,@Cacheable是不起作用的,原因是@Cacheable是基于Spring AOP代理类,f2()属于内部方法,直接调用f()时,是不走代理的。举个例子:

不生效:

@Override
public Result saveEntity(Entity entity) {
  try {
    mapper.insert(entity);
    //Cacheable 不生效
    this.flushCacheable();
  }catch(Exception e){
    e.printStackTrace();
    return Result.fail(StatusMappingEnum.FAIL);
  }
}
@CacheEvict(value = "demand_minning3-comment~keys" , allEntries = true)
public void flushCacheable() {
}

正确使用:

@Override
@CacheEvict(value = "demand_minning3_comment" , allEntries = true)
public Result saveEntity(Entity entity) {
  try {
    mapper.insert(entity);
  }catch(Exception e){
    e.printStackTrace();
    return Result.fail(StatusMappingEnum.FAIL);
  }
}

SpringBoot中@Cacheable,@CacheEvict,@CachePut在同一个类中方法调用不起作用

相关文章

网友评论

      本文标题:SpringBoot中@CacheEvict在同一个类中的方法调

      本文链接:https://www.haomeiwen.com/subject/cgxauqtx.html