【发布时间】:2025-12-14 18:40:01
【问题描述】:
我正在研究二级 Hazelcast 缓存。缓存使用 findAll 方法工作得非常好,但是当我尝试更新现有数据或添加新数据并再次尝试使用它提供的 findAll 方法收集所有数据时旧记录没有更新一个。在这里我附上了我的代码。重点是当我尝试使用 findById 方法获取数据时,它会为我提供来自缓存本身的更新数据。我不想使用@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);
}
【问题讨论】:
标签: spring-boot caching hazelcast second-level-cache