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

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

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

Cache.invokeAll介绍

[英]Invokes an EntryProcessor against the set of Entrys specified by the set of keys.

If an Entry does not exist for the specified key, an attempt is made to load it (if a loader is configured) or a surrogate Entry, consisting of the key and a value of null is provided.

The order that the entries for the keys are processed is undefined. Implementations may choose to process the entries in any order, including concurrently. Furthermore there is no guarantee implementations will use the same EntryProcessor instance to process each entry, as the case may be in a non-local cache topology.

The result of executing the EntryProcessor is returned as a Map of EntryProcessorResults, one result per key. Should the EntryProcessor or Caching implementation throw an exception, the exception is wrapped and re-thrown when a call to javax.cache.processor.EntryProcessorResult#get() is made.
[中]针对键集指定的入口集调用入口处理器。
如果指定密钥的条目不存在,则尝试加载该条目(如果配置了加载程序),或者提供由该密钥和null值组成的代理条目。
键项的处理顺序未定义。实现可以选择以任何顺序处理条目,包括并发处理。此外,无法保证实现将使用相同的EntryProcessor实例来处理每个条目,因为在非本地缓存拓扑中可能存在这种情况。
执行EntryProcessor的结果作为EntryProcessorResults的映射返回,每个键一个结果。如果EntryProcessor或缓存实现抛出异常,则在调用javax时会包装并重新抛出异常。隐藏物加工机EntryProcessorResult#get()已生成。

代码示例

代码示例来源:origin: apache/ignite

/**
 * Update the cache using either invokeAll() or putAll().
 *
 * @param cache the cache
 * @param newVal the new value to put to the entries
 * @param invoke whether to use invokeAll() or putAll()
 * @param keys Keys to update.
 */
private void updateEntries(
  Cache<Integer, Integer> cache,
  int newVal,
  boolean invoke,
  Set<Integer> keys
) {
  if (invoke)
    cache.invokeAll(keys, new IntegerSetValue(newVal));
  else {
    final Map<Integer, Integer> entries = new HashMap<>(ENTRY_COUNT);
    for (final Integer key : keys)
      entries.put(key, newVal);
    cache.putAll(entries);
  }
}

代码示例来源:origin: apache/ignite

/**
 * @throws Exception If failed.
 */
@Test
public void testInvokeAll() throws Exception {
  Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);
  Set<Integer> keys = new HashSet<>();
  for (int i = 0; i < ENTRIES_NUM; i++)
    keys.add(i);
  Map<Integer, EntryProcessorResult<Object>> res = cache.invokeAll(keys, new EntryProcessor<Integer, String, Object>() {
    @Override public Object process(MutableEntry<Integer, String> entry, Object... args) {
      CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class);
      checkVersionedEntry(verEntry);
      return verEntry.version();
    }
  });
  assertEquals(ENTRIES_NUM, res.size());
}

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

@Test
public void invokeAll() {
 Set<Integer> keys = asSet(1, 2, 3);
 cache.invokeAll(keys, new GetEntryProcessor()); // miss
 changesOf(0, 3, 0, 0);
 cache.invokeAll(keys, new GetKeyEntryProcessor()); // miss
 changesOf(0, 3, 0, 0);
 cache.invokeAll(keys, new ExistEntryProcessor()); // miss
 changesOf(0, 3, 0, 0);
 cache.invokeAll(keys, new SetEntryProcessor("a")); // put
 changesOf(0, 3, 3, 0); // FIXME Why is there misses?
 cache.invokeAll(keys, new SetEntryProcessor("b")); // update
 changesOf(3, 0, 3, 0); // FIXME Why is there hits?
 cache.invokeAll(keys, new GetEntryProcessor()); // hit
 changesOf(3, 0, 0, 0);
 cache.invokeAll(keys, new GetKeyEntryProcessor()); // hit
 changesOf(3, 0, 0, 0);
 cache.invokeAll(keys, new ExistEntryProcessor()); // hit
 changesOf(3, 0, 0, 0);
 cache.invokeAll(asSet(2, 3, 4), new GetEntryProcessor()); // asymetric get
 changesOf(2, 1, 0, 0);
 cache.invokeAll(keys, new RemoveEntryProcessor()); // hit
 changesOf(3, 0, 0, 3); // FIXME Why is there a hit?
 cache.invokeAll(keys, new RemoveEntryProcessor()); // miss
 changesOf(0, 3, 0, 3); // FIXME Why is there a remove?
}

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

@Override
public <T> Map<K, EntryProcessorResult<T>> invokeAll(
  Set<? extends K> keys, EntryProcessor<K, V, T> entryProcessor, Object... arguments) {
 EntryProcessor<K0, V0, T> processor = wrapEntryProcessor(entryProcessor);
 Map<K0, EntryProcessorResult<T>> map = cache.invokeAll(compactBoundedKeys(keys), processor, arguments);
 Map<K, EntryProcessorResult<T>> m2 = new HashMap<K, EntryProcessorResult<T>>();
 for (Map.Entry<K0, EntryProcessorResult<T>> e : map.entrySet()) {
  m2.put(keyTransformer.expand(e.getKey()), e.getValue());
 }
 return m2;
}

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

@Override
  void execute(Cache<Object, Object> cache, Exchange exchange) {
    Message message = exchange.getIn();
    Set<Object> keys = message.getHeader(JCacheConstants.KEYS, Set.class);
    EntryProcessor<Object, Object, Object> entryProcessor = message.getHeader(JCacheConstants.ENTRY_PROCESSOR, EntryProcessor.class);
    Collection<Object> arguments = message.getHeader(JCacheConstants.ARGUMENTS, Collection.class);
    if (arguments == null) {
      arguments = Collections.emptyList();
    }
    message.setBody(
      keys != null
        ? cache.invokeAll(
          keys,
          entryProcessor,
          arguments)
        : cache.invoke(
          exchange.getIn().getHeader(JCacheConstants.KEY),
          entryProcessor,
          arguments)
    );
  }
},

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

@Test(expected = NullPointerException.class)
public void invokeAll_nullProcessor() {
 Set<Integer> keys = new HashSet<Integer>();
 keys.add(123);
 cache.invokeAll(keys, null);
}

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

@Test(expected = EntryProcessorException.class)
public void invokeAllEntryProcessorException() {
 Set<Integer> keys = new HashSet<Integer>();
 keys.add(123);
 Map<Integer, EntryProcessorResult<Object>> resultMap =
  cache.invokeAll(keys, new ThrowExceptionEntryProcessor<Integer, String, Object>(IllegalStateException.class));
 resultMap.get(123).get();
}

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

/**
 * Added for RI code coverage.
 */
@Test
public void invokeAllEntryProcessorReturnsNullResult() {
 Set<Integer> keys = new HashSet<Integer>();
 keys.add(123);
 Map<Integer, EntryProcessorResult<Object>> resultMap =
  cache.invokeAll(keys,
   new SetValueCreateEntryReturnDifferentTypeEntryProcessor<Integer, String, Object>(null, "newValue"));
 assertTrue(resultMap != null && resultMap.size() == 0);
}

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

@Test(expected = NullPointerException.class)
public void invokeAllNullKeys() {
 cache.invokeAll(null, new NoOpEntryProcessor<Integer, String>());
}

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

/**
 * Added for RI code coverage.
 */
@Test
public void invokeAllgetResultFromMap() {
 Set<Integer> keys = new HashSet<Integer>();
 keys.add(123);
 Map<Integer, EntryProcessorResult<String>> resultMap =
  cache.invokeAll(keys,
   new SetEntryProcessor<Integer, String>("aValue"));
 assertTrue(resultMap != null && resultMap.size() == 1);
 assertEquals("aValue", resultMap.get(123).get());
}

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

private <K> void doInvokeAll(Supplier<K> keySupplier,
   Cache<K, String> map1, Cache<K, String> map2) {
 K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get();
 HashSet<K> keys = new HashSet<>(Arrays.asList(key1, key2, key3));
 // Get multi via invokeAll
 Map<K, EntryProcessorResult<String>> res0 = map1.invokeAll(keys, GetValueProcessor.getInstance());
 assertEquals(null, res0.get(key1).get());
 assertEquals(null, res0.get(key2).get());
 assertEquals(null, res0.get(key3).get());
 // Put multi via invokeAll
 Map<K, String> data = new HashMap<>();
 data.put(key1, "one");
 data.put(key2, "two");
 data.put(key3, "three");
 map2.invokeAll(keys, SetArgsValuesProcessor.getInstance(), data);
 // Get multi via invokeAll
 Map<K, EntryProcessorResult<String>> res1 = map1.invokeAll(keys, GetValueProcessor.getInstance());
 assertEquals("one", res1.get(key1).get());
 assertEquals("two", res1.get(key2).get());
 assertEquals("three", res1.get(key3).get());
 // Remove multi via invokeAll
 map2.invokeAll(keys, RemoveProcessor.getInstance());
 // Get multi via invokeAll
 Map<K, EntryProcessorResult<String>> res2 = map1.invokeAll(keys, GetValueProcessor.getInstance());
 assertEquals(null, res2.get(key1).get());
 assertEquals(null, res2.get(key2).get());
 assertEquals(null, res2.get(key3).get());
}

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

cache.invokeAll(keys, new SetEntryProcessor<Integer, Integer>(setValue));
cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

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

@Test
public void shouldWriteThroughUsingInvokeAll_setValue_CreateEntry() {
 final String VALUE_PREFIX = "value_";
 final int NUM_KEYS = 10;
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 Set<Integer> keys = new HashSet<>();
 for (int key = 1; key <= NUM_KEYS; key++) {
  keys.add(key);
 }
 cache.invokeAll(keys, new SetEntryWithComputedValueProcessor<Integer>(VALUE_PREFIX, ""));
 assertEquals(NUM_KEYS, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 for (Integer key : keys) {
  String computedValue = VALUE_PREFIX + key;
  assertTrue(cacheWriter.hasWritten(key));
  assertEquals(computedValue, cacheWriter.get(key));
  assertEquals(computedValue, cache.get(key));
 }
}

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

cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

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

@Test
public void shouldWriteThroughUsingInvokeAll_setValue_RemoveEntry() {
 final String VALUE_PREFIX = "value_";
 final int NUM_KEYS = 10;
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 Set<Integer> keys = new HashSet<>();
 for (int key = 1; key <= NUM_KEYS; key++) {
  keys.add(key);
  cache.put(key, VALUE_PREFIX + key);
 }
 assertEquals(NUM_KEYS, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 cache.invokeAll(keys, new RemoveEntryProcessor<Integer, String, Object>(true));
 assertEquals(NUM_KEYS, cacheWriter.getWriteCount());
 assertEquals(NUM_KEYS, cacheWriter.getDeleteCount());
 for (Integer key : keys) {
  assertFalse(cacheWriter.hasWritten(key));
  assertEquals(null, cacheWriter.get(key));
  assertEquals(null, cache.get(key));
 }
}

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

@Test
public void shouldWriteThroughUsingInvokeAll_setValue_UpdateEntry() {
 final String VALUE_PREFIX_ORIGINAL = "value_";
 final String VALUE_PREFIX_UPDATED = "updateValue_";
 final int NUMBER_OF_KEYS = 10;
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 Set<Integer> keys = new HashSet<>();
 for (int key = 1; key <= NUMBER_OF_KEYS; key++) {
  keys.add(key);
  cache.put(key, VALUE_PREFIX_ORIGINAL + key);
 }
 assertEquals(NUMBER_OF_KEYS, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
   cache.invokeAll(keys, new SetEntryWithComputedValueProcessor<Integer>(VALUE_PREFIX_UPDATED, ""));
 assertEquals(NUMBER_OF_KEYS * 2, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 for (Integer key : keys) {
  String computedValue = VALUE_PREFIX_UPDATED + key;
  assertTrue(cacheWriter.hasWritten(key));
  assertEquals(computedValue, cacheWriter.get(key));
  assertEquals(computedValue, cache.get(key));
 }
}

相关文章