首页 > 解决方案 > 如何在缓存的分页结果中添加新对象或更新对象

问题描述

如何更新缓存的分页列表中的对象?创建对象时,它会在分页列表中返回,但执行更新后,分页列表中返回的对象不会更改。

类配置ehCache

@Configuration
@EnableCaching
public class EhCacheConfiguration {

    @Bean
    public CacheManager cacheManager() {
        return new EhCacheCacheManager(cacheManagerFactory().getObject());
    }

    @Bean
    public EhCacheManagerFactoryBean cacheManagerFactory() {
        EhCacheManagerFactoryBean bean = new EhCacheManagerFactoryBean();
        bean.setConfigLocation(new ClassPathResource("ehcache.xml"));
        bean.setShared(true);
        return bean;
    }
}

文件 ehCache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="ehcache.xsd">
    <cache name="contaCache"
           maxEntriesLocalHeap="100"
           timeToLiveSeconds="3600">
    </cache>
</ehcache>

我的服务如下所示:

@Service
public class ContaGerencialServiceImpl implements ContaGerencialService {
    private ContaGerencialRepository repository;

    @Autowired
    public ContaGerencialServiceImpl(ContaGerencialRepository repository) {
        this.repository = repository;
    }


    @Override
    @Cacheable(value = "contaCache", key = "#pageable.pageNumber")
    public Page<ContaGerencial> listPageable(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    @CachePut(value = "contaCache", key = "#conta.id")
    public ContaGerencial save(ContaGerencial conta) {
        return repository.save(conta);
    }

    @Override
    @CachePut(value = "contaCache", key = "#conta.id")
    public ContaGerencial update(ContaGerencial conta) {
        return repository.save(conta);
    }
}

Json 新对象:

{
  "numero":"102030",
  "nome":"Conta Gerencial 1"
}

分页列表:

{
    "content": [
        {
            "id": 2,
            "nome": "Conta Gerencial 1",
            "numero": "102030",
            "dataCadastro": "2019-07-26T00:00:00.000-0300",
            "dataUltimaAtualizacao": "2019-07-26T00:00:00.000-0300"
        }
    ],

     ...

}

json更新

{
    "id": 2,
    "numero": "222",
    "nome": "Conta Gerencial"
}

但是,列表中的分页结果是相同的。如何在返回时更新对象?谢谢你。

标签: javaspring-bootspring-mvcspring-data-jpaehcache

解决方案


以类似的方式,您如何使用注释来缓存@cacheable记录id。更新时,您必须使用 annotation 清除缓存@CacheEvict

    @Override
    @Cacheable(value = "contaCache", key = "#pageable.pageNumber")
    public Page<ContaGerencial> listPageable(Pageable pageable) {
        return repository.findAll(pageable);
    }

    @Override
    @CachePut(value = "contaCache", key = "#conta.id")
    public ContaGerencial save(ContaGerencial conta) {
        return repository.save(conta);
    }

    @Override
    @CachePut(value = "contaCache", key = "#conta.id")
    public ContaGerencial update(ContaGerencial conta) {
        return repository.save(conta);
    }

    @CacheEvict(value = "contaCache", key = "#id")
        public void evictSingleCacheValue(String id) {
    }

    @CacheEvict(value = "contaCache", key = "#pageable.pageNumber")
        public void evictListPageable(Pageable pageable) {
    }


推荐阅读