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

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

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

Cache.putAll介绍

[英]Copies all of the entries from the specified map to the Cache.

The effect of this call is equivalent to that of calling #put(Object,Object) on this cache once for each mapping from key k to value v in the specified map.

The order in which the individual puts occur is undefined.

The behavior of this operation is undefined if entries in the cache corresponding to entries in the map are modified or removed while this operation is in progress. or if map is modified while the operation is in progress.

In Default Consistency mode, individual puts occur atomically but not the entire putAll. Listeners may observe individual updates.
[中]将指定映射中的所有项复制到缓存。
对于指定映射中从键k到值v的每个映射,此调用的效果相当于在该缓存上调用#put(Object,Object)一次。
单个看跌期权的发生顺序未定义。
如果在执行此操作时修改或删除了与映射中的条目对应的缓存中的条目,则此操作的行为未定义。或者,如果在操作进行过程中修改了映射。
在默认一致性模式下,单个PUT以原子方式进行,而不是整个putAll。侦听器可能会观察个别更新。

代码示例

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

@Test
public void putAll() {
 Map<Integer, String> vals = new HashMap<>();
 vals.put(1, "a");
 vals.put(2, "b");
 cache.putAll(vals);
 changesOf(0, 0, 2, 0);
 vals.put(3, "c");
 cache.putAll(vals);
 changesOf(0, 0, 3, 0);
}

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

private void innerClear() {
  cache.get(1); // one miss
  cache.getAll(asSet(1, 2, 3)); // 3 misses
  cache.put(1, "a"); // one put
  cache.put(1, "b"); // one put and update
  cache.putAll(Collections.singletonMap(2, "b")); // 1 put
  cache.get(1); // one hit
  cache.remove(1); // one remove
  cache.removeAll(); // one remove
  changesOf(1, 4, 3, 2);

  cacheStatistics.clear();
  changesOf(-1, -4, -3, -2);
 }
}

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

@Override
  void execute(Cache<Object, Object> cache, Exchange exchange) {
    cache.putAll(
      exchange.getIn().getBody(Map.class)
    );
  }
},

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

@Override
public void putAll(Map<? extends K, ? extends V> map) {
 cache.putAll(compactMap(map));
}

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

@Test
public void putAll_Null() {
 try {
  cache.putAll(null);
  fail("should have thrown an exception - null map not allowed");
 } catch (NullPointerException e) {
  //good
 }
}

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

private <K> void doIterator(Supplier<K> keySupplier,
   Cache<K, String> map1, Cache<K, String> map2) {
 K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get(),
   key4 = keySupplier.get(), key5 = keySupplier.get(), key6 = keySupplier.get();
 assertFalse(map1.iterator().hasNext());
 Map<K, String> data = new HashMap<>();
 data.put(key1, "one");
 data.put(key2, "two");
 data.put(key3, "three");
 data.put(key4, "four");
 data.put(key5, "five");
 data.put(key6, "five");
 map2.putAll(data);
 Map<K, String> res0 = new HashMap<>();
 for (Cache.Entry<K, String> e : map1)
   res0.put(e.getKey(), e.getValue());
 assertEquals(data, res0);
 map2.clear();
}

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

@Test
public void putAll_NullValue() {
 Map<Long, String> data = createLSData(3);
 // note: using LinkedHashMap, we have made an effort to ensure the null
 // be added after other "good" values.
 data.put(System.currentTimeMillis(), null);
 try {
  cache.putAll(data);
  fail("should have thrown an exception - null value not allowed");
 } catch (NullPointerException e) {
  //good
 }
}

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

@Test
public void putAll() {
 Map<Date, Date> data = createDDData(3);
 cache.putAll(data);
 for (Map.Entry<Date, Date> entry : data.entrySet()) {
  assertSame(entry.getValue(), cache.get(entry.getKey()));
 }
}

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

@Test
 public void putAll() {
  Map<Long, String> data = createLSData(3);
  cache.putAll(data);
  for (Map.Entry<Long, String> entry : data.entrySet()) {
   assertEquals(entry.getValue(), cache.get(entry.getKey()));
  }
 }
}

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

private <K> void doClear(Supplier<K> keySupplier, Cache<K, String> map1, Cache<K, String> map2) {
 K key1 = keySupplier.get(), key2 = keySupplier.get(), key3 = keySupplier.get();
 Map<K, String> data = new HashMap<>();
 data.put(key1, "one");
 data.put(key2, "two");
 data.put(key3, "two");
 map2.putAll(data);
 map2.clear();
 assertEquals(null, map1.get(key1));
 assertEquals(null, map1.get(key2));
 assertEquals(null, map1.get(key3));
}

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

@Test
public void removeAll_1arg() {
 Map<Long, String> data = createLSData(3);
 cache.putAll(data);
 Iterator<Map.Entry<Long, String>> it = data.entrySet().iterator();
 it.next();
 Map.Entry removedEntry = it.next();
 it.remove();
 cache.removeAll(data.keySet());
 for (Long key : data.keySet()) {
  assertFalse(cache.containsKey(key));
 }
 assertEquals(removedEntry.getValue(), cache.get((Long) removedEntry.getKey()));
}

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

@Test
public void putAll_NullKey() {
 Map<Long, String> data = createLSData(3);
 // note: using LinkedHashMap, we have made an effort to ensure the null
 // be added after other "good" values.
 data.put(null, "");
 try {
  cache.putAll(data);
  fail("should have thrown an exception - null key not allowed");
 } catch (NullPointerException e) {
  //expected
 }
}

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

@Test
public void iterator() {
 LinkedHashMap<Long, String> data = createLSData(3);
 cache.putAll(data);
 Iterator<Cache.Entry<Long, String>> iterator = cache.iterator();
 while (iterator.hasNext()) {
  Cache.Entry<Long, String> next = iterator.next();
  assertEquals(next.getValue(), data.get(next.getKey()));
  iterator.remove();
  data.remove(next.getKey());
 }
 assertTrue(data.isEmpty());
}

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

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

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

@Test
 public void removeAll_0arg() {
  Map<Long, String> data = createLSData(3);
  cache.putAll(data);
  cache.removeAll();
  for (Long key : data.keySet()) {
   assertFalse(cache.containsKey(key));
  }
 }
}

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

/**
 * Ensure that a {@link Cache#putAll(java.util.Map)} does not cause
 * an entry to be loaded.
 */
@Test
public void shouldNotLoadUsingPutAll() {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 HashMap<String, String> map = new HashMap<>();
 map.put("gudday", "gudday");
 map.put("hello", "hello");
 map.put("howdy", "howdy");
 map.put("bonjour", "bonjour");
 //assert that the cache doesn't contain the map entries
 for (String key : map.keySet()) {
  assertThat(cache.containsKey(key), is(false));
 }
 cache.putAll(map);
 //assert that the cache contains the map entries
 for (String key : map.keySet()) {
  assertThat(cache.containsKey(key), is(true));
  assertThat(cache.get(key), is(map.get(key)));
 }
 //assert that nothing was loaded
 assertThat(cacheLoader.getLoadCount(), is(0));
 for (String key : map.keySet()) {
  assertThat(cacheLoader.hasLoaded(key), is(false));
 }
}

代码示例来源:origin: org.apache.tomee.patch/commons-jcs-jcache-extras

@Test
  public void checkWriteAllAndDeleteAll()
  {
    assertTrue(copy.isEmpty());
    assertFalse(cache.iterator().hasNext());
    cache.put("foo", "bar");
    assertEquals(1, copy.size());
    cache.remove("foo");
    assertTrue(copy.isEmpty());

    cache.putAll(new HashMap<String, String>() {{
      put("a", "b");
      put("b", "c");
    }});
    assertEquals(2, copy.size());
    cache.removeAll(new HashSet<String>(asList("a", "b")));
    assertTrue(copy.isEmpty());
  }
}

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

@Test
public void testExpiryOnCreation() throws Exception {
  // close cache since need to configure cache with ExpireOnCreationPolicy
  CacheManager mgr = cache.getCacheManager();
  mgr.destroyCache(cache.getName());
  MutableConfiguration<Long, String> config = new MutableConfiguration<Long, String>();
  config.setStatisticsEnabled(true);
  config.setTypes(Long.class, String.class);
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(ExpireOnCreationPolicy.class));
  cache = mgr.createCache(getTestCacheName(), config);
  cache.put(1L, "hello");
  assertEventually(new AssertionRunnable() {
    @Override
    public void run() throws Exception {
     assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
    }
  }, statisticsUpdateTimeoutMillis);
  Map<Long, String> map = new HashMap<Long, String>();
  map.put(2L, "goodbye");
  map.put(3L, "world");
  cache.putAll(map);
  assertEventually(new AssertionRunnable() {
   @Override
   public void run() throws Exception {
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
   }
  }, statisticsUpdateTimeoutMillis);
}

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

@Test
public void putAllShouldCallGetExpiry() {
 CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
 expiryPolicyServer.setExpiryPolicy(expiryPolicy);
 MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
 config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
 Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
 Map<Integer, Integer> map = new HashMap<>();
 map.put(1, 1);
 map.put(2, 2);
 cache.put(1, 1);
 assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
 assertThat(expiryPolicy.getAccessCount(), is(0));
 assertThat(expiryPolicy.getUpdatedCount(), is(0));
 expiryPolicy.resetCount();
 cache.putAll(map);
 assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(1));
 assertThat(expiryPolicy.getAccessCount(), is(0));
 assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(1));
 expiryPolicy.resetCount();
}

相关文章