org.ehcache.Cache.getAll()方法的使用及代码示例

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

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

Cache.getAll介绍

[英]Retrieves all values associated with the given key set.
[中]检索与给定键集关联的所有值。

代码示例

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

@Test
public void testSimpleGetAll() throws Exception {
 Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));
 testCache.put(1, "one");
 testCache.put(2, "two");
 Map<Number, CharSequence> all = testCache.getAll(new HashSet<Number>(Arrays.asList(1, 2, 3)));
 assertThat(all.keySet(), containsInAnyOrder((Number)1, 2, 3));
 assertThat(all.get(1), Matchers.<CharSequence>equalTo("one"));
 assertThat(all.get(2), Matchers.<CharSequence>equalTo("two"));
 assertThat(all.get(3), is(nullValue()));
}

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

@Test
public void testBulkOps() throws Exception {
 List<Cache<Long, String>> caches = new ArrayList<>();
 caches.add(CACHE1);
 caches.add(CACHE2);
 Map<Long, String> entriesMap = new HashMap<>();
 entriesMap.put(1L, "one");
 entriesMap.put(2L, "two");
 entriesMap.put(3L, "three");
 entriesMap.put(4L, "four");
 entriesMap.put(5L, "five");
 entriesMap.put(6L, "six");
 caches.forEach(cache -> cache.putAll(entriesMap));
 CLUSTER.getClusterControl().terminateActive();
 Set<Long> keySet = entriesMap.keySet();
 caches.forEach(cache -> {
  Map<Long, String> all = cache.getAll(keySet);
  assertThat(all.get(1L), is("one"));
  assertThat(all.get(2L), is("two"));
  assertThat(all.get(3L), is("three"));
  assertThat(all.get(4L), is("four"));
  assertThat(all.get(5L), is("five"));
  assertThat(all.get(6L), is("six"));
 });
}

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

@Test
public void testSimplePutWithExpiry_getAll() throws Exception {
 insert(testCache, getEntries());
 assertThat(cacheSize(testCache), is(2));
 manualTimeSource.setTimeMillis(1001);
 assertThat(testCache.getAll(new HashSet<Number>(Arrays.asList(1, 2))).size(), is(2));
}

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

@Test
public void testBasicOps() {
 client1 = cacheManager.createCache("basicops" + cacheConsistency.name(), configuration);
 assertThat(sor.isEmpty(), is(true));
 Set<Long> keys = new HashSet<>();
 ThreadLocalRandom.current().longs(10).forEach(x -> {
  keys.add(x);
  client1.put(x, Long.toString(x));
 });
 assertThat(sor.size(), is(10));
 CacheManager anotherCacheManager = newCacheManager();
 Cache<Long, String> client2 = anotherCacheManager.createCache("basicops" + cacheConsistency.name(),
     getCacheConfig());
 Map<Long, String> all = client2.getAll(keys);
 assertThat(all.keySet(), containsInAnyOrder(keys.toArray()));
 keys.stream().limit(3).forEach(client2::remove);
 assertThat(sor.size(), is(7));
}

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

@Test
public void testClear() throws Exception {
 List<Cache<Long, String>> caches = new ArrayList<>();
 caches.add(CACHE1);
 caches.add(CACHE2);
 Map<Long, String> entriesMap = new HashMap<>();
 entriesMap.put(1L, "one");
 entriesMap.put(2L, "two");
 entriesMap.put(3L, "three");
 entriesMap.put(4L, "four");
 entriesMap.put(5L, "five");
 entriesMap.put(6L, "six");
 caches.forEach(cache -> cache.putAll(entriesMap));
 Set<Long> keySet = entriesMap.keySet();
 caches.forEach(cache -> {
  Map<Long, String> all = cache.getAll(keySet);
  assertThat(all.get(1L), is("one"));
  assertThat(all.get(2L), is("two"));
  assertThat(all.get(3L), is("three"));
  assertThat(all.get(4L), is("four"));
  assertThat(all.get(5L), is("five"));
  assertThat(all.get(6L), is("six"));
 });
 CACHE1.clear();
 CACHE2.clear();
 CLUSTER.getClusterControl().terminateActive();
 keySet.forEach(x -> assertThat(CACHE1.get(x), nullValue()));
 keySet.forEach(x -> assertThat(CACHE2.get(x), nullValue()));
}

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

testCache.getAll(new HashSet<Number>(Arrays.asList(1, 2, 3, 4)));
 fail("expected BulkCacheLoadingException");
} catch (BulkCacheLoadingException ex) {

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

@Test
public void testBulkOps() {
 TestCacheLoaderWriter loaderWriter = new TestCacheLoaderWriter();
 CacheConfiguration<Long, String> cacheConfiguration = getCacheConfiguration(loaderWriter);
 CacheManager cacheManager = CacheManagerBuilder
     .newCacheManagerBuilder()
     .with(cluster(CLUSTER_URI).autoCreate())
     .withCache("cache-1", cacheConfiguration)
     .build(true);
 Cache<Long, String> cache = cacheManager.getCache("cache-1", Long.class, String.class);
 Map<Long, String> mappings = new HashMap<>();
 for (int i = 1; i <= 5; i++) {
  mappings.put((long) i, "" + i);
 }
 cache.putAll(mappings);
 assertThat(loaderWriter.storeMap.keySet(), containsInAnyOrder(mappings.keySet().toArray()));
 cache.clear();
 Map<Long, String> loadedData = cache.getAll(mappings.keySet());
 assertThat(mappings.keySet(), containsInAnyOrder(loadedData.keySet().toArray()));
 cache.removeAll(mappings.keySet());
 assertThat(loaderWriter.storeMap.isEmpty(), is(true));
}

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

@Test
public void testGetAll_cache_loader_throws_exception() throws Exception {
 CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
   heap(100));
 CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
 CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
 when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
 when(cacheLoaderWriter.loadAll(ArgumentMatchers.any(Iterable.class))).thenThrow(new Exception("Simulating an exception from the cache loader"));
 when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
 CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
 CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriter).build();
 CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
 Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
 Set<String> fewKeysSet = new HashSet<String>() {
  {
   add("key0");
   add("key2");
  }
 };
 // the call to getAll
 try {
  myCache.getAll(fewKeysSet);
  fail();
 } catch (BulkCacheLoadingException bcwe) {
  // since onHeapStore.bulkComputeIfAbsent sends batches of 1 element,
  assertThat(bcwe.getFailures().size(), is(2));
  assertThat(bcwe.getSuccesses().size(), is(0));
 }
}

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

@Test
public void testGetAll_store_throws_cache_exception() throws Exception {
 CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
   heap(100));
 CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
 CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
 CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
 when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
 when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
 CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider).using(new CustomStoreProvider());
 CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
 when(cacheLoaderWriter.loadAll(argThat(hasItems("key0", "key2")))).thenReturn( new HashMap(){{put("key0","value0");  put("key2","value2");}});
 Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
 Set<String> fewKeysSet = new HashSet<String>() {
  {
   add("key0");
   add("key2");
  }
 };
 // the call to getAll
 Map<String, String> fewEntries = myCache.getAll(fewKeysSet);
 assertThat(fewEntries.size(), is(2));
 assertThat(fewEntries.get("key0"), is("value0"));
 assertThat(fewEntries.get("key2"), is("value2"));
}

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

@Test
public void testGetAll_with_cache_loader() throws Exception {
 CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
   heap(100));
 CacheLoaderWriterProvider cacheLoaderWriterProvider = mock(CacheLoaderWriterProvider.class);
 CacheLoaderWriter cacheLoaderWriter = mock(CacheLoaderWriter.class);
 when(cacheLoaderWriter.load(ArgumentMatchers.any())).thenThrow(new RuntimeException("We should not have called .load() but .loadAll()"));
 when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);
 CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
 CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.withLoaderWriter(cacheLoaderWriter).build();
 CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
 when(cacheLoaderWriter.loadAll(argThat(IsCollectionContaining.<String>hasItem("key0")))).thenReturn(new HashMap(){{put("key0","value0");}});
 when(cacheLoaderWriter.loadAll(argThat(IsCollectionContaining.<String>hasItem("key2")))).thenReturn(new HashMap(){{put("key2","value2");}});
 Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
 Set<String> fewKeysSet = new HashSet<String>() {
  {
   add("key0");
   add("key2");
  }
 };
 // the call to getAll
 Map<String, String> fewEntries = myCache.getAll(fewKeysSet);
 assertThat(fewEntries.size(), is(2));
 assertThat(fewEntries.get("key0"), is("value0"));
 assertThat(fewEntries.get("key2"), is("value2"));
}

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

Map<Long, String> all = cache2.getAll(keySet);
assertThat(all.get(1L), is("one"));
assertThat(all.get(2L), is("two"));
all = cache1.getAll(keySet);
assertThat(all.get(1L), nullValue());
assertThat(all.get(2L), nullValue());

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

@Test
public void testGetAll_without_cache_loader() throws Exception {
 CacheConfigurationBuilder cacheConfigurationBuilder = CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class,
   heap(100));
 CacheConfiguration<String, String> cacheConfiguration = cacheConfigurationBuilder.build();
 CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder();
 CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);
 Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);
 for (int i = 0; i < 3; i++) {
  myCache.put("key" + i, "value" + i);
 }
 Set<String> fewKeysSet = new HashSet<String>() {
  {
   add("key0");
   add("key2");
  }
 };
 // the call to getAll
 Map<String, String> fewEntries = myCache.getAll(fewKeysSet);
 assertThat(fewEntries.size(), is(2));
 assertThat(fewEntries.get("key0"), is("value0"));
 assertThat(fewEntries.get("key2"), is("value2"));
}

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

@SuppressWarnings("unchecked")
@Test
public void getAll() {
 expect(cache.getAll(asSet(1))).containsExactly(MapEntry.entry(1, null));
 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

@SuppressWarnings("unchecked")
@Test
public void getAll() {
 expect(cache.getAll(asSet(1))).containsExactly(MapEntry.entry(1, null));
 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

cache.getAll(keys);
assertEquals(3, listener1.created.get());
assertEquals(4, listener1.updated.get());

代码示例来源: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(asSet(2)); // one remove
 changesOf(1, 4, 3, 2);
 cacheStatistics.clear();
 changesOf(-1, -4, -3, -2);
}

代码示例来源: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(asSet(2)); // one remove
 changesOf(1, 4, 3, 2);
 tierStatistics.clear();
 changesOf(-1, -4, -3, -2);
}

代码示例来源:origin: net.oschina.j2cache/j2cache-core

@Override
public Map<String, Object> get(Collection<String> keys) {
  return cache.getAll(keys.stream().collect(Collectors.toSet()));
}

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

public Collection<TransactionHusk> getUnconfirmedTxs() {
  LOCK.lock();
  Set<Sha3Hash> unconfirmedKeys = new HashSet<>(pendingKeys);
  Collection<TransactionHusk> unconfirmedTxs = pendingPool.getAll(unconfirmedKeys).values();
  if (unconfirmedTxs.size() > 0) {
    log.debug("unconfirmedKeys={} unconfirmedTxs={}",
        unconfirmedKeys.size(), unconfirmedTxs.size());
  }
  LOCK.unlock();
  return unconfirmedTxs;
}

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

public void batch(Set<Sha3Hash> keys) {
  LOCK.lock();
  if (keys.size() > 0) {
    Map<Sha3Hash, TransactionHusk> map = pendingPool.getAll(keys);
    int countOfBatchedTxs = map.size();
    for (Sha3Hash key : map.keySet()) {
      TransactionHusk foundTx = map.get(key);
      if (foundTx != null) {
        db.put(key.getBytes(), foundTx.getData());
        addReadCache(foundTx);
      } else {
        countOfBatchedTxs -= 1;
      }
    }
    this.countOfTxs += countOfBatchedTxs;
    this.flush(keys);
  }
  LOCK.unlock();
}

相关文章