I have a service that takes in a DTO and returns some result:
@Override
public int foo(Bar bar) {
....
}
Bar is as follows (simplified):
public class Bar {
public int id;
public String name;
public String baz;
@Override
public int hashCode() {
//this is already being defined for something else
...
}
@Override
public boolean equals(Object o) {
//this is already being defined for something else
...
}
}
I want to use @Cacheable on the foo method; however, I want to hash on the id and name properties, but not baz. Is there a way to do this?
You can use this approach also
@Override
@Cacheable(key="{#bar.name, #bar.id}")
public int foo(Bar bar) {
....
}
It is suggested not to use hashcode as keys @Cacheable key on multiple method arguments
Yes, you can specify using a Spring-EL expression along these lines:
@Override
@Cacheable(key="#bar.name.concat('-').concat(#bar.id)")
public int foo(Bar bar) {
....
}
or define a modified hashCode on bar and call that:
@Override
@Cacheable(key="#bar.hashCodeWithIdName")
public int foo(Bar bar) {
....
}
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