java - 如何在缓存的分页结果中添加新对象或更新对象
问题描述
如何更新缓存的分页列表中的对象?创建对象时,它会在分页列表中返回,但执行更新后,分页列表中返回的对象不会更改。
类配置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"
}
但是,列表中的分页结果是相同的。如何在返回时更新对象?谢谢你。
解决方案
以类似的方式,您如何使用注释来缓存@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) {
}
推荐阅读
- excel - 在 Excel VBA 上尝试“.Insert Shift:=xlDown”会在特定 PC 上出现错误
- python - Python CSV dictReader,没有得到预期的输出
- dialogflow-es - 让 Google 助理听 1 分钟
- python - 如何在 python 中重置 CUDA GPU
- variables - 解释随机变量列表的索引表达式
- python-3.x - 如何在 Python 中创建一个在 3 个索引和 numpy.nan 值之间递增的列表
- c# - 如何使用基于代码的配置在 serilog 中输出紧凑的日志级别
- eclipse - 选择工作区后 Eclipse 2020-03 冻结
- mysql - Laravel 查询返回的行数少于 MySQL
- javascript - 如果文档存在,则 Firestore 增量值,否则设置它