Java Code Examples for javax.cache.Cache#putAll()

The following examples show how to use javax.cache.Cache#putAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IgniteCacheNearRestartRollbackSelfTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 2
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPutAll() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);
    
    Map<String, String> map = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        map.put("" + i, "" + i);
    }
    
    long start = System.currentTimeMillis();
    cache.putAll(map);
    System.out.println(System.currentTimeMillis() - start);
    
    for (int i = 0; i < 10000; i++) {
        assertThat(cache.containsKey("" + i)).isTrue();
    }
    
    cache.close();
    runner.stop();
}
 
Example 3
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAllHighVolume() throws Exception {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .port(6311)
            .run();
    
    URL configUrl = getClass().getResource("redisson-jcache.json");
    Config cfg = Config.fromJSON(configUrl);
    
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager()
            .createCache("test", config);

    Map<String, String> m = new HashMap<>();
    for (int i = 0; i < 10000; i++) {
        m.put("" + i, "" + i);
    }
    cache.putAll(m);
    
    Map<String, String> entries = cache.getAll(m.keySet());
    assertThat(entries).isEqualTo(m);
    
    cache.close();
    runner.stop();
}
 
Example 4
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 5
Source File: CacheWriterTest.java    From blazingcache with Apache License 2.0 4 votes vote down vote up
@Test
public void shouldWriteThoughUsingPutAll_partialSuccess() {
    BatchPartialSuccessRecordingClassWriter<Integer, String> cacheWriter = new BatchPartialSuccessRecordingClassWriter<>(3, 100);
    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<Integer, String> config
                = new MutableConfiguration<Integer, String>()
                .setTypes(Integer.class, String.class)
                .setCacheWriterFactory(new FactoryBuilder.SingletonFactory<>(cacheWriter))
                .setWriteThrough(true);

        Cache<Integer, String> cache = cacheManager.createCache("simpleCache", config);
        assertEquals(0, cacheWriter.getWriteCount());
        assertEquals(0, cacheWriter.getDeleteCount());

        HashMap<Integer, String> map = new HashMap<>();
        map.put(1, "Gudday World");
        map.put(2, "Bonjour World");
        map.put(3, "Hello World");
        map.put(4, "Ciao World");

        try {
            cache.putAll(map);
            assertTrue("expected CacheWriterException to be thrown for BatchPartialSuccessRecordingClassWriter", false);
        } catch (CacheWriterException cwe) {

            // ignore expected exception
        }

        int numSuccess = 0;
        int numFailure = 0;
        for (Integer key : map.keySet()) {
            if (cacheWriter.hasWritten(key)) {
                assertTrue(cache.containsKey(key));
                assertEquals(map.get(key), cacheWriter.get(key));
                assertEquals(map.get(key), cache.get(key));
                numSuccess++;
            } else {
                assertFalse(cache.containsKey(key));
                assertFalse(cacheWriter.hasWritten(key));
                assertEquals(cache.get(key), cacheWriter.get(key));
                numFailure++;
            }

            assertEquals(cache.get(key), cacheWriter.get(key));
        }

        assertEquals(numSuccess + numFailure, map.size());
        assertEquals(numSuccess, cacheWriter.getWriteCount());
        assertEquals(0, cacheWriter.getDeleteCount());
    }
}
 
Example 6
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 4 votes vote down vote up
@Test
public void expire_whenAccessed() {
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ParameterizedExpiryPolicy(Duration.ETERNAL, Duration.ZERO, null)));

    Cache<Integer, Integer> cache = getCacheManager().createCache("test-aaa", config);

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.getAndReplace(1, 2));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.getAndRemove(1));

    cache.put(1, 1);
    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.remove(1));

    cache.put(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.remove(1, 1));

    cache.getAndPut(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.getAndPut(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.getAndPut(1, 1));
    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.putIfAbsent(1, 1);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    map.put(1, 1);
    cache.putAll(map);

    assertTrue(cache.containsKey(1));
    assertNotNull(cache.get(1));
    assertFalse(cache.containsKey(1));
    assertNull(cache.get(1));

    cache.put(1, 1);
    Iterator<Cache.Entry<Integer, Integer>> iterator = cache.iterator();

    assertTrue(iterator.hasNext());
    assertEquals((Integer) 1, iterator.next().getValue());
    assertFalse(cache.iterator().hasNext());
}
 
Example 7
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that a cache using a {@link javax.cache.expiry.ExpiryPolicy} configured to
 * return a {@link Duration#ZERO} for newly created entries will immediately
 * expire the entries.
 */
private void expire_whenCreated(Factory<? extends ExpiryPolicy> expiryPolicyFactory) {
  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
  config.setTypes(Integer.class, Integer.class);
  config.setExpiryPolicyFactory(expiryPolicyFactory);
  config = extraSetup(config);
  config.setStatisticsEnabled(true);

  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  cache.put(1, 1);


  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));
  assertEquals(0, listener.getCreated());

  cache.put(1, 1);

  assertFalse(cache.remove(1));

  cache.put(1, 1);

  assertFalse(cache.remove(1, 1));

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.putIfAbsent(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  map.put(1, 1);
  cache.putAll(map);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);
  assertEquals(0, listener.getCreated());

  assertFalse(cache.iterator().hasNext());

  cache.getAndPut(1, 1);
  assertEquals(0, listener.getCreated());
}
 
Example 8
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that a cache using a {@link javax.cache.expiry.ExpiryPolicy} configured to
 * return a {@link Duration#ZERO} after accessing entries will immediately
 * expire the entries.
 */
@Test
public void expire_whenAccessed() {
  MutableConfiguration<Integer, Integer> config = new MutableConfiguration<Integer, Integer>();
  config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ParameterizedExpiryPolicy(Duration.ETERNAL, Duration.ZERO, null)));

  Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.getAndReplace(1, 2));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.getAndRemove(1));

  cache.put(1, 1);
  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.remove(1));

  cache.put(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.remove(1, 1));

  cache.getAndPut(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.getAndPut(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.getAndPut(1, 1));
  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.putIfAbsent(1, 1);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
  map.put(1, 1);
  cache.putAll(map);

  assertTrue(cache.containsKey(1));
  assertNotNull(cache.get(1));
  assertFalse(cache.containsKey(1));
  assertNull(cache.get(1));

  cache.put(1, 1);
  Iterator<Entry<Integer, Integer>> iterator = cache.iterator();

  assertTrue(iterator.hasNext());
  assertEquals((Integer) 1, iterator.next().getValue());
  assertFalse(cache.iterator().hasNext());
}