我在Spring Boot應用程序中使用EHCache 2.9,並將緩存配置爲在300秒(5分鐘)後過期。EhCache-已過期的元素不會驅逐
當我運行應用程序並首次請求元素時,它會被緩存,之後永不過期。 但是,當我做@CachePut
它成功更新並更新元素然後返回。 我的配置有什麼問題?
這裏是我的ehcache.xml中文件:
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<defaultCache maxElementsInMemory="500" eternal="false"
overflowToDisk="false" memoryStoreEvictionPolicy="LFU" />
<diskStore path="java.io.tempdir"/>
<cache name="appointments"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
這裏是我如何請求緩存:
@Cacheable("appointments")
public List<Event> getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception {
return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}
@CachePut("appointments")
public List<Event> refreshEventsCache(String eventsForUser, Date startDate, Date endDate) throws Exception {
return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}
有什麼建議?
您正在使用哪種Spring Boot版本以及如何配置ehcache? –
Spring引導是1.3.5,我在Application.java中添加了@EnableCaching註釋並添加了ehcache.xml來配置它 – Oleg
在類路徑的根目錄中是否有'ehcache.xml'?即在'src/main/resources'中?如果不是,則需要設置'spring.cache.ehcache.config'屬性並將其指向'ehcache.xml'。你也可以刪除ehcache的特定版本標籤,並讓spring啓動管理版本(然後你將得到2.10)。 –