I am working on a second-level Hazelcast cache. The cache is working perfectly fine with the findAll method but when I am trying to update existing data or adding new data and again trying to gather all data using the findAll method it gives old records not updated one. Here I Attached my code. The highlight is when I'm trying to fetch data using the findById method it gives me updated data from the cache itself. I don't want to use @CacheEvit.
@Override
@CachePut(cacheNames = "cache",key="#profileDTO.id")
public ProfileDTO save(ProfileDTO profileDTO) {
log.debug("Request to save Profile : {}", profileDTO);
Profile profile = profileMapper.toEntity(profileDTO);
profile = profileRepository.save(profile);
return profileMapper.toDto(profile);
}
/**
* Get all the profiles.
*
* @return the list of entities.
*/
@Override
@Cacheable(cacheNames = "cache")
public List<ProfileDTO> findAll() {
log.debug("Request to get all Profiles");
return profileRepository.findAll().stream()
.map(profileMapper::toDto)
.collect(Collectors.toCollection(LinkedList::new));
}
/**
* Get one profile by id.
*
* @param id the id of the entity.
* @return the entity.
*/
@Override
@Transactional(readOnly = true)
@Cacheable(cacheNames = { "cache" },key = "#id")
public Optional<ProfileDTO> findOne(Long id) {
log.debug("Request to get Profile : {}", id);
return profileRepository.findById(id)
.map(profileMapper::toDto);
}