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

The following examples show how to use javax.cache.Cache#invokeAll() . 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: CacheVersionedEntryAbstractTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * @throws Exception If failed.
 */
@Test
public void testInvokeAll() throws Exception {
    Cache<Integer, String> cache = grid(0).cache(DEFAULT_CACHE_NAME);

    Set<Integer> keys = new HashSet<>();

    for (int i = 0; i < ENTRIES_NUM; i++)
        keys.add(i);

    Map<Integer, EntryProcessorResult<Object>> res = cache.invokeAll(keys, new EntryProcessor<Integer, String, Object>() {
        @Override public Object process(MutableEntry<Integer, String> entry, Object... args) {
            CacheEntry<Integer, String> verEntry = entry.unwrap(CacheEntry.class);

            checkVersionedEntry(verEntry);

            return verEntry.version();
        }
    });

    assertEquals(ENTRIES_NUM, res.size());
}
 
Example 2
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 3
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
    //establish and open a CacheLoaderServer to handle cache
    //cache loading requests from a CacheLoaderClient

    // this cacheLoader just returns the key as the value.
    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy));
    config.setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(recordingCacheLoader));
    config.setReadThrough(true);

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

    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;

    // set keys to read through
    Set<Integer> keys = new HashSet<>();
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
        keys.add(key);
    }

    // verify read-through of getValue of non-existent entries
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

    assertTrue(expiryPolicy.getCreationCount() >= keys.size());
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

}
 
Example 4
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 4 votes vote down vote up
@Test
public void invokeAllSetValueShouldCallGetExpiry() {

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicy));
    Cache<Integer, Integer> cache = getCacheManager().createCache("test-234", config);

    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
    final Integer setValue = 456;
    final Integer modifySetValue = 789;

    // set half of the keys so half of invokeAll will be modify and rest will be create.
    Set<Integer> keys = new HashSet<>();
    int createdCount = 0;
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
        keys.add(key);
        if (key <= MAX_KEY_VALUE - 2) {
            cache.put(key, setValue);
            createdCount++;
        }
    }

    assertTrue(expiryPolicy.getCreationCount() >= createdCount);
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();

    // verify modify or create
    cache.invokeAll(keys, new SetEntryProcessor<Integer, Integer>(setValue));

    assertTrue(expiryPolicy.getCreationCount() >= (keys.size() - createdCount));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertTrue(expiryPolicy.getUpdatedCount() >= (createdCount));
    expiryPolicy.resetCount();

    // verify accessed
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

    assertThat(expiryPolicy.getCreationCount(), is(0));
    System.out.println("expiryPolicy.getAccessCount():" + expiryPolicy.getAccessCount());
    assertTrue(expiryPolicy.getAccessCount() >= (keys.size()));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 5
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void invokeAllSetValueShouldCallGetExpiry() {

  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);

  final Integer INITIAL_KEY = 123;
  final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;
  final Integer setValue = 456;
  final Integer modifySetValue = 789;

  // set half of the keys so half of invokeAll will be modify and rest will be create.
  Set<Integer> keys = new HashSet<>();
  int createdCount = 0;
  for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
    keys.add(key);
    if (key <= MAX_KEY_VALUE - 2) {
      cache.put(key, setValue);
      createdCount++;
    }
  }

  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(createdCount));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
  expiryPolicy.resetCount();

  // verify modify or create
  cache.invokeAll(keys, new SetEntryProcessor<Integer, Integer>(setValue));

  assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size() - createdCount));
  assertThat(expiryPolicy.getAccessCount(), is(0));
  assertThat(expiryPolicy.getUpdatedCount(), greaterThanOrEqualTo(createdCount));
  expiryPolicy.resetCount();

  // verify accessed
  cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

  assertThat(expiryPolicy.getCreationCount(), is(0));
  assertThat(expiryPolicy.getAccessCount(), greaterThanOrEqualTo(keys.size()));
  assertThat(expiryPolicy.getUpdatedCount(), is(0));
}
 
Example 6
Source File: CacheExpiryTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test
public void invokeAllReadThroughEnabledGetOnNonExistentEntry() throws IOException {
  //establish and open a CacheLoaderServer to handle cache
  //cache loading requests from a CacheLoaderClient

  // this cacheLoader just returns the key as the value.
  RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
  try (CacheLoaderServer<Integer, Integer> cacheLoaderServer = new CacheLoaderServer<>(10000, recordingCacheLoader)) {
    cacheLoaderServer.open();

    //establish a CacheLoaderClient that a Cache can use for loading entries
    //(via the CacheLoaderServer)
    CacheLoaderClient<Integer, Integer> cacheLoader =
        new CacheLoaderClient<>(cacheLoaderServer.getInetAddress(), cacheLoaderServer.getPort());

    CountingExpiryPolicy expiryPolicy = new CountingExpiryPolicy();
    expiryPolicyServer.setExpiryPolicy(expiryPolicy);

    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient));
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(cacheLoader));
    config.setReadThrough(true);

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

    final Integer INITIAL_KEY = 123;
    final Integer MAX_KEY_VALUE = INITIAL_KEY + 4;

    // set keys to read through
    Set<Integer> keys = new HashSet<>();
    for (int key = INITIAL_KEY; key <= MAX_KEY_VALUE; key++) {
      keys.add(key);
    }

    // verify read-through of getValue of non-existent entries
    cache.invokeAll(keys, new GetEntryProcessor<Integer, Integer>());

    assertThat(expiryPolicy.getCreationCount(), greaterThanOrEqualTo(keys.size()));
    assertThat(expiryPolicy.getAccessCount(), is(0));
    assertThat(expiryPolicy.getUpdatedCount(), is(0));
    expiryPolicy.resetCount();
    closeTestCache();
  }
}