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

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

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

Caffeine.executor介绍

[英]Specifies the executor to use when running asynchronous tasks. The executor is delegated to when sending removal notifications, when asynchronous computations are performed by AsyncLoadingCache or LoadingCache#refresh or #refreshAfterWrite, or when performing periodic maintenance. By default, ForkJoinPool#commonPool() is used.

The primary intent of this method is to facilitate testing of caches which have been configured with #removalListener or utilize asynchronous computations. A test may instead prefer to configure the cache to execute tasks directly on the same thread.

Beware that configuring a cache with an executor that throws RejectedExecutionExceptionmay experience non-deterministic behavior.
[中]指定运行异步任务时要使用的执行器。当发送删除通知时,当异步加载缓存或加载缓存#刷新或#刷新后写入执行异步计算时,或当执行定期维护时,执行器被委派到。默认情况下,使用ForkJoinPool#commonPool()。
此方法的主要目的是方便测试已配置#removalListener或使用异步计算的缓存。测试可能更倾向于将缓存配置为直接在同一线程上执行任务。
请注意,使用抛出RejectedExecutionException的执行器配置缓存可能会遇到不确定性行为。

代码示例

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

@Test(expectedExceptions = IllegalStateException.class)
public void executor_twice() {
 Caffeine.newBuilder().executor(MoreExecutors.directExecutor())
   .executor(MoreExecutors.directExecutor());
}

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

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

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

@Test
public void executor() {
 Caffeine<?, ?> builder = Caffeine.newBuilder().executor(MoreExecutors.directExecutor());
 assertThat(builder.getExecutor(), is(MoreExecutors.directExecutor()));
 builder.build();
}

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

public void testComputeIfAbsentEviction() {
 Cache<String, String> c = CaffeinatedGuava.build(
   Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1));
 assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
 assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
 assertThat(c.asMap().computeIfAbsent("hash-1", k -> "")).isEqualTo("");
 assertThat(c.size()).isEqualTo(1);
 assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo("");
}

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

@DataProvider(name = "params")
public Object[][] providesCache() {
 ConcurrentMap<String, String> source = new ConcurrentHashMap<>();
 ConcurrentMap<String, Date> lastLoad = new ConcurrentHashMap<>();
 AsyncLoadingCache<String, String> cache = Caffeine.newBuilder()
   .expireAfterWrite(TTL, TimeUnit.MILLISECONDS)
   .executor(executor)
   .buildAsync(new Loader(source, lastLoad));
 return new Object[][] {{ cache, source, lastLoad }};
}

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

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

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

@Override
public void setUp() throws Exception {
 super.setUp();
 this.cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .expireAfterAccess(500000, TimeUnit.MILLISECONDS)
   .executor(MoreExecutors.directExecutor())
   .maximumSize(count));
}

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

public void testComputeEviction() {
 Cache<String, String> c = CaffeinatedGuava.build(
   Caffeine.newBuilder().executor(MoreExecutors.directExecutor()).maximumSize(1));
 assertThat(c.asMap().compute("hash-1", (k, v) -> "a")).isEqualTo("a");
 assertThat(c.asMap().compute("hash-1", (k, v) -> "b")).isEqualTo("b");
 assertThat(c.asMap().compute("hash-1", (k, v) -> "c")).isEqualTo("c");
 assertThat(c.size()).isEqualTo(1);
 assertThat(c.asMap().computeIfAbsent("hash-2", k -> "")).isEqualTo("");
}

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

Builder(String cacheName, CaffeineConfiguration<K, V> config) {
 this.config = config;
 this.cacheName = cacheName;
 this.caffeine = Caffeine.newBuilder();
 this.statistics = new JCacheStatisticsMXBean();
 this.ticker = config.getTickerFactory().create();
 this.executor = config.getExecutorFactory().create();
 this.expiryPolicy = config.getExpiryPolicyFactory().create();
 this.dispatcher = new EventDispatcher<>(executor);
 caffeine.executor(executor);
 config.getCacheEntryListenerConfigurations().forEach(dispatcher::register);
}

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

public CaffeinePolicy(Config config) {
 policyStats = new PolicyStats("product.Caffeine");
 BasicSettings settings = new BasicSettings(config);
 maximumSize = settings.maximumSize();
 cache = Caffeine.newBuilder()
   .initialCapacity(maximumSize)
   .maximumSize(maximumSize)
   .executor(Runnable::run)
   .build();
}

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

public void testEviction_maxSizeOneSegment() {
 IdentityLoader<Integer> loader = identityLoader();
 LoadingCache<Integer, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .executor(MoreExecutors.directExecutor())
   .maximumSize(MAX_SIZE), 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

@GwtIncompatible("maximumWeight")
public void testMaximumWeight_withoutWeigher() {
 Caffeine<Object, Object> builder = Caffeine.newBuilder()
   .executor(MoreExecutors.directExecutor())
   .maximumWeight(1);
 try {
  CaffeinatedGuava.build(builder, identityLoader());
  fail();
 } catch (IllegalStateException expected) {}
}

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

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

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

public void testExpiration_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);
 checkExpiration(cache, loader, ticker, removalListener);
}

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

public void testExpiringGet_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);
 runExpirationTest(cache, loader, ticker, removalListener);
}

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

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

代码示例来源: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 testRemovalScheduler_expireAfterAccess() {
 FakeTicker ticker = new FakeTicker();
 CountingRemovalListener<String, Integer> removalListener = countingRemovalListener();
 WatchedCreatorLoader loader = new WatchedCreatorLoader();
 LoadingCache<String, Integer> cache = CaffeinatedGuava.build(Caffeine.newBuilder()
   .expireAfterAccess(EXPIRING_TIME, MILLISECONDS)
   .executor(MoreExecutors.directExecutor())
   .removalListener(removalListener)
   .ticker(ticker::read),
   loader);
 runRemovalScheduler(cache, removalListener, loader, ticker, KEY_PREFIX, EXPIRING_TIME);
}

相关文章