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

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

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

Cache.estimatedSize介绍

[英]Returns the approximate number of entries in this cache. The value returned is an estimate; the actual count may differ if there are concurrent insertions or removals, or if some entries are pending removal due to expiration or weak/soft reference collection. In the case of stale entries this inaccuracy can be mitigated by performing a #cleanUp() first.
[中]返回此缓存中的大致条目数。返回的值是一个估计值;如果存在并发插入或删除,或者某些条目由于过期或弱/软引用集合而挂起删除,则实际计数可能不同。对于过时的条目,可以通过先执行#cleanUp()来减少这种不准确。

代码示例

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

/** Ensures that that all the pending work is performed (Guava limits work per cycle). */
 private static void awaitFullCleanup(Cache<?, ?> cache) {
  for (;;) {
   long size = cache.estimatedSize();
   cache.cleanUp();

   if (size == cache.estimatedSize()) {
    break;
   }
  }
 }
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replaceConditionally_sameWeight(
  Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 assertThat(cache.asMap().replace("a", asList(1, 2, 3), asList(4, 5, 6)), is(true));
 assertThat(cache.estimatedSize(), is(2L));
 assertThat(eviction.weightedSize().getAsLong(), is(4L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void remove(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 assertThat(cache.asMap().remove("a"), is(asList(1, 2, 3)));
 assertThat(cache.estimatedSize(), is(1L));
 assertThat(eviction.weightedSize().getAsLong(), is(1L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void removeConditionally_fails(
  Cache<String, List<Integer>> cache, CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 assertThat(cache.asMap().remove("a", asList(-1, -2, -3)), is(false));
 assertThat(cache.estimatedSize(), is(2L));
 assertThat(eviction.weightedSize().getAsLong(), is(4L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void put(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.put("a", asList(1, 2, 3));
 assertThat(cache.estimatedSize(), is(1L));
 assertThat(eviction.weightedSize().getAsLong(), is(3L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void replace_changeWeight(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 cache.asMap().replace("a", asList(-1, -2, -3, -4));
 assertThat(cache.estimatedSize(), is(2L));
 assertThat(eviction.weightedSize().getAsLong(), is(5L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  weigher = CacheWeigher.COLLECTION, population = Population.EMPTY,
  keys = ReferenceType.STRONG, values = ReferenceType.STRONG)
public void invalidateAll(Cache<String, List<Integer>> cache,
  CacheContext context, Eviction<?, ?> eviction) {
 cache.putAll(ImmutableMap.of("a", asList(1, 2, 3), "b", asList(1)));
 cache.invalidateAll();
 assertThat(cache.estimatedSize(), is(0L));
 assertThat(eviction.weightedSize().getAsLong(), is(0L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
  maximumSize = Maximum.FULL, weigher = CacheWeigher.TEN)
public void weightedSize(Cache<Integer, Integer> cache, CacheContext context,
  Eviction<Integer, Integer> eviction) {
 long weightedSize = 0;
 for (Integer key : cache.asMap().keySet()) {
  weightedSize += eviction.weightOf(key).getAsInt();
 }
 assertThat(weightedSize, is(eviction.weightedSize().getAsLong()));
 assertThat(eviction.weightedSize().getAsLong(), is(10 * cache.estimatedSize()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
  maximumSize = Maximum.FULL, removalListener = Listener.REJECTING)
public void maximumSize_increase_max(Cache<Integer, Integer> cache,
  CacheContext context, Eviction<Integer, Integer> eviction) {
 eviction.setMaximum(Long.MAX_VALUE);
 assertThat(cache.estimatedSize(), is(context.initialSize()));
 assertThat(eviction.getMaximum(), is(Long.MAX_VALUE - Integer.MAX_VALUE)); // impl detail
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_insert_failure_before(AsyncCache<Integer, Integer> cache,
  CacheContext context) {
 CompletableFuture<Integer> failedFuture = CompletableFuture.completedFuture(null);
 failedFuture.completeExceptionally(new IllegalStateException());
 cache.put(context.absentKey(), failedFuture);
 assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
 assertThat(cache.synchronous().estimatedSize(), is(context.initialSize()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL },
  mustExpireWithAnyOf = { AFTER_ACCESS, AFTER_WRITE, VARIABLE },
  expiry = { CacheExpiry.DISABLED, CacheExpiry.CREATE, CacheExpiry.WRITE, CacheExpiry.ACCESS },
  expireAfterAccess = {Expire.DISABLED, Expire.ONE_MINUTE},
  expireAfterWrite = {Expire.DISABLED, Expire.ONE_MINUTE},
  expiryTime = Expire.ONE_MINUTE)
public void estimatedSize(Cache<Integer, Integer> cache, CacheContext context) {
 context.ticker().advance(1, TimeUnit.MINUTES);
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

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

@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void estimatedSize(Cache<Integer, Integer> cache, CacheContext context) {
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_replace_failure_before(AsyncCache<Integer, Integer> cache,
  CacheContext context) {
 CompletableFuture<Integer> failedFuture = CompletableFuture.completedFuture(null);
 failedFuture.completeExceptionally(new IllegalStateException());
 cache.put(context.middleKey(), failedFuture);
 assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
 assertThat(cache.synchronous().estimatedSize(), is(context.initialSize() - 1));
}

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

@CheckNoWriter
@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.FULL },
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_replace_failure_after(AsyncCache<Integer, Integer> cache,
  CacheContext context) {
 CompletableFuture<Integer> failedFuture = CompletableFuture.completedFuture(null);
 cache.put(context.middleKey(), failedFuture);
 failedFuture.completeExceptionally(new IllegalStateException());
 assertThat(cache.getIfPresent(context.absentKey()), is(nullValue()));
 assertThat(cache.synchronous().estimatedSize(), is(context.initialSize() - 1));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, maximumSize = Maximum.FULL,
  removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void maximumSize_increase(Cache<Integer, Integer> cache,
  CacheContext context, Eviction<Integer, Integer> eviction) {
 eviction.setMaximum(2 * context.maximumWeightOrSize());
 assertThat(cache.estimatedSize(), is(context.initialSize()));
 assertThat(eviction.getMaximum(), is(2 * context.maximumWeightOrSize()));
}

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

@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void putAll_empty(Cache<Integer, Integer> cache, CacheContext context) {
 cache.putAll(new HashMap<>());
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(population = { Population.SINGLETON, Population.PARTIAL, Population.FULL })
public void invalidateAll_full(Cache<Integer, Integer> cache, CacheContext context) {
 cache.invalidateAll(context.original().keySet());
 assertThat(cache.estimatedSize(), is(0L));
 assertThat(cache, hasRemovalNotifications(context,
   context.original().size(), RemovalCause.EXPLICIT));
 verifyWriter(context, (verifier, writer) -> {
  verifier.deletedAll(context.original(), RemovalCause.EXPLICIT);
 });
}

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

@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void invalidate_absent(Cache<Integer, Integer> cache, CacheContext context) {
 cache.invalidate(context.absentKey());
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
  mustExpireWithAnyOf = AFTER_WRITE, expireAfterWrite = Expire.ONE_MINUTE)
public void setExpiresAfter(Cache<Integer, Integer> cache, CacheContext context,
  @ExpireAfterWrite Expiration<Integer, Integer> expireAfterWrite) {
 expireAfterWrite.setExpiresAfter(2, TimeUnit.MINUTES);
 assertThat(expireAfterWrite.getExpiresAfter(TimeUnit.MINUTES), is(2L));
 context.ticker().advance(90, TimeUnit.SECONDS);
 cache.cleanUp();
 assertThat(cache.estimatedSize(), is(context.initialSize()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(removalListener = { Listener.DEFAULT, Listener.REJECTING })
public void put_insert(AsyncCache<Integer, Integer> cache, CacheContext context) {
 CompletableFuture<Integer> value = CompletableFuture.completedFuture(context.absentValue());
 cache.put(context.absentKey(), value);
 assertThat(cache.synchronous().estimatedSize(), is(context.initialSize() + 1));
 assertThat(context, both(hasMissCount(0)).and(hasHitCount(0)));
 assertThat(context, both(hasLoadSuccessCount(1)).and(hasLoadFailureCount(0)));
 assertThat(cache.synchronous().getIfPresent(context.absentKey()), is(context.absentValue()));
}

相关文章