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

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

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

Caffeine.weakKeys介绍

[英]Specifies that each key (not value) stored in the cache should be wrapped in a WeakReference (by default, strong references are used).

Warning: when this method is used, the resulting cache will use identity ( ==) comparison to determine equality of keys. Its Cache#asMap view will therefore technically violate the Map specification (in the same way that IdentityHashMapdoes).

Entries with keys that have been garbage collected may be counted in Cache#estimatedSize(), but will never be visible to read or write operations; such entries are cleaned up as part of the routine maintenance described in the class javadoc.

This feature cannot be used in conjunction with #writer.
[中]指定缓存中存储的每个键(不是值)都应包装在WeakReference中(默认情况下,使用强引用)。
警告:使用此方法时,生成的缓存将使用标识(=)比较来确定键的相等性。因此,它的Cache#asMap视图在技术上违反了Map规范(与identityhashmapp的做法相同)。
带有已被垃圾收集的密钥的条目可能会在缓存#estimatedSize()中计数,但决不会对读或写操作可见;这些条目作为javadoc类中描述的日常维护的一部分进行清理。
此功能不能与#writer一起使用。

代码示例

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

@Test(expectedExceptions = IllegalStateException.class)
public void weakKeys_twice() {
 Caffeine.newBuilder().weakKeys().weakKeys();
}

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

@GwtIncompatible("weakKeys")
public void testKeyStrengthSetTwice() {
 Caffeine<Object, Object> builder1 = Caffeine.newBuilder().weakKeys();
 try {
  builder1.weakKeys();
  fail();
 } catch (IllegalStateException expected) {}
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void weakKeys_writer() {
 Caffeine.newBuilder().writer(writer).weakKeys();
}

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

@Test
public void weakKeys() {
 Caffeine.newBuilder().weakKeys().build();
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void writer_weakKeys() {
 Caffeine.newBuilder().writer(writer).weakKeys();
}

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

@Test
public void configured() {
 Caffeine<Object, Object> configured = Caffeine.newBuilder()
   .initialCapacity(1).weakKeys()
   .expireAfterAccess(1, TimeUnit.SECONDS).expireAfterWrite(1, TimeUnit.SECONDS)
   .removalListener((k, v, c) -> {}).recordStats();
 assertThat(configured.build(), is(not(nullValue())));
 assertThat(configured.buildAsync(), is(not(nullValue())));
 assertThat(configured.build(loader), is(not(nullValue())));
 assertThat(configured.buildAsync(loader), is(not(nullValue())));
 assertThat(configured.refreshAfterWrite(1, TimeUnit.SECONDS).toString(),
   is(not(Caffeine.newBuilder().toString())));
 assertThat(Caffeine.newBuilder().maximumSize(1).toString(),
   is(not(Caffeine.newBuilder().maximumWeight(1).toString())));
}

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

.weakKeys()
  .weakValues(), recursiveLoader);
cacheRef.set(recursiveCache);
  .weakKeys()
  .weakValues(), recursiveLoader);
cacheRef.set(recursiveCache);

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

builder.weakKeys();

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

builder.weakKeys();

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

builder.weakKeys();

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

public void testParse_weakKeys() {
 CaffeineSpec spec = parse("weakKeys");
 assertEquals(spec.initialCapacity, UNSET_INT);
 assertEquals(spec.maximumSize, UNSET_INT);
 assertEquals(spec.maximumWeight, UNSET_INT);
 assertEquals(Strength.WEAK, spec.keyStrength);
 assertNull(spec.valueStrength);
 assertNull(spec.expireAfterWriteTimeUnit);
 assertNull(spec.expireAfterAccessTimeUnit);
 assertNull(spec.refreshAfterWriteTimeUnit);
 assertCaffeineEquivalence(
   Caffeine.newBuilder().weakKeys(), Caffeine.from(spec));
}

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

public void testParse_whitespaceAllowed() {
 CaffeineSpec spec = parse(" initialCapacity=10,\nmaximumSize=20,\t\r"
   + "weakKeys \t ,softValues \n , \r  expireAfterWrite \t =  15s\n\n");
 assertEquals(10, spec.initialCapacity);
 assertEquals(20, spec.maximumSize);
 assertEquals(spec.maximumWeight, UNSET_INT);
 assertEquals(Strength.WEAK, spec.keyStrength);
 assertEquals(Strength.SOFT, spec.valueStrength);
 assertEquals(TimeUnit.SECONDS, spec.expireAfterWriteTimeUnit);
 assertEquals(15L, spec.expireAfterWriteDuration);
 assertNull(spec.expireAfterAccessTimeUnit);
 Caffeine<?, ?> expected = Caffeine.newBuilder()
   .initialCapacity(10)
   .maximumSize(20)
   .weakKeys()
   .softValues()
   .expireAfterWrite(15L, TimeUnit.SECONDS);
 assertCaffeineEquivalence(expected, Caffeine.from(spec));
}

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

public void testParse_multipleKeys() {
 CaffeineSpec spec = parse("initialCapacity=10,maximumSize=20,"
   + "weakKeys,weakValues,expireAfterAccess=10m,expireAfterWrite=1h");
 assertEquals(10, spec.initialCapacity);
 assertEquals(20, spec.maximumSize);
 assertEquals(spec.maximumWeight, UNSET_INT);
 assertEquals(Strength.WEAK, spec.keyStrength);
 assertEquals(Strength.WEAK, spec.valueStrength);
 assertEquals(TimeUnit.HOURS, spec.expireAfterWriteTimeUnit);
 assertEquals(TimeUnit.MINUTES, spec.expireAfterAccessTimeUnit);
 assertEquals(1L, spec.expireAfterWriteDuration);
 assertEquals(10L, spec.expireAfterAccessDuration);
 Caffeine<?, ?> expected = Caffeine.newBuilder()
   .initialCapacity(10)
   .maximumSize(20)
   .weakKeys()
   .weakValues()
   .expireAfterAccess(10L, TimeUnit.MINUTES)
   .expireAfterWrite(1L, TimeUnit.HOURS);
 assertCaffeineEquivalence(expected, Caffeine.from(spec));
}

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

builder.weakKeys();
} else if (context.keyStrength == ReferenceType.SOFT) {
 throw new IllegalStateException();

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

builder.weakKeys();

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

builder.weakKeys();

代码示例来源:origin: net.sourceforge.owlapi/owlapi-distribution

static <Q, T> LoadingCache<Q, T> build(CacheLoader<Q, T> c) {
  return Caffeine.newBuilder().weakKeys().softValues().build(c);
}

代码示例来源:origin: net.sourceforge.owlapi/owlapi-impl

static <Q, T> LoadingCache<Q, T> build(CacheLoader<Q, T> c) {
  return Caffeine.newBuilder().weakKeys().softValues().build(c);
}

代码示例来源:origin: owlcs/owlapi

static <Q, T> LoadingCache<Q, T> build(CacheLoader<Q, T> c) {
  return Caffeine.newBuilder().weakKeys().softValues().build(c);
}

相关文章