我正在尝试使用@Cachable 和@CacheEvict 通过spring 框架管理redis 缓存。可以在类的私有(private)方法上添加 @CacheEvict 吗?
最佳答案
@Cacheable
仅在 bean 之间调用时才被评估,即使对于公共(public)方法也是如此。即为此:
public class MyBean {
@Cacheable
public String getString(int i) {
return Integer.toString(i);
}
public void myOtherMethod() {
String myString = getString(2);
}
}
不会触发缓存。
因此,用@Cacheable
声明一个private
方法是没有意义的。
请注意,方面也是如此(如其他解决方案中所建议的);这些在调用类内方法时也不会触发。
关于java - @CacheEvict 注释是否适用于私有(private)方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47131569/