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

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

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

Caffeine.weigher介绍

[英]Specifies the weigher to use in determining the weight of entries. Entry weight is taken into consideration by #maximumWeight(long) when determining which entries to evict, and use of this method requires a corresponding call to #maximumWeight(long) prior to calling #build. Weights are measured and recorded when entries are inserted into or updated in the cache, and are thus effectively static during the lifetime of a cache entry.

When the weight of an entry is zero it will not be considered for size-based eviction (though it still may be evicted by other means).

Important note: Instead of returning this as a Caffeine instance, this method returns Caffeine. From this point on, either the original reference or the returned reference may be used to complete configuration and build the cache, but only the "generic" one is type-safe. That is, it will properly prevent you from building caches whose key or value types are incompatible with the types accepted by the weigher already provided; the Caffeine type cannot do this. For best results, simply use the standard method-chaining idiom, as illustrated in the documentation at top, configuring a Caffeine and building your Cache all in a single statement.

Warning: if you ignore the above advice, and use this Caffeine to build a cache whose key or value type is incompatible with the weigher, you will likely experience a ClassCastException at some undefined point in the future.
[中]指定用于确定条目重量的秤。在确定要逐出哪些条目时,#maximumWeight(long)会考虑条目权重,使用此方法需要在调用#build之前对#maximumWeight(long)进行相应的调用。当条目插入缓存或在缓存中更新时,会测量和记录权重,因此在缓存条目的生存期内,权重实际上是静态的。
当条目的权重为零时,将不考虑基于大小的逐出(尽管仍可以通过其他方式逐出)。
重要提示:此方法不返回this作为咖啡因实例,而是返回咖啡因。从现在开始,可以使用原始引用或返回的引用来完成配置和构建缓存,但只有“泛型”引用是类型安全的。也就是说,它将适当地防止您构建密钥或值类型与已提供的称重机接受的类型不兼容的缓存;咖啡因类型不能做到这一点。为了获得最佳效果,只需使用标准的方法链接习惯用法(如顶部文档所示),在一条语句中配置一个链接并构建缓存。
警告:如果您忽略上述建议,并使用此命令来构建其键或值类型与称重器不兼容的缓存,您可能会在将来的某个未定义点遇到ClassCastException。

代码示例

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

@Test(expectedExceptions = NullPointerException.class)
public void weigher_null() {
 Caffeine.newBuilder().weigher(null);
}

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

/** Configures the maximum weight and returns if set. */
private boolean configureMaximumWeight() {
 if (config.getMaximumWeight().isPresent()) {
  caffeine.maximumWeight(config.getMaximumWeight().getAsLong());
  Weigher<K, V> weigher = config.getWeigherFactory().map(Factory::create)
    .orElseThrow(() -> new IllegalStateException("Weigher not configured"));
  caffeine.weigher((K key, Expirable<V> expirable) -> {
   return weigher.weigh(key, expirable.get());
  });
 }
 return config.getMaximumWeight().isPresent();
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void weigher_twice() {
 Caffeine.newBuilder().weigher(Weigher.singletonWeigher()).weigher(Weigher.singletonWeigher());
}

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

@Test
public void fromString_lenientParsing() {
 Caffeine.from("maximumSize=100").weigher((k, v) -> 0).build();
}

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

@Test
public void weigher() {
 Weigher<Object, Object> weigher = (k, v) -> 0;
 Caffeine<?, ?> builder = Caffeine.newBuilder().maximumWeight(0).weigher(weigher);
 assertThat(builder.weigher, is(sameInstance(weigher)));
 builder.build();
}

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

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

代码示例来源: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

@Test
public void fromSpec_lenientParsing() {
 Caffeine.from(CaffeineSpec.parse("maximumSize=100")).weigher((k, v) -> 0).build();
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void maximumSize_weigher() {
 Caffeine.newBuilder().weigher(Weigher.singletonWeigher()).maximumSize(1);
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void weigher_maximumSize() {
 Caffeine.newBuilder().maximumSize(1).weigher(Weigher.singletonWeigher());
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void weigher_noMaximumWeight() {
 Caffeine.newBuilder().weigher(Weigher.singletonWeigher()).build();
}

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

public void testMaximumWeight_withWeigher() {
 Caffeine<Object, Object> builder = Caffeine.from(parse("maximumWeight=9000"));
 builder.weigher((k, v) -> 42).build(k -> null);
}

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

public void testMaximumSize_withWeigher() {
 Caffeine<Object, Object> builder = Caffeine.from(parse("maximumSize=9000"));
 builder.weigher((k, v) -> 42).build(k -> null);
}

代码示例来源:origin: apache/incubator-druid

public static CaffeineCache create(final CaffeineCacheConfig config, final Executor executor)
{
 Caffeine<Object, Object> builder = Caffeine.newBuilder().recordStats();
 if (config.getExpireAfter() >= 0) {
  builder
    .expireAfterAccess(config.getExpireAfter(), TimeUnit.MILLISECONDS);
 }
 if (config.getSizeInBytes() >= 0) {
  builder.maximumWeight(config.getSizeInBytes());
 } else {
  builder.maximumWeight(Math.min(MAX_DEFAULT_BYTES, JvmUtils.getRuntimeInfo().getMaxHeapSizeBytes() / 10));
 }
 builder
   .weigher((NamedKey key, byte[] value) -> value.length
                        + key.key.length
                        + key.namespace.length() * Character.BYTES
                        + FIXED_COST)
   .executor(executor);
 return new CaffeineCache(builder.build(), config);
}

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

@Test
public void putWeighted_noOverflow() {
 Cache<Integer, Integer> cache = Caffeine.newBuilder()
   .executor(CacheExecutor.DIRECT.create())
   .weigher(CacheWeigher.MAX_VALUE)
   .maximumWeight(Long.MAX_VALUE)
   .build();
 BoundedLocalCache<Integer, Integer> map = asBoundedLocalCache(cache);
 cache.put(1, 1);
 map.lazySetEdenMaximum(0L);
 map.lazySetWeightedSize(BoundedLocalCache.MAXIMUM_CAPACITY);
 cache.put(2, 2);
 assertThat(map.size(), is(1));
 assertThat(map.adjustedWeightedSize(), is(BoundedLocalCache.MAXIMUM_CAPACITY));
}

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

@GwtIncompatible("weigher")
public void testWeigher_withMaximumSize() {
 try {
  Caffeine.newBuilder()
    .weigher(constantWeigher(42))
    .maximumSize(1);
  fail();
 } catch (IllegalStateException expected) {}
 try {
  Caffeine.newBuilder()
    .maximumSize(1)
    .weigher(constantWeigher(42));
  fail();
 } catch (IllegalStateException expected) {}
}

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

public void testEviction_maxWeightOneSegment() {
 IdentityLoader<Integer> loader = identityLoader();
 LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .executor(MoreExecutors.directExecutor())
   .maximumWeight(2 * MAX_SIZE)
   .weigher(constantWeigher(2)), 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 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

@GwtIncompatible("weigher")
public void testWeigher_withoutMaximumWeight() {
 Caffeine<Object, Object> builder = Caffeine.newBuilder()
   .weigher(constantWeigher(42));
 try {
  CaffeinatedGuava.build(builder, identityLoader());
  fail();
 } catch (IllegalStateException expected) {}
}

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

public void testEviction_maxWeight() {
 CountingRemovalListener<Integer, Integer> removalListener = countingRemovalListener();
 IdentityLoader<Integer> loader = identityLoader();
 LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .maximumWeight(2 * MAX_SIZE)
   .weigher(constantWeigher(2))
   .executor(MoreExecutors.directExecutor())
   .removalListener(removalListener), loader);
 for (int i = 0; i < 2 * MAX_SIZE; i++) {
  cache.getUnchecked(i);
  assertTrue(cache.size() <= MAX_SIZE);
 }
 assertEquals(MAX_SIZE, cache.size());
 assertEquals(MAX_SIZE, removalListener.getCount());
 CacheTesting.checkValidState(cache);
}

相关文章