How can I use @CachePut
on an object, and update a cache by multiple properties of it?
Example:
Cached persons are added to PERSONS
cache every time the 'findOneByFirstnameAndLastname()` method is invoked.
If the Person
object is persisted, I also want the cache to update the person. But how can I tell @CachePut
to use both firstname+lastname
as the key for the PERSONS
cache? Right now the save()
method does not update the cache...
public interface PersonRepository extends CrudRepository<Person, Long> {
//assume there is only one valid person
@Cachable("PERSONS")
Person findOneByFirstnameAndLastname(String firstname, String lastname);
//TODO how to update cache by entity.firstname + entity.lastname
@CachePut("PERSONS")
@Override
Person save(Person entity);
}
At the end I succeeded in using reference to the parameters (#p1, #2
) during lookip and reference to #result.*
during persist:
@Cacheable(cacheNames = "PERSONS", key = "#p1 + #p2")
Person findByFirstnameAndLastName(String firstname, String lastname);
@CachePut(cacheNames = "PERSONS", key = "#result.firstName + #result.lastName")
Person save(Person person);
Yet I don't know why I could not use #firstname + #lastname
or #person.firstname + #person.lastname
, but Spring constantly complains about having null
parameters then. Maybe Spring cannot resolve the parameter names at this stage, whysoever.