简体   繁体   English

按计划将缓存逐出后未使用Spring缓存

[英]Spring cache not being used after Cache evict on schedule

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. 当我第一次调用调用getAll()的api时,它花费了预期的时间。 When I call getAll() again, it is fast as result is returned from cache as expected. 当我再次调用getAll()时,速度很快,因为按预期从缓存返回了结果。

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. 但是,按计划,我调用refreshAllCustomers()清除缓存并使用getAll()调用重新填充它,在这种情况下,我希望结果将再次被缓存。

After the call to refreshAllCustomers() , it seems that any calls to getAll() runs the query and not returning the results from the cache itself. 在调用refreshAllCustomers() ,似乎对getAll()所有调用都会运行查询,并且不会从缓存本身返回结果。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM