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

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

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

Cache.policy介绍

[英]Returns access to inspect and perform low-level operations on this cache based on its runtime characteristics. These operations are optional and dependent on how the cache was constructed and what abilities the implementation exposes.
[中]返回基于此缓存的运行时特征对其进行检查和执行低级操作的访问权限。这些操作是可选的,取决于缓存的构造方式以及实现公开的功能。

代码示例

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

@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.DISABLED, weigher = CacheWeigher.DEFAULT,
  expireAfterAccess = Expire.DISABLED, expireAfterWrite = Expire.DISABLED,
  refreshAfterWrite = Expire.DISABLED, keys = ReferenceType.STRONG,
  values = ReferenceType.STRONG)
@Test(dataProvider = "caches")
public void noPolicy(Cache<Integer, Integer> cache, CacheContext context) {
 assertThat(cache.policy().eviction(), is(Optional.empty()));
 assertThat(cache.policy().expireAfterWrite(), is(Optional.empty()));
 assertThat(cache.policy().expireAfterAccess(), is(Optional.empty()));
 assertThat(cache.policy().refreshAfterWrite(), is(Optional.empty()));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine,
  population = Population.EMPTY, expiry = CacheExpiry.DISABLED)
public void expireVariably_notEnabled(Cache<Integer, Integer> cache) {
 assertThat(cache.policy().expireVariably(), is(Optional.empty()));
}

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

private void checkContainsInOrder(Cache<Integer, Integer> cache, Integer... expect) {
 cache.cleanUp();
 List<Integer> evictionList = ImmutableList.copyOf(
   cache.policy().eviction().get().coldest(Integer.MAX_VALUE).keySet());
 assertThat(cache.asMap().size(), is(equalTo(expect.length)));
 assertThat(cache.asMap().keySet(), containsInAnyOrder(expect));
 assertThat(evictionList, is(equalTo(asList(expect))));
}

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

/** Returns the fixed expiration policy for the given parameter. */
private static Policy.Expiration<Integer, Integer> expirationPolicy(
  Parameter parameter, Cache<Integer, Integer> cache) {
 if (parameter.isAnnotationPresent(ExpireAfterAccess.class)) {
  return cache.policy().expireAfterAccess().get();
 } else if (parameter.isAnnotationPresent(ExpireAfterWrite.class)) {
  return cache.policy().expireAfterWrite().get();
 } else if (parameter.isAnnotationPresent(RefreshAfterWrite.class)) {
  return cache.policy().refreshAfterWrite().get();
 }
 throw new AssertionError("Expiration parameter must have a qualifier annotation");
}

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

@Test
public void expireAfterWrite_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .expireAfterWrite(Integer.MAX_VALUE, TimeUnit.NANOSECONDS);
 assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}

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

@Test
public void expireAfterWrite_duration_small() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(0));
 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 expireAfterAccess_duration_small() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(Duration.ofMillis(0));
 assertThat(builder.expireAfterAccessNanos, is(0L));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L));
}

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

@Test
public void expireAfterAccess_small() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().expireAfterAccess(0, TimeUnit.MILLISECONDS);
 assertThat(builder.expireAfterAccessNanos, is(0L));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.MILLISECONDS), is(0L));
}

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

@Test
public void expireAfterAccess_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .expireAfterAccess(Integer.MAX_VALUE, TimeUnit.NANOSECONDS);
 assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}

代码示例来源: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 expireAfterWrite_duration_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .expireAfterWrite(Duration.ofNanos(Integer.MAX_VALUE));
 assertThat(builder.expireAfterWriteNanos, is((long) Integer.MAX_VALUE));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterWrite().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}

代码示例来源: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 expireAfterAccess_duration_large() {
 Caffeine<?, ?> builder = Caffeine.newBuilder()
   .expireAfterAccess(Duration.ofNanos(Integer.MAX_VALUE));
 assertThat(builder.expireAfterAccessNanos, is((long) Integer.MAX_VALUE));
 Expiration<?, ?> expiration = builder.build().policy().expireAfterAccess().get();
 assertThat(expiration.getExpiresAfter(TimeUnit.NANOSECONDS), is((long) Integer.MAX_VALUE));
}

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

@Test(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE,
  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})
public void putIfAbsent_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
 cache.put(1, ImmutableList.of(1));
 context.ticker().advance(1, TimeUnit.MINUTES);
 cache.asMap().putIfAbsent(1, ImmutableList.of(1, 2, 3));
 assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
}

代码示例来源: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(dataProvider = "caches")
@CacheSpec(implementation = Implementation.Caffeine, population = Population.EMPTY,
  maximumSize = Maximum.FULL, weigher = CacheWeigher.COLLECTION, expiryTime = Expire.ONE_MINUTE,
  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})
public void put_weighted(Cache<Integer, List<Integer>> cache, CacheContext context) {
 cache.put(1, ImmutableList.of(1));
 context.ticker().advance(1, TimeUnit.MINUTES);
 cache.put(1, ImmutableList.of(1, 2, 3));
 assertThat(cache.policy().eviction().get().weightedSize().getAsLong(), is(3L));
}

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

@Test(dataProvider = "caches")
@CacheSpec(compute = Compute.SYNC, implementation = Implementation.Caffeine,
  population = Population.EMPTY, maximumSize = Maximum.FULL)
public void drain_blocksCapacity(Cache<Integer, Integer> cache, CacheContext context) {
 BoundedLocalCache<Integer, Integer> localCache = asBoundedLocalCache(cache);
 checkDrainBlocks(localCache, () -> cache.policy().eviction().ifPresent(
   policy -> policy.setMaximum(0L)));
}

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

@CacheSpec
@CheckNoWriter @CheckNoStats
@Test(dataProvider = "caches")
public void stats(Cache<Integer, Integer> cache, CacheContext context) {
 CacheStats stats = cache.stats()
   .plus(new CacheStats(1, 2, 3, 4, 5, 6, 7)
   .minus(new CacheStats(6, 5, 4, 3, 2, 1, 0)));
 assertThat(stats, is(new CacheStats(0, 0, 0, 1, 3, 5, 7)));
 assertThat(cache.policy().isRecordingStats(), is(context.isRecordingStats()));
}

相关文章