spring全家桶中@CacheEvict⽆效情况?
@CacheEvict(value =“test”, allEntries = true)
1、使⽤@CacheEvict注解的⽅法必须是controller层直接调⽤,service⾥间接调⽤不⽣效。
2、原因是因为key值跟你查询⽅法的key值不统⼀,所以导致缓存并没有清除
3、把@CacheEvict的⽅法和@Cache的⽅法放到⼀个java⽂件中写,他俩在两个java⽂件的话,会导致
@CacheEvict失效。
4、返回值必须设置为void
It is important to note that void methods can be used with @CacheEvict
5、@CacheEvict必须作⽤在⾛代理的⽅法上
在使⽤Spring @CacheEvict注解的时候,要注意,如果类A的⽅法f1()被标注了 @CacheEvict注解,那么当类A的其他⽅法,例如:
f2(),去直接调⽤f1()的时候, @CacheEvict是不起作⽤的,原因是 @CacheEvict是基于Spring AOP代理类,f2()属于内部⽅法,直接调⽤f1()时,是不⾛代理的。
举个例⼦:
不⽣效:
@Override
public void saveEntity(Menu menu){
try{
mapper.insert(menu);
//Cacheable 不⽣效
}catch(Exception e){
e.printStackTrace();
}
}
@CacheEvict(value ="test", allEntries =true)
public void test(){
}
正确使⽤:
cacheable@Override
@CacheEvict(value ="test", allEntries =true)
public void saveEntity(Menu menu){
try{
mapper.insert(menu);
}catch(Exception e){
e.printStackTrace();
}
}