我使用Spring Boot和EhCache開發日曆應用程序。Spring Cache - 生成自定義緩存結果
某些用戶可以請求某個特定周的音樂會,其他人可以請求整個月的音樂會。所有的響應都被緩存了300秒以減少響應時間。
緩存方法的輸入參數是startDate
,endDate
和username
。
問題是,當用戶請求整個月的音樂會,然後在同一個月內的一個星期內,該方法將被執行兩次,即使「周」響應可以基於「月「迴應,因爲本週可能是緩存月份的一個子集。
實例:
1.
INPUT
startDate:08.06.2016, endDate:07.07.2016, username:exampleUser
//the method is executed, response cached
OUTPUT
concert "Example Concert" on 13.06.2016
2.
INPUT
startDate:08.06.2016, endDate:15.06.2016, username:exampleUser
//the method is executed, response cached
OUTPUT
concert "Example Concert" on 13.06.2016
正如你可以看到的例子中,該方法將被執行兩次,但實際上沒有必要在執行該方法第二個例子,可以從第一個請求中返回的緩存數據中提取響應。
我想創建concerts
緩存定製的「發電機」,這將能遍歷所有緩存數據,發現不同的日期範圍是「遏制」要求startDate
和endDate
這裏是我到目前爲止:
緩存方法:
@Override
@CachePut("concerts")
public List<Event> refreshEventsCache(String eventsForUser, Date startDate, Date endDate) throws Exception {
return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}
@Override
@Cacheable(value = "concerts")
public List<Event> getEvents(String eventsForUser, Date startDate, Date endDate) throws Exception {
return fetchEventsFromTheServer(eventsForUser, startDate, endDate);
}
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="concerts"
maxElementsInMemory="5000"
eternal="false"
timeToIdleSeconds="0"
timeToLiveSeconds="300"
overflowToDisk="false"
memoryStoreEvictionPolicy="LFU" />
</ehcache>
Application.java:
...
@SpringBootApplication
@ComponentScan
@EnableCaching
@EnableScheduling
public class SpringBootWebApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(applicationClass);
}
private static Class<SpringBootWebApplication> applicationClass = SpringBootWebApplication.class;
}
如何建立 「智能」 高速緩存產生,這將返回緩存的數據子集?
您能否提供一個示例,我如何聲明/注入自定義緩存管理器?現在我使用Spring Boot默認的緩存管理器自動配置 – Oleg
您可以在'@ Service'類中執行'@ Autowired' CacheManager cacheManager – shazin