gpt4 book ai didi

java - 按计划进行缓存驱逐后,Spring 缓存未被使用

转载 作者:行者123 更新时间:2023-12-02 01:13:24 25 4
gpt4 key购买 nike

我有一个存储库类,如下所示:

@EnableCaching
@EnableScheduling
@Repository("dao")
public class CustomerDao implements CustomerDao<Customer> {
//...

@Cacheable(value = "customers")
@Override
public List<Customer> getAll() {
LOG.info("in getALL() method");
return this.jdbcTemplate.query(this.QUERY_ALL_CUSTOMER, new CustomerRowMapper());
}

@CacheEvict(value = "customers", allEntries = true)
@Scheduled(fixedDelay = 60000L)
public void refreshAllCustomers() {
LOG.info("Refreshing Customers");
getAll();
LOG.info("Refreshing Customers Finished");
}
}

当我第一次调用调用 getAll() 的 api 时,它按照预期需要时间。当我再次调用 getAll() 时,速度很快,因为结果按预期从缓存中返回。

但是,按照计划,我调用 refreshAllCustomers() 来清除缓存并使用 getAll() 调用重新填充缓存,在这种情况下,我希望结果为再次被缓存。

在调用 refreshAllCustomers() 后,似乎对 getAll() 的任何调用都会运行查询,并且不会从缓存本身返回结果。

有什么想法为什么会发生这种情况吗?我是否缺少配置或没有正确执行某些操作。

最佳答案

@CacheEvict方法执行后生效。refreshAllCustomers()方法执行后,缓存被清除。如果要使用@CacheEvict注解可以在方法执行前使缓存失效,可以尝试@CacheEvict(value =customers, before Invocation = true)

关于java - 按计划进行缓存驱逐后,Spring 缓存未被使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57673771/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com