javax.cache.Cache.loadAll()方法的使用及代码示例

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

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

Cache.loadAll介绍

[英]Asynchronously loads the specified entries into the cache using the configured CacheLoader for the given keys.

If an entry for a key already exists in the Cache, a value will be loaded if and only if replaceExistingValues is true. If no loader is configured for the cache, no objects will be loaded. If a problem is encountered during the retrieving or loading of the objects, an exception is provided to the CompletionListener. Once the operation has completed, the specified CompletionListener is notified.

Implementations may choose to load multiple keys from the provided Set in parallel. Iteration however must not occur in parallel, thus allow for non-thread-safe Sets to be used.

The thread on which the completion listener is called is implementation dependent. An implementation may also choose to serialize calls to different CompletionListeners rather than use a thread per CompletionListener.
[中]使用为给定密钥配置的CacheLoader将指定项异步加载到缓存中。
如果缓存中已存在密钥项,则当且仅当replaceExistingValues为true时,才会加载值。如果没有为缓存配置加载程序,则不会加载对象。如果在检索或加载对象期间遇到问题,则会向CompletionListener提供异常。操作完成后,将通知指定的CompletionListener。
实现可以选择并行地从提供的集合加载多个密钥。然而,迭代不能并行进行,因此允许使用非线程安全集。
调用完成侦听器的线程依赖于实现。实现还可以选择序列化对不同CompletionListener的调用,而不是对每个CompletionListener使用线程。

代码示例

代码示例来源:origin: ehcache/ehcache3

@Test
public void test107LoaderOverriddenByEhcacheTemplateLoaderWriter() throws Exception {
 final AtomicBoolean loaderFactoryInvoked = new AtomicBoolean(false);
 final DumbCacheLoader product2CacheLoader = new DumbCacheLoader();
 MutableConfiguration<Long, Product> product2Configuration = new MutableConfiguration<>();
 product2Configuration.setTypes(Long.class, Product.class).setReadThrough(true);
 product2Configuration.setCacheLoaderFactory(() -> {
  loaderFactoryInvoked.set(true);
  return product2CacheLoader;
 });
 Cache<Long, Product> productCache2 = cacheManager.createCache("productCache2", product2Configuration);
 assertThat(loaderFactoryInvoked.get(), is(false));
 Product product = productCache2.get(124L);
 assertThat(product.getId(), is(124L));
 assertThat(ProductCacheLoaderWriter.seen, hasItem(124L));
 assertThat(product2CacheLoader.seen, is(empty()));
 CompletionListenerFuture future = new CompletionListenerFuture();
 productCache2.loadAll(Collections.singleton(42L), false, future);
 future.get();
 assertThat(ProductCacheLoaderWriter.seen, hasItem(42L));
 assertThat(product2CacheLoader.seen, is(empty()));
}

代码示例来源:origin: ehcache/ehcache3

@Test
@SuppressWarnings("unchecked")
public void enablingWriteThroughDoesNotForceReadThrough() throws Exception {
 MutableConfiguration<Long, String> config = getConfiguration(false, cacheLoader, true, cacheWriter);
 Cache<Long, String> cache = cachingProvider.getCacheManager().createCache("writingCache", config);
 cache.put(42L, "Tadam!!!");
 Set<Long> keys = singleton(25L);
 cache.loadAll(keys, false, null);
 cache.get(100L);
 verify(cacheLoader).loadAll(keys);
 verifyNoMoreInteractions(cacheLoader);
 verify(cacheWriter).write(any(Cache.Entry.class));
}

代码示例来源:origin: ehcache/ehcache3

@Test
public void testLoaderConfiguration() throws Exception {
 final AtomicBoolean loaderCreated = new AtomicBoolean(false);
 MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
 configuration.setTypes(String.class, String.class).setReadThrough(true);
 configuration.setCacheLoaderFactory(() -> {
  loaderCreated.set(true);
  return new TestCacheLoader();
 });
 CachingProvider provider = Caching.getCachingProvider();
 CacheManager cacheManager = provider.getCacheManager();
 Cache<String, String> cache = cacheManager.createCache("cache", configuration);
 assertThat(loaderCreated.get(), is(true));
 cache.putIfAbsent("42", "The Answer");
 TestCacheLoader.seen.clear();
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(Collections.singleton("42"), true, future);
 future.get();
 assertThat(TestCacheLoader.seen, contains("42"));
}

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

@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
 cache.loadAll(compactBoundedKeys(keys), replaceExistingValues, completionListener);
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void load_noLoaderNoCompletionListener() {
 // Added for code coverage.
 Set<Long> keys = new HashSet<Long>();
 keys.add(1L);
 CompletionListener NULL_COMPLETION_LISTENER = null;
 cache.loadAll(keys, true, NULL_COMPLETION_LISTENER);
}

代码示例来源:origin: com.hazelcast.simulator/tests-common

@TimeStep
public void timeStep() throws ExecutionException, InterruptedException {
  CompletionListenerFuture loaded = new CompletionListenerFuture();
  cache.loadAll(keySet, true, loaded);
  if (waitForLoadAllFutureCompletion) {
    loaded.get();
  }
}

代码示例来源:origin: javax.cache/cache-tests

/**
  * Added for code coverage.
  */
 @Test
 public void testLoadAllWithExecptionAndNoCompletionListener() throws Exception {
  FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>();
  cacheLoaderServer.setCacheLoader(cacheLoader);

  HashSet<String> keys = new HashSet<>();
  keys.add("gudday");
  keys.add("hello");
  keys.add("howdy");
  keys.add("bonjour");

  CompletionListener NULL_COMPLETION_LISTENER = null;
  cache.loadAll(keys, false, NULL_COMPLETION_LISTENER);
 }
}

代码示例来源:origin: javax.cache/cache-tests

@Test
public void load_Closed() {
 cache.close();
 try {
  cache.loadAll(null, true, null);
  fail("should have thrown an exception - cache closed");
 } catch (IllegalStateException e) {
  //good
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}  )}
 * will propagate an exception from a {@link CacheLoader}.
 */
@Test
public void shouldPropagateExceptionUsingLoadAll() throws Exception {
 FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 try{
  future.get();
  fail();
 } catch (ExecutionException e) {
  assertThat(e.getCause(), instanceOf(CacheLoaderException.class));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * won't load <code>null</code> entries.
 */
@Test
public void shouldNotLoadMultipleNullEntriesUsingLoadAll() throws Exception {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 for (String key : keys) {
  assertThat(cache.containsKey(key), is(false));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * won't load <code>null</code> entries.
 */
@Test
public void shouldNotLoadMultipleNullEntriesUsingLoadAll() throws Exception {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 for (String key : keys) {
  assertThat(cache.containsKey(key), is(false));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * won't load <code>null</code> values.
 */
@Test
public void shouldNotLoadMultipleNullValuesUsingLoadAll() throws Exception {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 for (String key : keys) {
  assertThat(cache.containsKey(key), is(false));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )}
 * using a <code>null</code> key will raise an exception
 */
@Test
public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add(null);
 try {
  CompletionListenerFuture future = new CompletionListenerFuture();
  cache.loadAll(keys, false, future);
  fail("Expected a NullPointerException");
 } catch (NullPointerException e) {
  //SKIP: expected
 } finally {
  assertThat(cacheLoader.getLoadCount(), is(0));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)} )}
 * using a <code>null</code> key will raise an exception
 */
@Test
public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashSet<String> keys = new HashSet<>();
 keys.add(null);
 try {
  CompletionListenerFuture future = new CompletionListenerFuture();
  cache.loadAll(keys, false, future);
  fail("Expected a NullPointerException");
 } catch (NullPointerException e) {
  //SKIP: expected
 } finally {
  assertThat(cacheLoader.getLoadCount(), is(0));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * for a non-existent single value will cause it to be loaded and not written.
 */
@Test
public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception {
 String key = "message";
 HashSet<String> keys = new HashSet<>();
 keys.add(key);
 assertThat(cache.containsKey(key), is(false));
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 assertThat(cache.get(key), is(equalTo(key)));
 assertThat(recordingCacheLoader.getLoadCount(), is(1));
 assertThat(recordingCacheLoader.hasLoaded(key), is(true));
 //ensure nothing has been written
 assertThat(recordingCacheWriter.getWriteCount(), is(0L));
 assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * for a non-existent single value will cause it to be loaded.
 */
@Test
public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 String key = "message";
 HashSet<String> keys = new HashSet<>();
 keys.add(key);
 assertThat(cache.containsKey(key), is(false));
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 assertThat(cache.get(key), is(equalTo(key)));
 assertThat(cacheLoader.getLoadCount(), is(1));
 assertThat(cacheLoader.hasLoaded(key), is(true));
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(Set, boolean, javax.cache.integration.CompletionListener)}
 * for a non-existent single value will cause it to be loaded.
 */
@Test
public void shouldLoadSingleMissingEntryUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 String key = "message";
 HashSet<String> keys = new HashSet<>();
 keys.add(key);
 assertThat(cache.containsKey(key), is(false));
 CompletionListenerFuture future = new CompletionListenerFuture();
 cache.loadAll(keys, false, future);
 //wait for the load to complete
 future.get();
 assertThat(future.isDone(), is(true));
 assertThat(cache.get(key), is(equalTo(key)));
 assertThat(cacheLoader.getLoadCount(), is(1));
 assertThat(cacheLoader.hasLoaded(key), is(true));
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * using a <code>null</code> key will raise an exception
 */
@Test
public void shouldNotLoadWithNullKeysUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 try {
  CompletionListenerFuture future = new CompletionListenerFuture();
  cache.loadAll(null, false, future);
  fail("Expected a NullPointerException");
 } catch (NullPointerException e) {
  //SKIP: expected
 } finally {
  assertThat(cacheLoader.getLoadCount(), is(0));
 }
}

代码示例来源:origin: javax.cache/cache-tests

/**
 * Ensure that {@link Cache#loadAll(java.util.Set, boolean, javax.cache.integration.CompletionListener)}
 * using a <code>null</code> key will raise an exception
 */
@Test
public void shouldNotLoadWithNullKeysUsingLoadAll() throws Exception {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 try {
  CompletionListenerFuture future = new CompletionListenerFuture();
  cache.loadAll(null, false, future);
  fail("Expected a NullPointerException");
 } catch (NullPointerException e) {
  //SKIP: expected
 } finally {
  assertThat(cacheLoader.getLoadCount(), is(0));
 }
}

代码示例来源:origin: org.infinispan/infinispan-jcache

@Override
  public void call() {
   JCacheManager jCacheManager = createJCacheManager(cm, this);
   InMemoryJCacheLoader<Integer, String> cacheLoader = new InMemoryJCacheLoader<Integer, String>();
   cacheLoader.store(1, "v1").store(2, "v2");
   MutableConfiguration<Integer, String> cfg = new MutableConfiguration<Integer, String>();
   // JDK6 fails to compile when calling FactoryBuilder.factoryOf() :(
   cfg.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory(cacheLoader));
   Cache<Integer, String> cache = jCacheManager.createCache(cacheName, cfg);
   assertEquals(0, cacheLoader.getLoadCount());
   CompletionListenerFuture future = new CompletionListenerFuture();
   cache.loadAll(CollectionFactory.makeSet(1, 2), true, future);
   futureGet(future);
   assertEquals(2, cacheLoader.getLoadCount());
  }
});

相关文章