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

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

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

Cache.getAll介绍

[英]Gets a collection of entries from the Cache, returning them as Map of the values associated with the set of keys requested.

If the cache is configured read-through, and a get for a key would return null because an entry is missing from the cache, the Cache's CacheLoader is called in an attempt to load the entry. If an entry cannot be loaded for a given key, the key will not be present in the returned Map.
[中]

代码示例

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

@Test
public void factory() {
 Cache<Integer, Integer> cache = cacheManager.getCache("guice");
 Map<Integer, Integer> result = cache.getAll(ImmutableSet.of(1, 2, 3));
 assertThat(result, is(ImmutableMap.of(1, 1, 2, 2, 3, 3)));
}

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

/**
 * @param threads Threads count.
 * @param recsPerThread initial records per thread.
 * @param restartedCache cache to obtain data from.
 */
private void verifyByChunk(int threads, int recsPerThread, Cache<Integer, HugeIndexedObject> restartedCache) {
  int verifyChunk = 100;
  int totalRecsToVerify = recsPerThread * threads;
  int chunks = totalRecsToVerify / verifyChunk;
  for (int c = 0; c < chunks; c++) {
    Set<Integer> keys = new TreeSet<>();
    for (int i = 0; i < verifyChunk; i++)
      keys.add(i + c * verifyChunk);
    Map<Integer, HugeIndexedObject> values = restartedCache.getAll(keys);
    for (Map.Entry<Integer, HugeIndexedObject> next : values.entrySet()) {
      Integer key = next.getKey();
      int actVal = values.get(next.getKey()).iVal;
      int i = key;
      Assert.assertEquals(i, actVal);
      if (i % 1000 == 0)
        X.println(" >> Verified: " + i);
    }
  }
}

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

@Test
public void test_getCacheHitsAndMisses() {
 heapCache.put("key1", "value1");
 heapCache.put("key3", "value3");
 heapCache.put("key5", "value5");
 HashSet<String> keys = new HashSet<>(5);
 for (int i = 1; i <= 5; i++) {
  keys.add("key" + i);
 }
 heapCache.getAll(keys);
 assertThat(heapStatistics.getCacheHits(), is(3L));
 assertThat(heapStatistics.getCacheMisses(), is(2L));
}

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

@Test
public void getAll() {
 expect(cache.getAll(asSet(1))).isEmpty();
 changesOf(0, 1, 0, 0);
 cache.put(1, "a");
 cache.put(2, "b");
 changesOf(0, 0, 2, 0);
 expect(cache.getAll(asSet(1, 2, 3))).containsKeys(1, 2);
 changesOf(2, 1, 0, 0);
}

代码示例来源: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) {
    exchange.getIn().setBody(
      cache.getAll(
        exchange.getIn().getHeader(JCacheConstants.KEYS, Set.class))
    );
  }
},

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

@Override
public Map<K, V> getAll(Set<? extends K> keys) {
 Map<K0, V0> m = cache.getAll(compactBoundedKeys(keys));
 return expandMap(m);
}

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

@Test
public void getAll_NullKey() {
 HashSet<Long> keys = new HashSet<Long>();
 keys.add(1L);
 keys.add(null);
 keys.add(2L);
 try {
  cache.getAll(keys);
  fail("should have thrown an exception - null key in keys not allowed");
 } catch (NullPointerException e) {
  //expected
 }
}

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

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

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

/**
 * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> values
 * aren't placed in the cache.
 */
@Test
public void shouldNotLoadNullValues() {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 //attempt to get the keys
 Map<String, String> map = cache.getAll(keys);
 //nothing should have been loaded
 assertThat(map.size(), is(0));
}

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

/**
 * Ensure that a {@link CacheLoader} that returns <code>null</code> values
 * aren't placed in the cache.
 */
@Test
public void shouldNotLoadNullValues() {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 //attempt to get the keys
 Map<String, String> map = cache.getAll(keys);
 //nothing should have been loaded
 assertThat(map.size(), is(0));
}

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

/**
 * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> entries
 * aren't placed in the cache.
 */
@Test
public void shouldNotLoadNullEntries() {
 NullValueCacheLoader<String, String> nullCacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(nullCacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 //attempt to get the keys
 Map<String, String> map = cache.getAll(keys);
 //nothing should have been loaded
 assertThat(map.size(), is(0));
}

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

@Test
public void getAll() {
 ArrayList<Long> keysInCache = new ArrayList<Long>();
 keysInCache.add(1L);
 keysInCache.add(2L);
 for (Long k : keysInCache) {
  cache.put(k, "value" + k);
 }
 HashSet<Long> keysToGet = new HashSet<Long>();
 keysToGet.add(2L);
 keysToGet.add(3L);
 HashSet<Long> keysExpected = new HashSet<Long>();
 keysExpected.add(2L);
 Map<Long, String> map = cache.getAll(keysToGet);
 assertEquals("size", keysExpected.size(), map.size());
 for (Long key : keysExpected) {
  assertTrue(map.containsKey(key));
  assertEquals("key  : key=" + key, cache.get(key), map.get(key));
  assertEquals("value: key=" + key, "value" + key, map.get(key));
 }
}

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

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

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

/**
 * Ensure that a {@link javax.cache.integration.CacheLoader} that returns <code>null</code> values
 * aren't placed in the cache or written
 */
@Test
public void shouldNotLoadNullValues() {
 NullValueCacheLoader<String, String> cacheLoader = new NullValueCacheLoader<>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 //attempt to get the keys
 Map<String, String> map = cache.getAll(keys);
 //nothing should have been loaded
 assertThat(map.size(), is(0));
 //ensure nothing has been written
 assertThat(recordingCacheWriter.getWriteCount(), is(0L));
 assertThat(recordingCacheWriter.getDeleteCount(), is(0L));
}

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

/**
 * Ensure that a {@link Cache#getAll(java.util.Set)} will not load entries.
 */
@Test
public void shouldNotLoadUsingGetAll() {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 //get the keys
 Map<String, String> map = cache.getAll(keys);
 //assert that the map content is as expected
 assertThat(map.size(), is(0));
 for (String key : keys) {
  assertThat(map.containsKey(key), is(false));
 }
 //assert that the loader state is as expected
 assertThat(cacheLoader.getLoadCount(), is(0));
 for (String key : keys) {
  assertThat(cacheLoader.hasLoaded(key), is(false));
 }
 //attempting to load the same keys should not result in loading
 cache.getAll(keys);
 assertThat(cacheLoader.getLoadCount(), is(0));
}

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

/**
 * Ensure that a {@link Cache#getAll(java.util.Set)} using one or more
 * <code>null</code> keys will not load anything.
 */
@Test
public void shouldNotLoadWithNullKeyUsingGetAll() {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 keys.add(null);
 //attempt to get the keys
 try {
  Map<String, String> map = cache.getAll(keys);
  fail("Should have thrown a NullPointerException");
 } catch (NullPointerException e) {
  //assert that nothing was actually loaded
  assertThat(cacheLoader.getLoadCount(), is(0));
  for (String key : keys) {
   assertThat(cacheLoader.hasLoaded(key), is(false));
  }
 }
}

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

/**
 * Ensure that a {@link Cache#getAll(java.util.Set)} using one or more
 * <code>null</code> keys will not load anything.
 */
@Test
public void shouldNotLoadWithNullKeyUsingGetAll() {
 RecordingCacheLoader<String> cacheLoader = new RecordingCacheLoader<String>();
 cacheLoaderServer.setCacheLoader(cacheLoader);
 //construct a set of keys
 HashSet<String> keys = new HashSet<String>();
 keys.add("gudday");
 keys.add("hello");
 keys.add("howdy");
 keys.add("bonjour");
 keys.add(null);
 //attempt to get the keys
 try {
  Map<String, String> map = cache.getAll(keys);
  fail("Should have thrown a NullPointerException");
 } catch (NullPointerException e) {
  //assert that nothing was actually loaded
  assertThat(cacheLoader.getLoadCount(), is(0));
  for (String key : keys) {
   assertThat(cacheLoader.hasLoaded(key), is(false));
  }
 }
}

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

@Test
public void shouldNotInvokeWriteThroughCallingGetAll() {
 int NUM_KEYS = 4;
 Set<Integer> keys = new HashSet<>();
 for (int i = 1; i <= NUM_KEYS; i++) {
  keys.add(i);
 }
 // getAll returns null case.
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 Map<Integer, String> map = cache.getAll(keys);
 assertTrue(map.size() == 0);
 assertEquals(0, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 // getAll returns non-null case.
 for (Integer key : keys) {
  cache.put(key, "value" + key);
 }
 assertEquals(NUM_KEYS, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
 map = cache.getAll(keys);
 assertEquals(keys.size(), map.size());
 assertEquals(NUM_KEYS, cacheWriter.getWriteCount());
 assertEquals(0, cacheWriter.getDeleteCount());
}

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

@Test
public void getAllShouldCallGetExpiryForAccessedEntry() {
 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);
 Set<Integer> keys = new HashSet<>();
 keys.add(1);
 keys.add(2);
 // when getting a non-existent entry, getExpiryForAccessedEntry is not called.
 cache.getAll(keys);
 assertThat(expiryPolicy.getCreationCount(), is(0));
 assertThat(expiryPolicy.getAccessCount(), is(0));
 assertThat(expiryPolicy.getUpdatedCount(), is(0));
 cache.put(1, 1);
 cache.put(2, 2);
 assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(2));
 assertThat(expiryPolicy.getAccessCount(), is(0));
 assertThat(expiryPolicy.getUpdatedCount(), is(0));
 expiryPolicy.resetCount();
 // when getting an existing entry, getExpiryForAccessedEntry is called.
 cache.get(1);
 cache.get(2);
 assertThat(expiryPolicy.getCreationCount(), is(0));
 assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(2));
 assertThat(expiryPolicy.getUpdatedCount(), is(0));
 expiryPolicy.resetCount();
}

相关文章