I am trying to use @Cachable and @CacheEvict for managing redis cache via spring framework. Can one add @CacheEvict on private methods of the class?
@Cacheable is only evaluated when called between beans, even for public methods. Ie for this:
public class MyBean {
@Cacheable
public String getString(int i) {
return Integer.toString(i);
}
public void myOtherMethod() {
String myString = getString(2);
}
}
the caching will not be triggered.
Therefore, it does not make sense to declare a private method with @Cacheable.
Note that the same is true for Aspects (as suggested in the other solution); those are not triggered when calling intra-class methods either.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With