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

The following examples show how to use com.github.benmanes.caffeine.cache.Caffeine#from() . 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: RepositoryCache.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public RepositoryCache(String cacheSpec, MeterRegistry meterRegistry) {
    this.cacheSpec = requireNonNull(validateCacheSpec(cacheSpec), "cacheSpec");
    requireNonNull(meterRegistry, "meterRegistry");

    final Caffeine<Object, Object> builder = Caffeine.from(cacheSpec);
    if (cacheSpec.contains("maximumWeight=")) {
        builder.weigher((Weigher<CacheableCall, Object>) CacheableCall::weigh);
    }
    cache = builder.recordStats()
                   .buildAsync((key, executor) -> {
                       logger.debug("Cache miss: {}", key);
                       return key.execute();
                   });

    CaffeineCacheMetrics.monitor(meterRegistry, cache, "repository");
}
 
Example 2
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 6 votes vote down vote up
ProtobufRedisLoadingCache(
    K keyPrototype,
    V valuePrototype,
    Duration redisTtl,
    @Nullable CaffeineSpec localCacheSpec,
    RemoteCache<K, V> remoteCache) {
  checkNotNull(keyPrototype, "keyPrototype");
  checkNotNull(valuePrototype, "valuePrototype");
  checkNotNull(redisTtl, "redisTtl");
  this.remoteCache = checkNotNull(remoteCache, "remoteCache");
  final Caffeine<Object, Object> caffeineBuilder =
      localCacheSpec != null
          ? Caffeine.from(localCacheSpec)
          : Caffeine.newBuilder().maximumSize(0);
  cache =
      caffeineBuilder
          .executor(CurrentRequestContextExecutor.INSTANCE)
          .buildAsync((k, executor) -> remoteCache.get(k).toCompletableFuture());
  setArgs = SetArgs.Builder.px(redisTtl.toMillis());
}
 
Example 3
Source File: FileService.java    From armeria with Apache License 2.0 5 votes vote down vote up
private static Cache<PathAndEncoding, AggregatedHttpFile> newCache(String cacheSpec) {
    final Caffeine<Object, Object> b = Caffeine.from(cacheSpec);
    b.recordStats()
     .removalListener((RemovalListener<PathAndEncoding, AggregatedHttpFile>) (key, value, cause) -> {
         if (value != null) {
             final HttpData content = value.content();
             if (content instanceof ByteBufHolder) {
                 ((ByteBufHolder) content).release();
             }
         }
     });
    return b.build();
}