I have a repository class as follows:
@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");
}
}
When I call the api that calls getAll()
first time, it takes time as expected. When I call getAll()
again, it is fast as result is returned from cache as expected.
However, on schedule, I call the refreshAllCustomers()
to clear the cache and re-populate it using getAll()
call in which case I expect the result to be cached again.
After the call to refreshAllCustomers()
, it seems that any calls to getAll()
runs the query and not returning the results from the cache itself.
Any ideas why this is happening? Am I missing a config or not doing something correctly.
@CacheEvict
在执行该方法后生效。执行refreshAllCustomers ()
方法后,将清除缓存。如果要在执行该方法之前使用@CacheEvict
批注使缓存无效,则可以尝试@CacheEvict (value = customers, before Invocation = true)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.