本文整理了Java中com.github.benmanes.caffeine.cache.Caffeine.maximumSize()
方法的一些代码示例,展示了Caffeine.maximumSize()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Caffeine.maximumSize()
方法的具体详情如下:
包路径:com.github.benmanes.caffeine.cache.Caffeine
类名称:Caffeine
方法名:maximumSize
[英]Specifies the maximum number of entries the cache may contain. Note that the cache may evict an entry before this limit is exceeded or temporarily exceed the threshold while evicting. As the cache size grows close to the maximum, the cache evicts entries that are less likely to be used again. For example, the cache may evict an entry because it hasn't been used recently or very often.
When size is zero, elements will be evicted immediately after being loaded into the cache. This can be useful in testing, or to disable caching temporarily without a code change.
This feature cannot be used in conjunction with #maximumWeight.
[中]指定缓存可能包含的最大条目数。请注意,缓存可能会在超过此限制之前逐出条目,或在逐出时暂时超过阈值。当缓存大小接近最大值时,缓存会逐出不太可能再次使用的条目。例如,缓存可能会逐出一个条目,因为它最近或经常没有被使用。
当大小为零时,元素将在加载到缓存后立即被逐出。这在测试中很有用,或者在不更改代码的情况下暂时禁用缓存。
此功能不能与#最大重量一起使用。
代码示例来源:origin: ben-manes/caffeine
public void testMaximumSize_setTwice() {
Caffeine<Object, Object> builder = Caffeine.newBuilder().maximumSize(16);
try {
// even to the same value is not allowed
builder.maximumSize(16);
fail();
} catch (IllegalStateException expected) {}
}
代码示例来源:origin: ben-manes/caffeine
@Test(expectedExceptions = IllegalArgumentException.class)
public void maximumSize_negative() {
Caffeine.newBuilder().maximumSize(-1);
}
代码示例来源:origin: ben-manes/caffeine
/** Configures the maximum size and returns if set. */
private boolean configureMaximumSize() {
if (config.getMaximumSize().isPresent()) {
caffeine.maximumSize(config.getMaximumSize().getAsLong());
}
return config.getMaximumSize().isPresent();
}
代码示例来源:origin: ben-manes/caffeine
@Test(expectedExceptions = IllegalStateException.class)
public void maximumWeight_maximumSize() {
Caffeine.newBuilder().maximumSize(1).maximumWeight(1);
}
代码示例来源:origin: ben-manes/caffeine
public void testMaximumSize_negative() {
Caffeine<Object, Object> builder = Caffeine.newBuilder();
try {
builder.maximumSize(-1);
fail();
} catch (IllegalArgumentException expected) {}
}
代码示例来源:origin: ben-manes/caffeine
@Test
public void maximumSize_small() {
Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(0);
assertThat(builder.maximumSize, is(0L));
Cache<?, ?> cache = builder.build();
assertThat(cache.policy().eviction().get().getMaximum(), is(0L));
}
代码示例来源:origin: ben-manes/caffeine
@Test
public void maximumSize_large() {
Caffeine<?, ?> builder = Caffeine.newBuilder().maximumSize(Integer.MAX_VALUE);
assertThat(builder.maximumSize, is((long) Integer.MAX_VALUE));
Cache<?, ?> cache = builder.build();
assertThat(cache.policy().eviction().get().getMaximum(), is((long) Integer.MAX_VALUE));
}
代码示例来源:origin: ben-manes/caffeine
public void testComputeIfAbsentEviction() {
Cache<String, String> c = CaffeinatedGuava.build(
Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1));
assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
assertThat(c.size()).isEqualTo(1);
assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo("");
}
代码示例来源:origin: ben-manes/caffeine
@Override
public void setUp() throws Exception {
super.setUp();
this.cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.expireAfterAccess(500000, TimeUnit.MILLISECONDS)
.executor(MoreExecutors.directExecutor())
.maximumSize(count));
}
代码示例来源:origin: ben-manes/caffeine
public void testComputeEviction() {
Cache<String, String> c = CaffeinatedGuava.build(
Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1));
assertThat(c.asMap().compute("hash-1", (k, v) -> "a")).isEqualTo("a");
assertThat(c.asMap().compute("hash-1", (k, v) -> "b")).isEqualTo("b");
assertThat(c.asMap().compute("hash-1", (k, v) -> "c")).isEqualTo("c");
assertThat(c.size()).isEqualTo(1);
assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo("");
}
代码示例来源: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
private static <K, V> ConcurrentMap<K, V> map() {
return Caffeine.newBuilder()
.maximumSize(Integer.MAX_VALUE)
.<K, V>build().asMap();
}
代码示例来源:origin: ben-manes/caffeine
public CaffeinePolicy(Config config) {
policyStats = new PolicyStats("product.Caffeine");
BasicSettings settings = new BasicSettings(config);
maximumSize = settings.maximumSize();
cache = Caffeine.newBuilder()
.initialCapacity(maximumSize)
.maximumSize(maximumSize)
.executor(Runnable::run)
.build();
}
代码示例来源:origin: ben-manes/caffeine
@GwtIncompatible("maximumWeight")
public void testMaximumSize_andWeight() {
Caffeine<Object, Object> builder = Caffeine.newBuilder().maximumSize(16);
try {
builder.maximumWeight(16);
fail();
} catch (IllegalStateException expected) {}
}
代码示例来源:origin: ben-manes/caffeine
public void testEviction_maxSizeOneSegment() {
IdentityLoader<Integer> loader = identityLoader();
LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
.executor(MoreExecutors.directExecutor())
.maximumSize(MAX_SIZE), loader);
for (int i = 0; i < 2 * MAX_SIZE; i++) {
cache.getUnchecked(i);
assertEquals(Math.min(i + 1, MAX_SIZE), cache.size());
}
assertEquals(MAX_SIZE, cache.size());
CacheTesting.checkValidState(cache);
}
代码示例来源: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
private static void addBoundedTests(TestSuite suite) throws Exception {
suite.addTest(MapTestFactory.suite("BoundedCache", () -> {
Cache<String, String> cache = Caffeine.newBuilder().maximumSize(Long.MAX_VALUE).build();
return cache.asMap();
}));
suite.addTest(MapTestFactory.suite("BoundedAsyncCache", () -> {
AsyncLoadingCache<String, String> cache = Caffeine.newBuilder()
.maximumSize(Long.MAX_VALUE)
.buildAsync(key -> null);
return cache.synchronous().asMap();
}));
}
代码示例来源: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 testCaffeineFrom_string() {
Caffeine<?, ?> fromString = Caffeine.from(
"initialCapacity=10,maximumSize=20,weakKeys,weakValues,expireAfterAccess=10m");
Caffeine<?, ?> expected = Caffeine.newBuilder()
.initialCapacity(10)
.maximumSize(20)
.weakKeys()
.weakValues()
.expireAfterAccess(10L, TimeUnit.MINUTES);
assertCaffeineEquivalence(expected, fromString);
}
代码示例来源:origin: ben-manes/caffeine
public void testParse_maximumSize() {
CaffeineSpec spec = parse("maximumSize=9000");
assertEquals(spec.initialCapacity, UNSET_INT);
assertEquals(9000, spec.maximumSize);
assertNull(spec.keyStrength);
assertNull(spec.valueStrength);
assertNull(spec.expireAfterWriteTimeUnit);
assertNull(spec.expireAfterAccessTimeUnit);
assertNull(spec.refreshAfterWriteTimeUnit);
assertCaffeineEquivalence(
Caffeine.newBuilder().maximumSize(9000), Caffeine.from(spec));
}
内容来源于网络,如有侵权,请联系作者删除!