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

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

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

Caffeine.expireAfterWrite介绍

[英]Specifies that each entry should be automatically removed from the cache once a fixed duration has elapsed after the entry's creation, or the most recent replacement of its value.

Expired entries may be counted in Cache#estimatedSize(), but will never be visible to read or write operations. Expired entries are cleaned up as part of the routine maintenance described in the class javadoc.

If you can represent the duration as a java.time.Duration (which should be preferred when feasible), use #expireAfterWrite(Duration) instead.
[中]指定在创建条目或最近替换其值后经过固定的持续时间后,应自动从缓存中删除每个条目。
过期的条目可能会在缓存#estimatedSize()中计数,但对读写操作而言永远不会可见。过期条目将作为javadoc类中描述的日常维护的一部分进行清理。
如果可以将持续时间表示为java。时间持续时间(可行时应优先选择),改为使用#expireAfterWrite(持续时间)。

代码示例

代码示例来源:origin: dropwizard/dropwizard

RegexStringMatchingStrategy() {
  patternCache = Caffeine.newBuilder()
    .expireAfterWrite(1, TimeUnit.HOURS)
    .build(Pattern::compile);
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void expireAfterWrite_twice() {
 Caffeine.newBuilder().expireAfterWrite(1, TimeUnit.MILLISECONDS)
   .expireAfterWrite(1, TimeUnit.MILLISECONDS);
}

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

@Test(expectedExceptions = IllegalStateException.class)
public void expireAfterWrite_duration_twice() {
 Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(1))
   .expireAfterWrite(Duration.ofMillis(1));
}

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

public void testTimeToLive_setTwice() {
 Caffeine<Object, Object> builder =
   Caffeine.newBuilder().expireAfterWrite(3600, SECONDS);
 try {
  // even to the same value is not allowed
  builder.expireAfterWrite(3600, SECONDS);
  fail();
 } catch (IllegalStateException expected) {}
}

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

/** Configures the write expiration and returns if set. */
private boolean configureExpireAfterWrite() {
 if (config.getExpireAfterWrite().isPresent()) {
  caffeine.expireAfterWrite(config.getExpireAfterWrite().getAsLong(), TimeUnit.NANOSECONDS);
 }
 return config.getExpireAfterWrite().isPresent();
}

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

@GwtIncompatible // java.time.Duration
public void testTimeToLive_setTwice_duration() {
 Caffeine<Object, Object> builder =
   Caffeine.newBuilder().expireAfterWrite(java.time.Duration.ofSeconds(3600));
 try {
  // even to the same value is not allowed
  builder.expireAfterWrite(java.time.Duration.ofSeconds(3600));
  fail();
 } catch (IllegalStateException expected) {
 }
}

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

public void testTimeToLive_negative() {
 Caffeine<Object, Object> builder = Caffeine.newBuilder();
 try {
  builder.expireAfterWrite(-1, SECONDS);
  fail();
 } catch (IllegalArgumentException expected) {}
}

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

public void testAsMapValues_iteratorRemove() {
  Cache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
    .expireAfterWrite(1000, TimeUnit.MILLISECONDS)
    .executor(MoreExecutors.directExecutor())
    .ticker(fakeTicker::read));

  cache.put(10, 20);
  Iterator<Integer> iterator = cache.asMap().values().iterator();
  iterator.next();
  iterator.remove();

  assertEquals(0, cache.size());
 }
}

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

public void testConcurrentExpirationLoading() throws InterruptedException {
 testConcurrentLoading(Caffeine.newBuilder()
   .expireAfterWrite(10, TimeUnit.SECONDS)
   .executor(MoreExecutors.directExecutor()));
}

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

@GwtIncompatible // java.time.Duration
public void testTimeToLive_negative_duration() {
 Caffeine<Object, Object> builder = Caffeine.newBuilder();
 try {
  builder.expireAfterWrite(java.time.Duration.ofSeconds(-1));
  fail();
 } catch (IllegalArgumentException expected) {
 }
}

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

public void testTimeToLive_small() {
 CaffeinatedGuava.build(Caffeine.newBuilder()
   .expireAfterWrite(1, NANOSECONDS), identityLoader());
 // well, it didn't blow up.
}

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

public void testTimeToIdleAndToLive() {
 CaffeinatedGuava.build(Caffeine.newBuilder()
   .expireAfterWrite(1, NANOSECONDS)
   .expireAfterAccess(1, NANOSECONDS),
   identityLoader());
 // well, it didn't blow up.
}

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

public void testParse_writeExpirationMinutes() {
 CaffeineSpec spec = parse("expireAfterWrite=10m");
 assertEquals(TimeUnit.MINUTES, spec.expireAfterWriteTimeUnit);
 assertEquals(10L, spec.expireAfterWriteDuration);
 assertCaffeineEquivalence(
   Caffeine.newBuilder().expireAfterWrite(10L, TimeUnit.MINUTES), Caffeine.from(spec));
}

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

public void testParse_writeExpirationSeconds() {
 CaffeineSpec spec = parse("expireAfterWrite=10s");
 assertEquals(TimeUnit.SECONDS, spec.expireAfterWriteTimeUnit);
 assertEquals(10L, spec.expireAfterWriteDuration);
 assertCaffeineEquivalence(
   Caffeine.newBuilder().expireAfterWrite(10L, TimeUnit.SECONDS), Caffeine.from(spec));
}

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

public void testParse_writeExpirationHours() {
 CaffeineSpec spec = parse("expireAfterWrite=150h");
 assertEquals(TimeUnit.HOURS, spec.expireAfterWriteTimeUnit);
 assertEquals(150L, spec.expireAfterWriteDuration);
 assertCaffeineEquivalence(
   Caffeine.newBuilder().expireAfterWrite(150L, TimeUnit.HOURS), Caffeine.from(spec));
}

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

public void testRemovalScheduler_expireAfterWrite() {
 FakeTicker ticker = new FakeTicker();
 CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
 WatchedCreatorLoader loader = new WatchedCreatorLoader();
 LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .expireAfterWrite(EXPIRING_TIME, MILLISECONDS)
   .executor(MoreExecutors.directExecutor())
   .removalListener(removalListener)
   .ticker(ticker::read),
   loader);
 runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}

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

public void testParse_accessExpirationAndWriteExpiration() {
 CaffeineSpec spec = parse("expireAfterAccess=10s,expireAfterWrite=9m");
 assertEquals(TimeUnit.MINUTES, spec.expireAfterWriteTimeUnit);
 assertEquals(9L, spec.expireAfterWriteDuration);
 assertEquals(TimeUnit.SECONDS, spec.expireAfterAccessTimeUnit);
 assertEquals(10L, spec.expireAfterAccessDuration);
 assertCaffeineEquivalence(
   Caffeine.newBuilder()
    .expireAfterAccess(10L, TimeUnit.SECONDS)
    .expireAfterWrite(9L, TimeUnit.MINUTES),
   Caffeine.from(spec));
}

相关文章