I use Spring data Redis in order to cache serialized JPA entities in Redis using org.springframework.data.redis.cache.RedisCacheManager
Here is the method:
@Override
@Cacheable(value = MapCacheConfiguration.DATABASE_CACHE_NAME, key = "#root.method.name")
public Curriculum findCurriculumByMemberId(Long memberId) {
return curriculumRepository.findCurriculumByMemberId(memberId);
}
Unfortunately, upon restart of my boot application, the entities are still cached in Redis and I get a org.hibernate.LazyInitializationException
This might be for the reason described in this post i.e. access to a detached object by hibernate - in my case the serialized object left around in the cache.
Can someone please advise a strategy for dealing with this problem?
- Should I clean/empty the cache upon destroy of my app bearing in mind the process of repopulating the cache is expensive and the app is going to be hosted in the cloud (Heroku) where the dynos/containers are destroyed and recreated (and therefore restarted) quite frequently.
- Or is there a way to reattach to the cached entities to the entity manager?
- Is there a better solution?