Google Guava samouczek powiedział ważności pamięci podręcznej mogą być testowane z TickerGuava Ticker Cache wygasają
jako mojego zrozumienia, mogę go używać, aby wymusić szybki wydech. Czy mam rację:?
ale próbowałem w poniższym kodzie, i to nie działało, żadnych sugestii?
@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
}
Zamiast ' Thread.sleep', zwiększ wartość paska przez ponad 20 minut. Na przykład, zobacz te [testy jednostkowe] (https://github.com/ben-manes/caffeine/blob/master/caffeine/src/test/java/com/github/benmanes/caffeine/cache/ExpireAfterWriteTest.java) –
Dzięki. Teraz działa. – user3644708