Both answers from @Biju and @vsingh are correct; but I would like to add another alternative, if the Bar object you are trying to cache is complex or the foo method contains a large number of parameters using SpEL, it may not be the ideal solution for generating a key.
Alternatively, you can consider keyGenerator .
Example:
@Override @Cacheable(value="barCahceKey", keyGenerator="barKeyGenerator") public int foo(Bar bar) { .... } @Component public class BarKeyGenerator implements KeyGenerator { @Override public Object generate(Object o, Method method, Object... objects) {
With this approach, you have complete flexibility in building the key.
KeyGenerator API
Ithar source share