本文整理了Java中com.github.benmanes.caffeine.cache.Caffeine.newBuilder()
方法的一些代码示例,展示了Caffeine.newBuilder()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Caffeine.newBuilder()
方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Caffeine
类名称:Caffeine
方法名:newBuilder
[英]Constructs a new Caffeine instance with default settings, including strong keys, strong values, and no automatic eviction of any kind.
Note that while this return type is Caffeine, type parameters on the #build methods allow you to create a cache of any key and value type desired.
[中]使用默认设置构造一个新的咖啡因实例,包括强键、强值和任何类型的自动逐出。
请注意,虽然此返回类型是无效的,但是#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
@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 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 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 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
@GwtIncompatible // java.time.Duration
public void testRefresh_zero_duration() {
Caffeine<Object, Object> builder = Caffeine.newBuilder();
try {
builder.refreshAfterWrite(java.time.Duration.ZERO);
fail();
} catch (IllegalArgumentException expected) {
}
}
代码示例来源: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
public void testTimeToIdleAndToLive() {
CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterWrite(1, NANOSECONDS)
.expireAfterAccess(1, NANOSECONDS),
identityLoader());
// well, it didn't blow up.
}
代码示例来源:origin: ben-manes/caffeine
public void testGet_computeNull() {
LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.maximumSize(0)
.removalListener(listener),
constantLoader(null));
try {
cache.getUnchecked(new Object());
fail();
} catch (InvalidCacheLoadException e) { /* expected */}
assertTrue(listener.isEmpty());
checkEmpty(cache);
}
代码示例来源:origin: ben-manes/caffeine
public void testParse_recordStats() {
CaffeineSpec spec = parse("recordStats");
assertTrue(spec.recordStats);
assertCaffeineEquivalence(Caffeine.newBuilder().recordStats(), Caffeine.from(spec));
}
代码示例来源:origin: ben-manes/caffeine
public void testEviction_overflow() {
CountingRemovalListener<Object, Object> removalListener = countingRemovalListener();
IdentityLoader<Object> loader = identityLoader();
LoadingCache<Object, Object> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.maximumWeight(1L << 31)
.executor(MoreExecutors.directExecutor())
.weigher(constantWeigher(Integer.MAX_VALUE))
.removalListener(removalListener), loader);
cache.getUnchecked(objectWithHash(0));
cache.getUnchecked(objectWithHash(0));
assertEquals(1, removalListener.getCount());
}
代码示例来源:origin: ben-manes/caffeine
public void testParse_accessExpirationSeconds() {
CaffeineSpec spec = parse("expireAfterAccess=10s");
assertEquals(TimeUnit.SECONDS, spec.expireAfterAccessTimeUnit);
assertEquals(10L, spec.expireAfterAccessDuration);
assertCaffeineEquivalence(
Caffeine.newBuilder().expireAfterAccess(10L, TimeUnit.SECONDS),
Caffeine.from(spec));
}
代码示例来源:origin: ben-manes/caffeine
public void testExpiringGet_expireAfterAccess() {
FakeTicker ticker = new FakeTicker();
CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
WatchedCreatorLoader loader = new WatchedCreatorLoader();
LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
.executor(MoreExecutors.directExecutor())
.removalListener(removalListener)
.ticker(ticker::read),
loader);
runExpirationTest(cache, loader, ticker, removalListener);
}
代码示例来源:origin: spring-projects/spring-framework
@Test
public void changeCaffeineRecreateCache() {
CaffeineCacheManager cm = new CaffeineCacheManager("c1");
Cache cache1 = cm.getCache("c1");
Caffeine<Object, Object> caffeine = Caffeine.newBuilder().maximumSize(10);
cm.setCaffeine(caffeine);
Cache cache1x = cm.getCache("c1");
assertTrue(cache1x != cache1);
cm.setCaffeine(caffeine); // Set same instance
Cache cache1xx = cm.getCache("c1");
assertSame(cache1x, cache1xx);
}
代码示例来源:origin: ben-manes/caffeine
public void testParse_empty() {
CaffeineSpec spec = parse("");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(spec.maximumSize, UNSET_INT);
assertEquals(spec.maximumWeight, UNSET_INT);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterAccessTimeUnit);
assertNull(spec.expireAfterWriteTimeUnit);
assertNull(spec.refreshAfterWriteTimeUnit);
assertCaffeineEquivalence(Caffeine.newBuilder(), Caffeine.from(spec));
}
内容来源于网络,如有侵权,请联系作者删除!