首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >没有使用@CachePut更新缓存

没有使用@CachePut更新缓存
EN

Stack Overflow用户
提问于 2020-11-02 06:06:14
回答 1查看 426关注 0票数 1

我正在做一个二级哈泽尔广播缓存。缓存在findAll方法中非常正常,但是当我试图更新现有数据或添加新数据时,再次尝试使用findAll方法收集所有数据时,它会给出旧记录,而不是更新的记录。我在这里附上了我的密码。重点是当我试图使用findById方法获取数据时,它会从缓存本身提供更新的数据。我不想使用@CacheEvit。

代码语言:javascript
运行
复制
@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);
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-02 07:47:48

你没有提供完整的资料来源,但恐怕你把两件事混为一谈。

一方面,有春天的缓存。您可以使用@Cacheable注释来配置它。Spring缓存只添加单个元素,而不添加集合(cf )。相关的GitHub问题以下跌收尾)。

另一方面,还有Hibernate二级缓存,可以通过Spring的application.properties (或YAML)进行配置。这里有一个样本工程,它有正确的配置。它允许在存储库返回的集合中缓存实体。

这两个缓存是正交的。

我建议您从等式中删除Spring,只使用Hibernate的二级缓存。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64640563

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档