Java Code Examples for javax.cache.CacheManager#createCache()

The following examples show how to use javax.cache.CacheManager#createCache() . 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: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * https://github.com/jsr107/jsr107tck/issues/87
 *
 * Changed in 1.1, getCacheNames() throws exception after closing.
 */
@Test
public void close_cachesEmpty() {
  CacheManager cacheManager = getCacheManager();

  cacheManager.createCache("c1", new MutableConfiguration());
  cacheManager.createCache("c2", new MutableConfiguration());

  cacheManager.close();
  try {
    cacheManager.getCacheNames();
    fail();
  } catch (IllegalStateException e) {
    //expected
  }
}
 
Example 2
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void getCaches_MutateReturn() {
  CacheManager cacheManager = getCacheManager();

  cacheManager.createCache("c1", new MutableConfiguration());
  Cache cache1 = cacheManager.getCache("c1");

  try {
    Iterator iterator = cacheManager.getCacheNames().iterator();
    iterator.next();
    iterator.remove();
    fail();
  } catch (UnsupportedOperationException e) {
    // immutable
  }
}
 
Example 3
Source File: Eh107XmlIntegrationTest.java    From ehcache3 with Apache License 2.0 6 votes vote down vote up
@Test
public void testTemplateAddsListeners() throws Exception {
  CacheManager cacheManager = cachingProvider.getCacheManager(getClass().getResource("/ehcache-107-listeners.xml")
      .toURI(), getClass().getClassLoader());

  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  MutableCacheEntryListenerConfiguration<String, String> listenerConfiguration = new MutableCacheEntryListenerConfiguration<>(Test107CacheEntryListener::new, null, false, true);
  configuration.addCacheEntryListenerConfiguration(listenerConfiguration);

  Cache<String, String> cache = cacheManager.createCache("foos", configuration);
  cache.put("Hello", "Bonjour");

  assertThat(Test107CacheEntryListener.seen.size(), Matchers.is(1));
  assertThat(TestCacheEventListener.seen.size(), Matchers.is(1));
}
 
Example 4
Source File: ReadWriteICacheTest.java    From hazelcast-simulator with Apache License 2.0 6 votes vote down vote up
@Setup
public void setup() {
    counters = targetInstance.getList(name + "counters");

    RecordingCacheLoader<Integer> loader = new RecordingCacheLoader<>();
    loader.loadDelayMs = getDelayMs;

    RecordingCacheWriter<Integer, Integer> writer = new RecordingCacheWriter<>();
    writer.writeDelayMs = putDelayMs;
    writer.deleteDelayMs = removeDelayMs;

    config = new CacheConfig<>();
    config.setReadThrough(true);
    config.setWriteThrough(true);
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(loader));
    config.setCacheWriterFactory(FactoryBuilder.factoryOf(writer));

    CacheManager cacheManager = createCacheManager(targetInstance);
    cacheManager.createCache(name, config);
    cache = cacheManager.getCache(name);
}
 
Example 5
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
    public void shouldNotLoadWithNullKeyUsingLoadAll() throws Exception {

        HashSet<String> keys = new HashSet<>();
        keys.add(null);

        try {
            CacheManager cacheManager = getCacheManager();
            Cache<String, String> cache = cacheManager.createCache("test", new MutableConfiguration<String, String>());
            CompletionListenerFuture future = new CompletionListenerFuture();
            cache.loadAll(keys, false, future);

            fail("Expected a NullPointerException");
        } catch (NullPointerException e) {
            //SKIP: expected
        } finally {
//            assertThat(cacheLoader.getLoadCount(), is(0));
        }
    }
 
Example 6
Source File: OAuth2Test.java    From openwebbeans-meecrowave with Apache License 2.0 6 votes vote down vote up
private void createRedirectedClient(final int httpPort) throws URISyntaxException {
    final CachingProvider provider = Caching.getCachingProvider();
    final CacheManager cacheManager = provider.getCacheManager(
            ClassLoaderUtils.getResource("default-oauth2.jcs", OAuth2Test.class).toURI(),
            Thread.currentThread().getContextClassLoader());
    Cache<String, org.apache.cxf.rs.security.oauth2.common.Client> cache;
    try {
        cache = cacheManager
                .createCache(JCacheCodeDataProvider.CLIENT_CACHE_KEY, new MutableConfiguration<String, org.apache.cxf.rs.security.oauth2.common.Client>()
                        .setTypes(String.class, org.apache.cxf.rs.security.oauth2.common.Client.class)
                        .setStoreByValue(true));
    } catch (final RuntimeException re) {
        cache = cacheManager.getCache(JCacheCodeDataProvider.CLIENT_CACHE_KEY, String.class, org.apache.cxf.rs.security.oauth2.common.Client.class);
    }
    final org.apache.cxf.rs.security.oauth2.common.Client value = new org.apache.cxf.rs.security.oauth2.common.Client("c1", "cpwd", true);
    value.setRedirectUris(singletonList("http://localhost:" + httpPort + "/redirected"));
    cache.put("c1", value);
}
 
Example 7
Source File: ImmediateExpiryTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void immediate()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default",
            new MutableConfiguration<>()
                    .setExpiryPolicyFactory(
                            new FactoryBuilder.SingletonFactory<ExpiryPolicy>(new CreatedExpiryPolicy(Duration.ZERO))));
    final Cache<String, String> cache = cacheManager.getCache("default");
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", "bar");
    assertFalse(cache.containsKey("foo"));
    cachingProvider.close();
}
 
Example 8
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getTypedCache() {
  CacheManager cacheManager = getCacheManager();

  MutableConfiguration<String, Long> config = new MutableConfiguration<String, Long>().setTypes(String.class, Long.class);

  cacheManager.createCache("typed-cache", config);

  Cache<String, Long> cache = cacheManager.getCache("typed-cache", String.class, Long.class);

  assertNotNull(cache);
  assertEquals(String.class, cache.getConfiguration(CompleteConfiguration.class).getKeyType());
  assertEquals(Long.class, cache.getConfiguration(CompleteConfiguration.class).getValueType());
}
 
Example 9
Source File: CacheStopAndDestroySelfTest.java    From ignite with Apache License 2.0 5 votes vote down vote up
/**
 * Tests start -> destroy -> start -> close using CacheManager.
 */
@Test
public void testTckStyleCreateDestroyClose() throws Exception {
    startGridsMultiThreaded(gridCount());

    CacheManager mgr = Caching.getCachingProvider().getCacheManager();

    String cacheName = "cache";

    mgr.createCache(cacheName, new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class));

    mgr.destroyCache(cacheName);

    Cache<Integer, String> cache = mgr.createCache(cacheName,
        new MutableConfiguration<Integer, String>().setTypes(Integer.class, String.class));

    cache.close();

    // Check second close succeeds without exception.
    cache.close();

    try {
        cache.get(1);

        fail();
    }
    catch (IllegalStateException ignored) {
        // No-op;
    }
}
 
Example 10
Source File: CacheLoaderIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void setup() {
    // Adding fully qualified class name because of multiple Cache Provider (Ignite and Hazelcast)
    CachingProvider cachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider");
    CacheManager cacheManager = cachingProvider.getCacheManager();
    MutableConfiguration<Integer, String> config = new MutableConfiguration<Integer, String>().setReadThrough(true).setCacheLoaderFactory(new FactoryBuilder.SingletonFactory<>(new SimpleCacheLoader()));
    this.cache = cacheManager.createCache("SimpleCache", config);
}
 
Example 11
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected=UnsupportedOperationException.class)
public void no_online_listener_attachment_with_cache2k_defaults() {
  CachingProvider p = Caching.getCachingProvider();
  CacheManager cm = p.getCacheManager();
  MutableConfiguration cfg = ExtendedMutableConfiguration.of(new Cache2kBuilder<Long, Double>(){});
  Cache cache = cm.createCache("mute",  cfg);
  cache.registerCacheEntryListener(new CacheEntryListenerConfiguration() {
    @Override
    public Factory<CacheEntryListener> getCacheEntryListenerFactory() {
      fail("not expected to be called");
      return null;
    }

    @Override
    public boolean isOldValueRequired() {
      return false;
    }

    @Override
    public Factory<CacheEntryEventFilter> getCacheEntryEventFilterFactory() {
      return null;
    }

    @Override
    public boolean isSynchronous() {
      return false;
    }
  });
  cache.close();
}
 
Example 12
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void createCache_NullCacheConfiguration() {
  CacheManager cacheManager = getCacheManager();
  try {
    cacheManager.createCache("cache", null);
    fail("should have thrown an exception - null cache configuration not allowed");
  } catch (NullPointerException e) {
    //good
  }
}
 
Example 13
Source File: JCacheMetricsCompatibilityTest.java    From micrometer with Apache License 2.0 5 votes vote down vote up
JCacheMetricsCompatibilityTest() {
    CacheManager cacheManager = new RICachingProvider().getCacheManager();

    MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
    configuration.setTypes(String.class, String.class)
            .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR))
            .setStatisticsEnabled(true);

    this.cache = cacheManager.createCache("mycache", configuration);
}
 
Example 14
Source File: CacheLoaderTest.java    From hazelcast-simulator with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
    loaderList = targetInstance.getList(name + "loaders");

    config = new MutableConfiguration<>();
    config.setReadThrough(true);

    RecordingCacheLoader<Integer> recordingCacheLoader = new RecordingCacheLoader<>();
    recordingCacheLoader.loadAllDelayMs = loadAllDelayMs;
    config.setCacheLoaderFactory(FactoryBuilder.factoryOf(recordingCacheLoader));

    CacheManager cacheManager = createCacheManager(targetInstance);
    cacheManager.createCache(name, config);
    cache = cacheManager.getCache(name);
}
 
Example 15
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void create_cache2k_config_value_type_mismatch() {
  CachingProvider p = Caching.getCachingProvider();
  CacheManager cm = p.getCacheManager();
  MutableConfiguration cfg = ExtendedMutableConfiguration.of(new Cache2kBuilder<Long, Double>(){});
  Cache<Integer, Double> cache = cm.createCache("aCache",  cfg.setTypes(Long.class, Float.class));
  cache.close();
}
 
Example 16
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void createCache_Same() {
    String name = "c1";
    CacheManager cacheManager = getCacheManager();
    try {
        cacheManager.createCache(name, new MutableConfiguration());
        Cache cache1 = cacheManager.getCache(name);
        cacheManager.createCache(name, new MutableConfiguration());
        Cache cache2 = cacheManager.getCache(name);
        fail();
    } catch (CacheException exception) {
        //expected
    }
}
 
Example 17
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test(expected = NullPointerException.class)
public void getCacheNullValueClass() {
    String name = "c1";
    CacheManager manager = Caching.getCachingProvider().getCacheManager();
    manager.createCache(name, new MutableConfiguration().setTypes(Long.class, String.class));
    try {
        Caching.getCache(name, Long.class, null);
    } finally {
        manager.destroyCache(name);
    }
}
 
Example 18
Source File: RateLimitingFilter.java    From okta-jhipster-microservices-oauth-example with Apache License 2.0 5 votes vote down vote up
public RateLimitingFilter(JHipsterProperties jHipsterProperties) {
    this.jHipsterProperties = jHipsterProperties;

    CachingProvider cachingProvider = Caching.getCachingProvider();
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CompleteConfiguration<String, GridBucketState> config =
        new MutableConfiguration<String, GridBucketState>()
            .setTypes(String.class, GridBucketState.class);

    this.cache = cacheManager.createCache(GATEWAY_RATE_LIMITING_CACHE_NAME, config);
    this.buckets = Bucket4j.extension(JCache.class).proxyManagerForCache(cache);
}
 
Example 19
Source File: JCacheEhCacheAnnotationTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Bean
public CacheManager jCacheManager() {
	CacheManager cacheManager = this.cachingProvider.getCacheManager();
	MutableConfiguration<Object, Object> mutableConfiguration = new MutableConfiguration<>();
	mutableConfiguration.setStoreByValue(false);  // otherwise value has to be Serializable
	cacheManager.createCache("testCache", mutableConfiguration);
	cacheManager.createCache("primary", mutableConfiguration);
	cacheManager.createCache("secondary", mutableConfiguration);
	return cacheManager;
}
 
Example 20
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 3 votes vote down vote up
@Test(expected = ClassCastException.class)
public void getIncorrectCacheValueType() {
  CacheManager cacheManager = getCacheManager();

  MutableConfiguration<String, Long> config = new MutableConfiguration<String, Long>().setTypes(String.class, Long.class);

  cacheManager.createCache("typed-cache", config);

  Cache<String, String> cache = cacheManager.getCache("typed-cache", String.class, String.class);
}