com.github.benmanes.caffeine.cache.AsyncLoadingCache Java Examples

The following examples show how to use com.github.benmanes.caffeine.cache.AsyncLoadingCache. 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: Issue30Test.java    From caffeine with Apache License 2.0 6 votes vote down vote up
private void firstUpdate(AsyncLoadingCache<String, String> cache,
    ConcurrentMap<String, String> source) throws InterruptedException, ExecutionException {
  source.put(A_KEY, A_UPDATE_1);
  source.put(B_KEY, B_UPDATE_1);

  assertThat("should serve cached initial value", cache.get(A_KEY), is(futureOf(A_ORIGINAL)));
  assertThat("should serve cached initial value", cache.get(B_KEY), is(futureOf(B_ORIGINAL)));

  Thread.sleep(EPSILON); // sleep for less than expiration
  assertThat("still serve cached initial value", cache.get(A_KEY), is(futureOf(A_ORIGINAL)));
  assertThat("still serve cached initial value", cache.get(B_KEY), is(futureOf(B_ORIGINAL)));

  Thread.sleep(TTL + EPSILON); // sleep until expiration
  assertThat("now serve first updated value", cache.get(A_KEY), is(futureOf(A_UPDATE_1)));
  assertThat("now serve first updated value", cache.get(B_KEY), is(futureOf(B_UPDATE_1)));
}
 
Example #2
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 #3
Source File: CoalescingBulkloaderTest.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@NonNull private AsyncLoadingCache<Integer, Integer> createCache(AtomicInteger loaderCalled) {
    return Caffeine.newBuilder().buildAsync(cbl.apply(ints -> {
        loaderCalled.incrementAndGet();
        try {
            Thread.sleep(actualLoadTime);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return ints;
    }));
}
 
Example #4
Source File: CaffeineTest.java    From cache2k-benchmark with Apache License 2.0 5 votes vote down vote up
@Test
public void testAsyncLoader() throws Exception {
	AsyncLoadingCache c =
	Caffeine.newBuilder()
		.buildAsync(new AsyncCacheLoader<Object, Object>() {
			@Override
			public CompletableFuture<Object> asyncLoad(final Object key, final Executor executor) {
				return CompletableFuture.completedFuture("okay");
			}
		});
	CompletableFuture<Map> cf = c.getAll(Arrays.asList(1, 2, 3));
	Map m = cf.get();
}
 
Example #5
Source File: CacheValidationListener.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Validates the internal state of the cache. */
private void validate(ITestResult testResult) {
  boolean foundCache = false;
  CacheContext context = null;
  for (Object param : testResult.getParameters()) {
    if (param instanceof Cache<?, ?>) {
      foundCache = true;
      assertThat((Cache<?, ?>) param, is(validCache()));
    } else if (param instanceof AsyncLoadingCache<?, ?>) {
      foundCache = true;
      assertThat((AsyncLoadingCache<?, ?>) param, is(validAsyncCache()));
    } else if (param instanceof Map<?, ?>) {
      foundCache = true;
      assertThat((Map<?, ?>) param, is(validAsMap()));
    } else if (param instanceof CacheContext) {
      context = (CacheContext) param;
    }
  }
  if (context != null) {
    if (!foundCache) {
      assertThat(context.cache, is(validCache()));
    }
    checkWriter(testResult, context);
    checkNoStats(testResult, context);
    checkExecutor(testResult, context);
  }
}
 
Example #6
Source File: CacheProvider.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/** Returns a new cache generator. */
private static CacheGenerator newCacheGenerator(Method testMethod) {
  CacheSpec cacheSpec = testMethod.getAnnotation(CacheSpec.class);
  requireNonNull(cacheSpec, "@CacheSpec not found");
  Options options = Options.fromSystemProperties();

  // Inspect the test parameters for interface constraints (loading, async)
  boolean isAsyncOnly = hasCacheOfType(testMethod, AsyncCache.class);
  boolean isLoadingOnly =
      hasCacheOfType(testMethod, LoadingCache.class)
      || hasCacheOfType(testMethod, AsyncLoadingCache.class)
      || options.compute().filter(Compute.ASYNC::equals).isPresent();

  return new CacheGenerator(cacheSpec, options, isLoadingOnly, isAsyncOnly);
}
 
Example #7
Source File: Issue30Test.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private void secondUpdate(AsyncLoadingCache<String, String> cache,
    ConcurrentMap<String, String> source) throws Exception {
  source.put(A_KEY, A_UPDATE_2);
  source.put(B_KEY, B_UPDATE_2);

  assertThat("serve cached first updated value", cache.get(A_KEY), is(futureOf(A_UPDATE_1)));
  assertThat("serve cached first updated value", cache.get(B_KEY), is(futureOf(B_UPDATE_1)));

  Thread.sleep(EPSILON); // sleep for less than expiration
  assertThat("serve cached first updated value", cache.get(A_KEY), is(futureOf(A_UPDATE_1)));
  assertThat("serve cached first updated value", cache.get(A_KEY), is(futureOf(A_UPDATE_1)));
}
 
Example #8
Source File: Issue30Test.java    From caffeine with Apache License 2.0 5 votes vote down vote up
private void initialValues(AsyncLoadingCache<String, String> cache,
    ConcurrentMap<String, String> source, ConcurrentMap<String, Instant> lastLoad)
        throws InterruptedException, ExecutionException {
  source.put(A_KEY, A_ORIGINAL);
  source.put(B_KEY, B_ORIGINAL);
  lastLoad.clear();

  assertThat("should serve initial value", cache.get(A_KEY), is(futureOf(A_ORIGINAL)));
  assertThat("should serve initial value", cache.get(B_KEY), is(futureOf(B_ORIGINAL)));
}
 
Example #9
Source File: Issue30Test.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@Test(dataProvider = "params", invocationCount = 100, threadPoolSize = N_THREADS)
public void expiration(AsyncLoadingCache<String, String> cache,
    ConcurrentMap<String, String> source, ConcurrentMap<String, Instant> lastLoad)
        throws Exception {
  initialValues(cache, source, lastLoad);
  firstUpdate(cache, source);
  secondUpdate(cache, source);
}
 
Example #10
Source File: Issue30Test.java    From caffeine with Apache License 2.0 5 votes vote down vote up
@DataProvider(name = "params")
public Object[][] providesCache() {
  ConcurrentMap<String, String> source = new ConcurrentHashMap<>();
  ConcurrentMap<String, Instant> lastLoad = new ConcurrentHashMap<>();
  AsyncLoadingCache<String, String> cache = Caffeine.newBuilder()
      .expireAfterWrite(TTL, TimeUnit.MILLISECONDS)
      .executor(executor)
      .buildAsync(new Loader(source, lastLoad));
  return new Object[][] {{ cache, source, lastLoad }};
}
 
Example #11
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));
    }

}
 
Example #12
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 #13
Source File: NamespaceBundlesTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Pair<NamespaceBundles, List<NamespaceBundle>> splitBundlesUtilFactory(NamespaceBundleFactory utilityFactory,
        NamespaceName nsname, NamespaceBundles bundles, NamespaceBundle targetBundle, int numBundles)
        throws Exception {
    Field bCacheField = NamespaceBundleFactory.class.getDeclaredField("bundlesCache");
    bCacheField.setAccessible(true);
    ((AsyncLoadingCache<NamespaceName, NamespaceBundles>) bCacheField.get(utilityFactory)).put(nsname,
            CompletableFuture.completedFuture(bundles));
    return utilityFactory.splitBundles(targetBundle, numBundles, null);
}
 
Example #14
Source File: NamespaceServiceTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Pair<NamespaceBundles, List<NamespaceBundle>> splitBundles(NamespaceBundleFactory utilityFactory,
        NamespaceName nsname, NamespaceBundles bundles, NamespaceBundle targetBundle) throws Exception {
    Field bCacheField = NamespaceBundleFactory.class.getDeclaredField("bundlesCache");
    bCacheField.setAccessible(true);
    ((AsyncLoadingCache<NamespaceName, NamespaceBundles>) bCacheField.get(utilityFactory)).put(nsname,
            CompletableFuture.completedFuture(bundles));
    return utilityFactory.splitBundles(targetBundle, 2, null);
}
 
Example #15
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 5 votes vote down vote up
static CompletableFuture<Long> findStartPoint(PositionImpl p,
                                              long lastEntryId,
                                              AsyncLoadingCache<Long,MessageIdData> cache) {
    CompletableFuture<Long> promise = new CompletableFuture<>();
    findStartPointLoop(p, 0, lastEntryId, promise, cache);
    return promise;
}
 
Example #16
Source File: TitusClientImpl.java    From titus-control-plane with Apache License 2.0 5 votes vote down vote up
private AsyncLoadingCache<String, Job> buildCacheForJobs() {
    return Caffeine.newBuilder()
            .maximumSize(MAX_CACHE_SIZE)
            .buildAsync(new AsyncCacheLoader<String, Job>() {
                @Nonnull
                @Override
                public CompletableFuture<Job> asyncLoad(@Nonnull String jobId, @Nonnull Executor executor) {
                    ListenableFuture<Job> jobFuture = jobManagementServiceFutureStub.findJob(JobId.newBuilder().setId(jobId).build());
                    return toCompletableFuture(jobFuture, executor);
                }
            });
}
 
Example #17
Source File: CaffeineAsyncLoadingTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureOnAsynchronous() {
	FakeTicker ticker = new FakeTicker();
	
	AsyncLoadingCache<String, Integer> subject = Caffeine.newBuilder()
			.expireAfterAccess(240, TimeUnit.SECONDS)
			.refreshAfterWrite(120, TimeUnit.SECONDS)
			.ticker(ticker::read)
			.recordStats()
			.buildAsync(new AsyncCacheLoaderFailureImplementation());
	
	Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block());
	
	ticker.advance(Duration.ofSeconds(10));
	
	Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block());
	
	ticker.advance(Duration.ofSeconds(250));
	
	Mono<Integer> errorMono = Mono.fromFuture(subject.get("a"));
	
	boolean thrown = false;
	try {
		errorMono.block();
		thrown = false;
	} catch (Throwable t) {
		thrown = true;
	}
	Assert.assertTrue(thrown);
}
 
Example #18
Source File: CaffeineAsyncLoadingTest.java    From promregator with Apache License 2.0 5 votes vote down vote up
@Test
public void testRefreshIsAsynchronous() throws InterruptedException {
	FakeTicker ticker = new FakeTicker();
	
	AsyncLoadingCache<String, Integer> subject = Caffeine.newBuilder()
			.expireAfterAccess(240, TimeUnit.SECONDS)
			.refreshAfterWrite(120, TimeUnit.SECONDS)
			.ticker(ticker::read)
			.recordStats()
			.buildAsync(new AsyncCacheLoaderTimingImplementation());
	
	log.info("Starting first request");
	Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block());
	log.info("Stats on cache: "+subject.synchronous().stats().toString());
	
	ticker.advance(Duration.ofSeconds(10));
	
	log.info("Sending second request");
	Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block());
	log.info("Stats on cache: "+subject.synchronous().stats().toString());
	
	ticker.advance(Duration.ofSeconds(120));
	
	log.info("Sending third request");
	Assert.assertEquals(new Integer(0), Mono.fromFuture(subject.get("a")).block());
	// That's the interesting case here! Note the zero above: This means that we get old cache data (which is what we want!)
	log.info("Stats on cache: "+subject.synchronous().stats().toString());

	ticker.advance(Duration.ofSeconds(10));
	Thread.sleep(250); // wait until async loading took place
	
	log.info("Sending fourth request");
	Assert.assertEquals(new Integer(1), Mono.fromFuture(subject.get("a")).block());
	log.info("Stats on cache: "+subject.synchronous().stats().toString());
	
}
 
Example #19
Source File: AsyncSqsClientImpl.java    From dynein with Apache License 2.0 4 votes vote down vote up
private AsyncLoadingCache<String, String> initUrlCache(long maxCacheSize) {
  return Caffeine.newBuilder()
      .maximumSize(maxCacheSize)
      .buildAsync((key, executor) -> getQueueUrl(key));
}
 
Example #20
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
CompactedTopicContext(LedgerHandle ledger, AsyncLoadingCache<Long,MessageIdData> cache) {
    this.ledger = ledger;
    this.cache = cache;
}
 
Example #21
Source File: CompactedTopicImpl.java    From pulsar with Apache License 2.0 4 votes vote down vote up
static AsyncLoadingCache<Long,MessageIdData> createCache(LedgerHandle lh,
                                                         long maxSize) {
    return Caffeine.newBuilder()
        .maximumSize(maxSize)
        .buildAsync((entryId, executor) -> readOneMessageId(lh, entryId));
}
 
Example #22
Source File: LoadableMetricDir.java    From graphouse with Apache License 2.0 4 votes vote down vote up
public LoadableMetricDir(MetricDir parent, String name, MetricStatus status,
                         AsyncLoadingCache<MetricDir, DirContent> dirContentProvider) {
    super(parent, name, status);
    this.dirContentProvider = dirContentProvider;
}
 
Example #23
Source File: CacheContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public <K, V> AsyncLoadingCache<K, V> buildAsync(CacheLoader<K, V> loader) {
  checkState(isCaffeine() && isAsync());
  AsyncLoadingCache<K, V> cache = caffeine.buildAsync(loader);
  this.cache = cache.synchronous();
  return cache;
}
 
Example #24
Source File: CacheContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public <K, V> AsyncLoadingCache<K, V> buildAsync(AsyncCacheLoader<K, V> loader) {
  checkState(isCaffeine() && isAsync());
  AsyncLoadingCache<K, V> cache = caffeine.buildAsync(loader);
  this.cache = cache.synchronous();
  return cache;
}
 
Example #25
Source File: InternalMetrics.java    From promregator with Apache License 2.0 4 votes vote down vote up
public void addCaffeineCache(String cacheName, AsyncLoadingCache<?, ?> cache) {
	if (!this.enabled)
		return;
	
	this.caffeineCacheMetricsCollector.addCache(cacheName, cache);
}