6
谷歌番石榴教程說緩存到期可以Ticker番石榴北京時間緩存過期
爲我的理解進行測試,我可以用它來強迫一個快速呼氣。 我對?
但我嘗試了下面的代碼,它沒有工作,任何建議?
@Test
public void expireAfterWriteTestWithTicker() throws InterruptedException {
Ticker t = new Ticker() {
@Override
public long read() {
return TimeUnit.MILLISECONDS.toNanos(5);
}
};
//Use ticker to force expire in 5 millseconds
LoadingCache<String, String> cache = CacheBuilder.newBuilder()
.expireAfterWrite(20, TimeUnit.MINUTES).ticker(t).build(loader);
cache.getUnchecked("hello");
assertEquals(1, cache.size());
assertNotNull(cache.getIfPresent("hello"));
//sleep
Thread.sleep(10);
assertNull(cache.getIfPresent("hello")); //failed
}
而不是' Thread.sleep',增加超過20分鐘的股票。例如,請參閱這些[單元測試](https://github.com/ben-manes/caffeine/blob/master/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java ) –
謝謝。現在就開始工作了。 – user3644708