我有一个存储库类,如下所示:
@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)