com.github.benmanes.caffeine.cache.Caffeine.build()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(6.4k)|赞(0)|评价(0)|浏览(283)

本文整理了Java中com.github.benmanes.caffeine.cache.Caffeine.build()方法的一些代码示例,展示了Caffeine.build()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Caffeine.build()方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Caffeine
类名称:Caffeine
方法名:build

Caffeine.build介绍

[英]Builds a cache which does not automatically load values when keys are requested unless a mapping function is provided. Note that multiple threads can concurrently load values for distinct keys.

Consider #build(CacheLoader) instead, if it is feasible to implement a CacheLoader.

This method does not alter the state of this Caffeine instance, so it can be invoked again to create multiple independent caches.
[中]构建一个缓存,除非提供映射功能,否则在请求密钥时不会自动加载值。请注意,多个线程可以同时加载不同键的值。
如果可以实现一个CaseLoad,那么可以考虑使用“编译”(CaseloDad)。
此方法不会更改此实例的状态,因此可以再次调用它以创建多个独立缓存。

代码示例

代码示例来源:origin: spring-projects/spring-framework

/**
 * Create a native Caffeine Cache instance for the specified cache name.
 * @param name the name of the cache
 * @return the native Caffeine Cache instance
 */
protected com.github.benmanes.caffeine.cache.Cache<Object, Object> createNativeCaffeineCache(String name) {
  if (this.cacheLoader != null) {
    return this.cacheBuilder.build(this.cacheLoader);
  }
  else {
    return this.cacheBuilder.build();
  }
}

代码示例来源:origin: dropwizard/dropwizard

public MustacheViewRenderer() {
  this.factories = Caffeine.newBuilder().build(new CacheLoader<Class<? extends View>, MustacheFactory>() {
    @Override
    public MustacheFactory load(Class<? extends View> key) throws Exception {
      return createNewMustacheFactory(key);
    }
  });
}

代码示例来源:origin: dropwizard/dropwizard

RegexStringMatchingStrategy() {
  patternCache = Caffeine.newBuilder()
    .expireAfterWrite(1, TimeUnit.HOURS)
    .build(Pattern::compile);
}

代码示例来源:origin: dropwizard/dropwizard

public FreemarkerViewRenderer() {
  this.loader = new TemplateLoader();
  this.configurationCache = Caffeine.newBuilder().build(loader);
}

代码示例来源:origin: ben-manes/caffeine

/**
 * Returns a Caffeine cache wrapped in a Guava {@link Cache} facade.
 *
 * @param builder the configured cache builder
 * @return a cache exposed under the Guava APIs
 */
@NonNull
public static <K, V, K1 extends K, V1 extends V> Cache<K1, V1> build(
  @NonNull Caffeine<K, V> builder) {
 return new CaffeinatedGuavaCache<>(builder.build());
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void recordStats() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().recordStats();
 assertThat(builder.statsCounterSupplier, is(Caffeine.ENABLED_STATS_COUNTER_SUPPLIER));
 builder.build();
}

代码示例来源:origin: spring-projects/spring-framework

@Before
public void setUp() {
  nativeCache = Caffeine.newBuilder().build();
  cache = new CaffeineCache(CACHE_NAME, nativeCache);
  com.github.benmanes.caffeine.cache.Cache<Object, Object> nativeCacheNoNull
      = Caffeine.newBuilder().build();
  cacheNoNull =  new CaffeineCache(CACHE_NAME_NO_NULL, nativeCacheNoNull, false);
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void executor() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().executor(MoreExecutors.directExecutor());
 assertThat(builder.getExecutor(), is(MoreExecutors.directExecutor()));
 builder.build();
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void refreshAfterWrite() {
 Caffeine<Object, Object> builder = Caffeine.newBuilder()
   .refreshAfterWrite(1, TimeUnit.MILLISECONDS);
 assertThat(builder.getRefreshAfterWriteNanos(), is(TimeUnit.MILLISECONDS.toNanos(1)));
 builder.build(k -> k);
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void removalListener() {
 RemovalListener<Object, Object> removalListener = (k, v, c) -> {};
 Caffeine<?, ?> builder = Caffeine.newBuilder().removalListener(removalListener);
 assertThat(builder.getRemovalListener(false), is(removalListener));
 builder.build();
}

代码示例来源:origin: ben-manes/caffeine

@Test
 public void writer() {
  Caffeine<?, ?> builder = Caffeine.newBuilder().writer(writer);
  assertThat(builder.getCacheWriter(), is(writer));
  builder.build();
 }
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void ticker() {
 Ticker ticker = new FakeTicker()::read;
 Caffeine<?, ?> builder = Caffeine.newBuilder().ticker(ticker);
 assertThat(builder.ticker, is(ticker));
 builder.build();
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void expireAfterAccess_duration_small() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(Duration.ofMillis(0));
 assertThat(builder.expireAfterAccessNanos, is(0L));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L));
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void expireAfterWrite_small() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterWrite(0, TimeUnit.MILLISECONDS);
 assertThat(builder.expireAfterWriteNanos, is(0L));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L));
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void expireAfterWrite_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .expireAfterWrite(Integer.MAX_VALUE, TimeUnit.NANOSECONDS);
 assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void recordStats_custom() {
 Supplier<StatsCounter> supplier = () -> statsCounter;
 Caffeine<?, ?> builder = Caffeine.newBuilder().recordStats(supplier);
 builder.statsCounterSupplier.get().recordEviction(1);
 verify(statsCounter).recordEviction(1);
 builder.build();
}

代码示例来源:origin: ben-manes/caffeine

@Test
public void maximumWeight_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .maximumWeight(Integer.MAX_VALUE).weigher(Weigher.singletonWeigher());
 assertThat(builder.maximumWeight, is((long) Integer.MAX_VALUE));
 assertThat(builder.weigher, is(Weigher.singletonWeigher()));
 Eviction<?, ?> eviction = builder.build().policy().eviction().get();
 assertThat(eviction.getMaximum(), is((long) Integer.MAX_VALUE));
 assertThat(eviction.isWeighted(), is(true));
}

代码示例来源:origin: ben-manes/caffeine

private static <K, V> ConcurrentMap<K, V> map() {
 return Caffeine.newBuilder()
   .maximumSize(Integer.MAX_VALUE)
   .<K, V>build().asMap();
}

代码示例来源:origin: ben-manes/caffeine

@Test
 public void sanity() {
  LoadingCache<Integer, Integer> cache = Caffeine.newBuilder().build(k -> -k);
  assertEquals(-1, cache.get(1).intValue());
 }
}

代码示例来源:origin: ben-manes/caffeine

public void testMaximumWeight_withoutWeigher() {
 Caffeine<Object, Object> builder = Caffeine.from(parse("maximumWeight=9000"));
 try {
  builder.build(k -> null);
  fail();
 } catch (IllegalStateException expected) {}
}

相关文章