Java Code Examples for com.github.benmanes.caffeine.cache.AsyncLoadingCache#get()

The following examples show how to use com.github.benmanes.caffeine.cache.AsyncLoadingCache#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private static void findStartPointLoop(PositionImpl p, long start, long end,
                                       CompletableFuture<Long> promise,
                                       AsyncLoadingCache<Long,MessageIdData> cache) {
    long midpoint = start + ((end - start) / 2);

    CompletableFuture<MessageIdData> startEntry = cache.get(start);
    CompletableFuture<MessageIdData> middleEntry = cache.get(midpoint);
    CompletableFuture<MessageIdData> endEntry = cache.get(end);

    CompletableFuture.allOf(startEntry, middleEntry, endEntry).thenRun(
            () -> {
                if (comparePositionAndMessageId(p, startEntry.join()) <= 0) {
                    promise.complete(start);
                } else if (comparePositionAndMessageId(p, middleEntry.join()) <= 0) {
                    findStartPointLoop(p, start, midpoint, promise, cache);
                } else if (comparePositionAndMessageId(p, endEntry.join()) <= 0) {
                    findStartPointLoop(p, midpoint + 1, end, promise, cache);
                } else {
                    promise.complete(NEWER_THAN_COMPACTED);
                }
            }).exceptionally((exception) -> {
                    promise.completeExceptionally(exception);
                    return null;
                });
}
 
Example 2
Source File: CoalescingBulkloaderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void maxDelayIsNotMissedTooMuch() throws InterruptedException {
    AtomicInteger loaderCalled = new AtomicInteger(0);
    final AsyncLoadingCache<Integer, Integer> cache = createCache(loaderCalled);

    // a cache get won't take too long
    final CompletableFuture<Integer> result = cache.get(1);
    Awaitility.await().pollThread(Thread::new).pollInterval(1, MILLISECONDS)
            .between(maxDelay - delta, MILLISECONDS, maxDelay + delta, MILLISECONDS)
            .untilAtomic(loaderCalled, is(1));
    assertFalse("delay in load", result.isDone());
    Thread.sleep(actualLoadTime);
    assertThat(result.getNow(0), is(1));
}
 
Example 3
Source File: CoalescingBulkloaderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test
public void whenEnoughKeysAreRequestedTheLoadWillHappenImmediately() throws InterruptedException {
    AtomicInteger loaderCalled = new AtomicInteger(0);
    final AsyncLoadingCache<Integer, Integer> cache = createCache(loaderCalled);

    CompletableFuture<Integer>[] results = new CompletableFuture[maxLoadSize];
    for (int i = 0; i < maxLoadSize - 1; i++)
        results[i] = cache.get(i);
    Thread.sleep(delta);
    // requesting 9 keys does not trigger a load
    assertThat(loaderCalled.get(), is(0));

    for (int i = 0; i < maxLoadSize - 1; i++) {
        final CompletableFuture<Integer> result = cache.get(i);
        assertThat(result, sameInstance(results[i]));
        assertFalse("no load therefore unknown result", result.isDone());
    }
    Thread.sleep(delta);
    // requesting the same 9 keys still doesn't trigger a load
    assertThat(loaderCalled.get(), is(0));

    // requesting one more key will trigger immediately
    results[maxLoadSize - 1] = cache.get(maxLoadSize - 1);
    Awaitility.await().pollInterval(1, MILLISECONDS)
            .atMost(delta, MILLISECONDS)
            .untilAtomic(loaderCalled, is(Integer.valueOf(1)));

    // values are not immediately available because of the sleep in the loader
    for (int i = 0; i < maxLoadSize; i++) {
        assertThat(results[i].getNow(-1), is(-1));
    }
    Thread.sleep(actualLoadTime + delta);
    // slept enough
    for (int i = 0; i < maxLoadSize; i++) {
        assertThat(results[i].getNow(-1), is(i));
    }

}