如何在缓存的分页结果中添加新对象或更新对象

How to add new or update object in cached paged result

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

Class 配置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"
}

但是列表中的分页结果是一样的。如何更新 return 上的对象?谢谢。

与您使用 id 注释 @cacheable 缓存记录的方式类似。更新时,您必须使用注释 @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) {
    }