Java Code Examples for com.github.benmanes.caffeine.cache.Caffeine#buildAsync()

The following examples show how to use com.github.benmanes.caffeine.cache.Caffeine#buildAsync() . 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: CaffeineCache.java    From quarkus with Apache License 2.0 6 votes vote down vote up
public CaffeineCache(CaffeineCacheInfo cacheInfo, Executor executor) {
    this.name = cacheInfo.name;
    Caffeine<Object, Object> builder = Caffeine.newBuilder();
    if (executor != null) {
        builder.executor(executor);
    }
    if (cacheInfo.initialCapacity != null) {
        this.initialCapacity = cacheInfo.initialCapacity;
        builder.initialCapacity(cacheInfo.initialCapacity);
    }
    if (cacheInfo.maximumSize != null) {
        this.maximumSize = cacheInfo.maximumSize;
        builder.maximumSize(cacheInfo.maximumSize);
    }
    if (cacheInfo.expireAfterWrite != null) {
        this.expireAfterWrite = cacheInfo.expireAfterWrite;
        builder.expireAfterWrite(cacheInfo.expireAfterWrite);
    }
    if (cacheInfo.expireAfterAccess != null) {
        this.expireAfterAccess = cacheInfo.expireAfterAccess;
        builder.expireAfterAccess(cacheInfo.expireAfterAccess);
    }
    cache = builder.buildAsync();
}
 
Example 2
Source File: CaffeineCache.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private CaffeineCache(final Caffeine<? super K, ? super V> caffeine,
        final AsyncCacheLoader<K, V> loader,
        @Nullable final String cacheName) {

    if (cacheName != null) {
        this.metricStatsCounter =
                MetricsStatsCounter.of(cacheName, this::getMaxCacheSize, this::getCurrentCacheSize);
        caffeine.recordStats(() -> metricStatsCounter);
        this.asyncLoadingCache = caffeine.buildAsync(loader);
        this.synchronousCacheView = asyncLoadingCache.synchronous();
    } else {
        this.asyncLoadingCache = caffeine.buildAsync(loader);
        this.synchronousCacheView = asyncLoadingCache.synchronous();
        this.metricStatsCounter = null;
    }
}
 
Example 3
Source File: CaffeineCacheFromContext.java    From caffeine with Apache License 2.0 4 votes vote down vote up
public static <K, V> Cache<K, V> newCaffeineCache(CacheContext context) {
  Caffeine<Object, Object> builder = Caffeine.newBuilder();
  context.caffeine = builder;

  if (context.initialCapacity != InitialCapacity.DEFAULT) {
    builder.initialCapacity(context.initialCapacity.size());
  }
  if (context.isRecordingStats()) {
    builder.recordStats();
  }
  if (context.maximumSize != Maximum.DISABLED) {
    if (context.weigher == CacheWeigher.DEFAULT) {
      builder.maximumSize(context.maximumSize.max());
    } else {
      builder.weigher(context.weigher);
      builder.maximumWeight(context.maximumWeight());
    }
  }
  if (context.expiryType() != CacheExpiry.DISABLED) {
    builder.expireAfter(context.expiry);
  }
  if (context.afterAccess != Expire.DISABLED) {
    builder.expireAfterAccess(context.afterAccess.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.afterWrite != Expire.DISABLED) {
    builder.expireAfterWrite(context.afterWrite.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.refresh != Expire.DISABLED) {
    builder.refreshAfterWrite(context.refresh.timeNanos(), TimeUnit.NANOSECONDS);
  }
  if (context.expires() || context.refreshes()) {
    SerializableTicker ticker = context.ticker()::read;
    builder.ticker(ticker);
  }
  if (context.keyStrength == ReferenceType.WEAK) {
    builder.weakKeys();
  } else if (context.keyStrength == ReferenceType.SOFT) {
    throw new IllegalStateException();
  }
  if (context.isWeakValues()) {
    builder.weakValues();
  } else if (context.isSoftValues()) {
    builder.softValues();
  }
  if (context.cacheExecutor != CacheExecutor.DEFAULT) {
    builder.executor(context.executor);
  }
  if (context.cacheScheduler != CacheScheduler.DEFAULT) {
    builder.scheduler(context.scheduler);
  }
  if (context.removalListenerType != Listener.DEFAULT) {
    builder.removalListener(context.removalListener);
  }
  if (context.isStrongKeys() && !context.isAsync()) {
    builder.writer(context.cacheWriter());
  }
  if (context.isAsync()) {
    if (context.loader == null) {
      context.asyncCache = builder.buildAsync();
    } else {
      context.asyncCache = builder.buildAsync(
          context.isAsyncLoading ? context.loader.async() : context.loader);
    }
    context.cache = context.asyncCache.synchronous();
  } else if (context.loader == null) {
    context.cache = builder.build();
  } else {
    context.cache = builder.build(context.loader);
  }

  @SuppressWarnings("unchecked")
  Cache<K, V> castedCache = (Cache<K, V>) context.cache;
  RandomSeedEnforcer.resetThreadLocalRandom();
  return castedCache;
}