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

The following examples show how to use javax.cache.Cache#getAll() . 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: CacheLoaderTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testReadThroughGetAll() {

    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<String, String> config
                = new MutableConfiguration<String, String>()
                .setTypes(String.class, String.class)
                .setCacheLoaderFactory(new FactoryBuilder.ClassFactory(MockCacheLoader.class))
                .setReadThrough(true);

        Cache<String, String> cache = cacheManager.createCache("simpleCache", config);

        String key = "key";
        Map<String, String> result = cache.getAll(new HashSet<>(Arrays.asList(key)));
        assertEquals("LOADED_" + key, result.get(key));
        assertEquals("LOADED_" + key, cache.get(key));
    }
}
 
Example 2
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 3
Source File: JCacheTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAll() 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);
    
    cache.put("1", "2");
    cache.put("3", "4");
    
    Map<String, String> entries = cache.getAll(new HashSet<String>(Arrays.asList("1", "3", "7")));
    Map<String, String> expected = new HashMap<String, String>();
    expected.put("1", "2");
    expected.put("3", "4");
    assertThat(entries).isEqualTo(expected);
    
    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 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();
}
 
Example 5
Source File: JCacheGuiceTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@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)));
}