vendredi 1 mai 2015

Guava Ticker Cache expire

Google Guava tutorial said cache expiration can be tested with Ticker

as my understanding, I can use it to force a quick expiration. am I right?

but I tried in the following code, and it did not work, any suggestion?

@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 

}

Aucun commentaire:

Enregistrer un commentaire