Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does @CacheEvict annotation work on private methods?

Tags:

java

spring

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?

like image 400
enator Avatar asked Sep 08 '26 09:09

enator


1 Answers

@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.

like image 54
daniu Avatar answered Sep 11 '26 05:09

daniu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!