当前位置: 首页>>代码示例>>Java>>正文


Java Cache.putAll方法代码示例

本文整理汇总了Java中org.ehcache.Cache.putAll方法的典型用法代码示例。如果您正苦于以下问题:Java Cache.putAll方法的具体用法?Java Cache.putAll怎么用?Java Cache.putAll使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.ehcache.Cache的用法示例。


在下文中一共展示了Cache.putAll方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testCacheRemoveAll

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testCacheRemoveAll() throws Exception {
    final Cache<Object, Object> cache = getTestCache();
    final Map<String, String> map = generateRandomMapOfString(3);
    final Set<String> keys = map.keySet().stream().limit(2).collect(Collectors.toSet());

    cache.putAll(map);

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, false);
    mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);

    fluentTemplate()
        .withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_REMOVE_ALL)
        .withHeader(EhcacheConstants.KEYS, keys)
        .to("direct://start")
        .send();

    assertMockEndpointsSatisfied();

    cache.forEach(e -> assertFalse(keys.contains(e.getKey())));
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:24,代码来源:EhcacheProducerTest.java

示例2: testSimplePutAllWithEviction

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testSimplePutAllWithEviction() throws Exception {
  Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache",
      CacheConfigurationBuilder.newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(2))
          .build());

  Map<Integer, String> values = new HashMap<>();
  values.put(1, "one");
  values.put(2, "two");
  values.put(3, "three");
  values.put(4, "four");

  testCache.putAll(values);

  int count = 0;
  for (@SuppressWarnings("unused") Cache.Entry<Number, CharSequence> entry : testCache) {
    count++;
  }
  assertThat(count, is(2));
}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:21,代码来源:EvictionEhcacheTest.java

示例3: testPutAll_without_cache_writer

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testPutAll_without_cache_writer() 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);

  HashMap<String, String> stringStringHashMap = new HashMap<>();
  for (int i = 0; i < 3; i++) {
    stringStringHashMap.put("key" + i, "value" + i);
  }

  // the call to putAll
  myCache.putAll(stringStringHashMap);

  for (int i = 0; i < 3; i++) {
    assertThat(myCache.get("key" + i), is("value" + i));
  }

}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:25,代码来源:EhcacheBulkMethodsITest.java

示例4: testCacheGetAll

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testCacheGetAll() throws Exception {
    final Cache<Object, Object> cache = getTestCache();
    final Map<String, String> map = generateRandomMapOfString(3);
    final Set<String> keys = map.keySet().stream().limit(2).collect(Collectors.toSet());

    cache.putAll(map);

    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMinimumMessageCount(1);
    mock.expectedHeaderReceived(EhcacheConstants.ACTION_HAS_RESULT, true);
    mock.expectedHeaderReceived(EhcacheConstants.ACTION_SUCCEEDED, true);

    fluentTemplate()
        .withHeader(EhcacheConstants.ACTION, EhcacheConstants.ACTION_GET_ALL)
        .withHeader(EhcacheConstants.KEYS, keys)
        .to("direct://start")
        .send();

    assertMockEndpointsSatisfied();

    final Map<String, String> elements = mock.getExchanges().get(0).getIn().getBody(Map.class);
    keys.forEach(k -> {
        assertTrue(elements.containsKey(k));
        assertEquals(map.get(k), elements.get(k));
    });
}
 
开发者ID:HydAu,项目名称:Camel,代码行数:28,代码来源:EhcacheProducerTest.java

示例5: testSimplePutAll

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testSimplePutAll() throws Exception {
  Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", newCacheConfigurationBuilder(Number.class, CharSequence.class, heap(10)));

  Map<Integer, String> values = new HashMap<>();
  values.put(1, "one");
  values.put(2, "two");
  values.put(3, "three");

  testCache.putAll(values);

  assertThat(testCache.get(1), Matchers.<CharSequence>equalTo("one"));
  assertThat(testCache.get(2), Matchers.<CharSequence>equalTo("two"));
  assertThat(testCache.get(3), Matchers.<CharSequence>equalTo("three"));
}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:16,代码来源:SimpleEhcacheTest.java

示例6: testPutAll_with_cache_writer

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testPutAll_with_cache_writer() 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);
  doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriter).write(ArgumentMatchers
      .any(), ArgumentMatchers.any());
  when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriter);

  CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
  CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);

  Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);

  HashMap<String, String> stringStringHashMap = new HashMap<>();
  for (int i = 0; i < 3; i++) {
    stringStringHashMap.put("key" + i, "value" + i);
  }

  // the call to putAll
  myCache.putAll(stringStringHashMap);

  verify(cacheLoaderWriter, times(3)).writeAll(ArgumentMatchers.any(Iterable.class));
  Set set = new HashSet(){{add(entry("key0", "value0"));}};
  verify(cacheLoaderWriter).writeAll(set);
  set = new HashSet(){{add(entry("key1", "value1"));}};
  verify(cacheLoaderWriter).writeAll(set);
  set = new HashSet(){{add(entry("key2", "value2"));}};
  verify(cacheLoaderWriter).writeAll(set);

  for (int i = 0; i < 3; i++) {
    assertThat(myCache.get("key" + i), is("value" + i));
  }

}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:40,代码来源:EhcacheBulkMethodsITest.java

示例7: testPutAll_with_cache_writer_that_throws_exception

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testPutAll_with_cache_writer_that_throws_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 cacheLoaderWriterThatThrows = mock(CacheLoaderWriter.class);
  doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriterThatThrows).write(ArgumentMatchers
      .any(), ArgumentMatchers.any());
  doThrow(new Exception("Simulating an exception from the cache writer")).when(cacheLoaderWriterThatThrows).writeAll(ArgumentMatchers.any(Iterable.class));
  when(cacheLoaderWriterProvider.createCacheLoaderWriter(anyString(), ArgumentMatchers.any(CacheConfiguration.class))).thenReturn(cacheLoaderWriterThatThrows);

  CacheManagerBuilder<CacheManager> managerBuilder = CacheManagerBuilder.newCacheManagerBuilder().using(cacheLoaderWriterProvider);
  CacheManager cacheManager = managerBuilder.withCache("myCache", cacheConfiguration).build(true);

  Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);

  HashMap<String, String> stringStringHashMap = new HashMap<>();
  for (int i = 0; i < 3; i++) {
    stringStringHashMap.put("key" + i, "value" + i);
  }

  // the call to putAll
  try {
    myCache.putAll(stringStringHashMap);
    fail();
  } catch (BulkCacheWritingException bcwe) {
    assertThat(bcwe.getFailures().size(), is(3));
    assertThat(bcwe.getSuccesses().size(), is(0));
  }

}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:35,代码来源:EhcacheBulkMethodsITest.java

示例8: testPutAll_store_throws_cache_exception

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
  public void testPutAll_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);
    doThrow(new RuntimeException("We should not have called .write() but .writeAll()")).when(cacheLoaderWriter).write(ArgumentMatchers.any(), ArgumentMatchers.any());
    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);

    Cache<String, String> myCache = cacheManager.getCache("myCache", String.class, String.class);

    Map<String, String> stringStringHashMap = new HashMap<>();
    for (int i = 0; i < 3; i++) {
      stringStringHashMap.put("key" + i, "value" + i);
    }

    // the call to putAll
    myCache.putAll(stringStringHashMap);

    for (int i = 0; i < 3; i++) {
      // the store threw an exception when we call bulkCompute
      assertThat(myCache.get("key" + i), is(nullValue()));
      // but still, the cache writer could writeAll the values !
//      assertThat(cacheWriterToHashMapMap.get("key" + i), is("value" + i));
    }
    // but still, the cache writer could writeAll the values at once !
    verify(cacheLoaderWriter, times(1)).writeAll(ArgumentMatchers.any(Iterable.class));
    Set set = new HashSet() {{add(entry("key0", "value0")); add(entry("key1", "value1")); add(entry("key2", "value2"));}};
    verify(cacheLoaderWriter).writeAll(set);
  }
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:38,代码来源:EhcacheBulkMethodsITest.java

示例9: basicClusteredBulk

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void basicClusteredBulk() throws Exception {
  final CacheManagerBuilder<PersistentCacheManager> clusteredCacheManagerBuilder =
      newCacheManagerBuilder()
          .with(cluster(CLUSTER.getConnectionURI().resolve("/bulk-cm")).autoCreate())
          .withCache("clustered-cache", newCacheConfigurationBuilder(Long.class, String.class,
              ResourcePoolsBuilder.newResourcePoolsBuilder()
                  .with(ClusteredResourcePoolBuilder.clusteredDedicated("primary-server-resource", 2, MemoryUnit.MB)))
              .add(new ClusteredStoreConfiguration(Consistency.STRONG)));

  try (PersistentCacheManager cacheManager1 = clusteredCacheManagerBuilder.build(true)) {

    try (PersistentCacheManager cacheManager2 = clusteredCacheManagerBuilder.build(true)) {
      final Cache<Long, String> cache1 = cacheManager1.getCache("clustered-cache", Long.class, String.class);
      final Cache<Long, String> cache2 = cacheManager2.getCache("clustered-cache", Long.class, String.class);

      Map<Long, String> entriesMap = new HashMap<>();
      entriesMap.put(1L, "one");
      entriesMap.put(2L, "two");
      entriesMap.put(3L, "three");
      cache1.putAll(entriesMap);

      Set<Long> keySet = new HashSet<>(Arrays.asList(1L, 2L, 3L));
      Map<Long, String> all = cache2.getAll(keySet);
      assertThat(all.get(1L), is("one"));
      assertThat(all.get(2L), is("two"));
      assertThat(all.get(3L), is("three"));

      cache2.removeAll(keySet);

      all = cache1.getAll(keySet);
      assertThat(all.get(1L), nullValue());
      assertThat(all.get(2L), nullValue());
      assertThat(all.get(3L), nullValue());
    }
  }
}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:38,代码来源:BasicClusteredCacheOpsTest.java

示例10: insert

import org.ehcache.Cache; //导入方法依赖的package包/类
@Override
protected void insert(Cache<Number, CharSequence> testCache, Map<Number, CharSequence> entries) {
  testCache.putAll(entries);
}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:5,代码来源:PutAllExpiryEhcacheTest.java

示例11: testBulkWrites

import org.ehcache.Cache; //导入方法依赖的package包/类
@Test
public void testBulkWrites() throws Exception {
  WriteBehindTestLoaderWriter<String, String> loaderWriter = new WriteBehindTestLoaderWriter<>();
  CacheLoaderWriterProvider cacheLoaderWriterProvider = getMockedCacheLoaderWriterProvider(loaderWriter);

  try (CacheManager cacheManager = managerBuilder().using(cacheLoaderWriterProvider).build(true)) {
    Cache<String, String> testCache = cacheManager.createCache("testBulkWrites", CacheConfigurationBuilder.newCacheConfigurationBuilder(String.class, String.class, heap(100))
      .add(newUnBatchedWriteBehindConfiguration().concurrencyLevel(3).queueSize(10).build())
      .build());

    CountDownLatch countDownLatch = new CountDownLatch(20);
    loaderWriter.setLatch(countDownLatch);
    for (int i = 0; i < 10; i++)
      testCache.put("test" + i, "test" + i);

    Map<String, String> entries = new HashMap<>(10);
    Set<String> keys = new HashSet<>(10);
    for (int i = 10; i < 20; i++) {
      entries.put("test" + i, "test" + i);
      keys.add("test" + i);
    }

    testCache.putAll(entries);
    countDownLatch.await(5, SECONDS);
    for (int i = 0; i < 20; i++) {
      assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i));
    }

    CountDownLatch countDownLatch1 = new CountDownLatch(10);
    loaderWriter.setLatch(countDownLatch1);
    testCache.removeAll(keys);

    countDownLatch1.await(5, SECONDS);

    assertThat(loaderWriter.getData().size(), is(20));
    for (int i = 0; i < 10; i++) {
      assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i));
    }
    for (int i = 10; i < 20; i++) {
      assertThat("Key : " + i, loaderWriter.getData().get("test" + i), contains("test" + i, null));
    }
  }
}
 
开发者ID:ehcache,项目名称:ehcache3,代码行数:44,代码来源:AbstractWriteBehindTestBase.java


注:本文中的org.ehcache.Cache.putAll方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。