Below is my code snippet. @cacheput only inserts data onto redis on the first call. Does not update the value the second time save function is called. CacheManager in reference is a RedisCacheManager.
@CachePut(cacheNames = "User", key = "#user.Id")
@Override
public Optional<User> save(User user) {
if(em.contains(user) || user.isPersisted()) { // merge if exists
User retVal = em.merge(user);
retVal.setPersisted(true);
System.out.println("hashCode after merge-->"+retVal.hashCode());
return Optional.ofNullable(retVal);
} else {
em.persist(user);
user.setPersisted(true);
return Optional.ofNullable(user);
}
}
Dependency being used :
<dependency>
<groupId>biz.paluch.redis</groupId>
<artifactId>lettuce</artifactId>
<version>4.3.2.Final</version>
</dependency>
I did verify the hashcodes for the object to be updated and the object inserted during first save, they are different.