Java Code Examples for javax.cache.spi.CachingProvider#getCacheManager()

The following examples show how to use javax.cache.spi.CachingProvider#getCacheManager() . 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/104
 * Changed in 1.1, don't do getCache(..., null, null)
 */
@Test
public void testReuseCacheManagerGetCache() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  URI uri = provider.getDefaultURI();

  CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(cacheManager.isClosed());
  cacheManager.close();
  assertTrue(cacheManager.isClosed());

  try {
    cacheManager.getCache("nonExistent", Integer.class, Integer.class);
    fail();
  } catch (IllegalStateException e) {
    //expected
  }

  CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(otherCacheManager.isClosed());

  assertNotSame(cacheManager, otherCacheManager);
}
 
Example 2
Source File: EntityCacheTest.java    From requery with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializeGetPut() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();

    EntityCache cache = new EntityCacheBuilder(Models.DEFAULT)
            .useReferenceCache(false)
            .useSerializableCache(true)
            .useCacheManager(cacheManager)
            .build();

    Person p = new Person();
    p.setName("Alice");
    p.setUUID(UUID.randomUUID());
    p.setAddress(new Address());
    p.getAddress().setType(AddressType.HOME);

    int id = 100;
    cache.put(Person.class, id, p);

    Person d = cache.get(Person.class, id);
    Assert.assertNotNull(d);
    Assert.assertNotSame(p, d);
    Assert.assertEquals(p.getName(), d.getName());
    Assert.assertEquals(p.getUUID(), d.getUUID());
}
 
Example 3
Source File: NotSerializableTest.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
@Test
public void run()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    final CacheManager cacheManager = cachingProvider.getCacheManager();
    cacheManager.createCache("default", new MutableConfiguration<String, NotSerializableAndImHappyWithIt>().setStoreByValue(false));
    final Cache<String, NotSerializableAndImHappyWithIt> cache = cacheManager.getCache("default");
    assertFalse(cache.containsKey("foo"));
    cache.put("foo", new NotSerializableAndImHappyWithIt("bar"));
    assertTrue(cache.containsKey("foo"));
    assertEquals("bar", cache.get("foo").name);
    cache.remove("foo");
    assertFalse(cache.containsKey("foo"));
    cache.close();
    cacheManager.close();
    cachingProvider.close();
}
 
Example 4
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void testReuseCacheManager() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  URI uri = provider.getDefaultURI();

  CacheManager cacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(cacheManager.isClosed());
  cacheManager.close();
  assertTrue(cacheManager.isClosed());

  try {
    cacheManager.createCache("Dog", new MutableConfiguration());
    fail();
  } catch (IllegalStateException e) {
    //expected
  }

  CacheManager otherCacheManager = provider.getCacheManager(uri, provider.getDefaultClassLoader());
  assertFalse(otherCacheManager.isClosed());

  assertNotSame(cacheManager, otherCacheManager);
}
 
Example 5
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@Test
public void closeCacheManagerByURIAndClassLoader() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);

  provider.close(manager1.getURI(), loader1);
  provider.close(manager2.getURI(), loader2);

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 6
Source File: ParametersInURITest.java    From blazingcache with Apache License 2.0 6 votes vote down vote up
@Test
public void testParametersInURI() throws URISyntaxException {

    CachingProvider cachingProvider = Caching.getCachingProvider();
    Properties p = new Properties();
    try (CacheManager cacheManager = cachingProvider.getCacheManager(new URI(cachingProvider.getDefaultURI() + "?secret=testsecret"), cachingProvider.getDefaultClassLoader(), p)) {
        MutableConfiguration<String, Integer> config
                = new MutableConfiguration<String, Integer>()
                .setTypes(String.class, Integer.class)
                .setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(ONE_HOUR))
                .setStatisticsEnabled(true);

        Cache<String, Integer> cache = cacheManager.createCache("simpleCache", config);
        BlazingCacheManager manager = cacheManager.unwrap(BlazingCacheManager.class);
        assertEquals("testsecret", manager.getProperties().get("blazingcache.secret"));

        String key = "key";
        Integer value1 = 1;
        cache.put("key", value1);
        Integer value2 = cache.get(key);
        assertEquals(value1, value2);
        cache.remove(key);
        assertNull(cache.get(key));
    }
}
 
Example 7
Source File: LoadAtomicsWith107Test.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  MockitoAnnotations.initMocks(this);

  CachingProvider provider = Caching.getCachingProvider();
  cacheManager = provider.getCacheManager(this.getClass().getResource("/ehcache-loader-writer-107-load-atomics.xml").toURI(), getClass().getClassLoader());

  testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>()
      .setReadThrough(true)
      .setWriteThrough(true)
      .setCacheLoaderFactory(() -> cacheLoader)
      .setCacheWriterFactory(() -> cacheWriter)
      .setTypes(Number.class, CharSequence.class));
}
 
Example 8
Source File: IteratorTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
    public void testIterateAndRemove() throws Exception {

        CachingProvider cachingProvider = Caching.getCachingProvider();
        Properties p = new Properties();
        try (CacheManager cacheManager = cachingProvider.getCacheManager(cachingProvider.getDefaultURI(), cachingProvider.getDefaultClassLoader(), p)) {
            Cache<Long, String> cache = cacheManager.createCache("test", new MutableConfiguration().setTypes(Long.class, String.class));
            BlazingCacheCache<Long, String> blazingCache = cache.unwrap(BlazingCacheCache.class);
            for (long i = 0; i < 100L; i++) {
                String word = "";
                word = word + " " + "Trinity";
                cache.put(i, word);
            }

            Iterator<Cache.Entry<Long, String>> iterator = cache.iterator();

            while (iterator.hasNext()) {
                iterator.next();
                iterator.remove();
            }

            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheHits());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheHitPercentage());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheMissPercentage());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheGets());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCachePuts());
            assertEquals(100, (int) blazingCache.getStatisticsMXBean().getCacheRemovals());
            assertEquals(0, (int) blazingCache.getStatisticsMXBean().getCacheEvictions());
//            assertTrue(, (int) blazingCache.getStatisticsMXBean().getCache
//            assertThat((Float) lookupManagementAttribute(cache, CacheStatistics, "AveragePutTime"), greaterThanOrEqualTo(0f));
//            assertThat((Float) lookupManagementAttribute(cache, CacheStatistics, "AverageRemoveTime"), greaterThanOrEqualTo(0f));
        }
    }
 
Example 9
Source File: CachingProviderTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void closeCacheManagers() {
  CachingProvider provider = Caching.getCachingProvider();

  ClassLoader loader1 = CLASS_LOADER;
  CacheManager manager1 = provider.getCacheManager(provider.getDefaultURI(), loader1, null);
  manager1.close();

  ClassLoader loader2 = new MyClassLoader(CLASS_LOADER);
  CacheManager manager2 = provider.getCacheManager(provider.getDefaultURI(), loader2, null);
  manager2.close();

  assertNotSame(manager1, provider.getCacheManager(provider.getDefaultURI(), loader1, null));
  assertNotSame(manager2, provider.getCacheManager(provider.getDefaultURI(), loader2, null));
}
 
Example 10
Source File: StatisticsTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception {
  CachingProvider provider = Caching.getCachingProvider();
  cacheManager = provider.getCacheManager(getClass().getResource("/ehcache-107-stats.xml").toURI(), ClassLoader.getSystemClassLoader());
  MutableConfiguration<String, String> configuration = new MutableConfiguration<>();
  configuration.setTypes(String.class, String.class);
  heapCache = cacheManager.createCache("heap", configuration);
  heapStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) heapCache).getStatisticsMBean();
  offheapCache = cacheManager.createCache("offheap", configuration);
  offheapStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) offheapCache).getStatisticsMBean();
  diskCache = cacheManager.createCache("disk", configuration);
  diskStatistics = (Eh107CacheStatisticsMXBean) ((Eh107Cache<String, String>) diskCache).getStatisticsMBean();
}
 
Example 11
Source File: CachingTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getCacheManager_nullClassLoader() {
  CachingProvider provider = Caching.getCachingProvider();
  final ClassLoader NULL_CLASSLOADER = null;

  // null classloader is treated as provider.getDefaultClassLoader().
  CacheManager manager = provider.getCacheManager(provider.getDefaultURI(), NULL_CLASSLOADER, null);
  assertNotNull(manager);
  CacheManager sameManager = provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), null);
  assertEquals(sameManager, manager);
  assertEquals(sameManager.getClassLoader(), manager.getClassLoader());
}
 
Example 12
Source File: CachingTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getCacheManager_nonNullProperties() {
  // make sure existing cache managers are closed and the non empty properties get picked up
  try {
    Caching.getCachingProvider().close();
  } catch (CacheException ignore) {
    // ignore exception which may happen if the provider is not active
  }
  CachingProvider provider = Caching.getCachingProvider();
  Properties properties = new Properties();
  properties.put("dummy.com", "goofy");
  provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), properties);
  CacheManager manager = provider.getCacheManager();
  assertEquals("goofy", manager.getProperties().get("dummy.com"));
}
 
Example 13
Source File: TestJCache.java    From jframe with Apache License 2.0 5 votes vote down vote up
@Test
public void createCacheTest() {
    CachingProvider provider = Caching.getCachingProvider();
    CacheManager cacheManager = provider.getCacheManager();
    MutableConfiguration<Long, String> configuration = new MutableConfiguration<Long, String>()
            .setTypes(Long.class, String.class).setStoreByValue(false)
            .setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(Duration.ONE_MINUTE));
    Cache<Long, String> cache = cacheManager.createCache("jCache", configuration);
    cache.put(1L, "one");
    String value = cache.get(1L);
    LOG.info(value);

}
 
Example 14
Source File: TCKCacheManagerTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void getCacheManager_nonNullProperties() {
    CachingProvider provider = Caching.getCachingProvider();
    Properties properties = new Properties();

    assertSame(provider.getCacheManager(),
            provider.getCacheManager(provider.getDefaultURI(), provider.getDefaultClassLoader(), new Properties()));

    try (CacheManager manager = provider.getCacheManager();) {
        assertEquals(properties, manager.getProperties());
    }
}
 
Example 15
Source File: CacheLoaderTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testLoadallReplaceExisting() throws Exception {

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

        Cache<String, String> cache = cacheManager.createCache("simpleCache", config);
        cache.put("one", "to_be_replaced");
        Set<String> keys = new HashSet<>();
        keys.add("one");
        keys.add("two");
        keys.add("three");
        CompletionListenerFuture future = new CompletionListenerFuture();
        cache.loadAll(keys, true, future);
        future.get();

        for (String key : keys) {
            String result = cache.get(key);
            assertEquals("LOADED_" + key, result);
        }
    }
}
 
Example 16
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void create_cache2k_config_wrap() {
  CachingProvider p = Caching.getCachingProvider();
  CacheManager cm = p.getCacheManager();
  Cache<Long, Double> cache = cm.createCache("aCache", ExtendedMutableConfiguration.of(
    new Cache2kBuilder<Long, Double>(){}
      .entryCapacity(10000)
      .expireAfterWrite(5, TimeUnit.MINUTES)
      .with(new JCacheConfiguration.Builder()
        .copyAlwaysIfRequested(true)
      )
  ));
  assertTrue(cache instanceof CopyCacheProxy);
  cache.close();
}
 
Example 17
Source File: InternalCacheRule.java    From commons-jcs with Apache License 2.0 4 votes vote down vote up
@Override
public Statement apply(final Statement base, final Description description)
{
    return new Statement()
    {
        @Override
        public void evaluate() throws Throwable
        {
            final CachingProvider provider = Caching.getCachingProvider();
            final CacheManager manager = provider.getCacheManager();
            try
            {
                Field cache = null;
                CompleteConfiguration<?, ?> config = null;
                for (final Field f : test.getClass().getDeclaredFields())
                {
                    if (Cache.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        cache = f;
                    }
                    else if (Configuration.class.isAssignableFrom(f.getType()))
                    {
                        f.setAccessible(true);
                        config = (CompleteConfiguration<?, ?>) f.get(test);
                    }
                }
                if (cache != null)
                {
                    if (config == null)
                    {
                        throw new IllegalStateException("Define a Configuration field");
                    }
                    cache.set(test, manager.createCache(cache.getName(), config));
                }
                base.evaluate();
            }
            finally
            {
                manager.close();
                provider.close();
            }
        }
    };
}
 
Example 18
Source File: TestUtils.java    From triava with Apache License 2.0 4 votes vote down vote up
public static CacheManager getDefaultCacheManager()
{
    CachingProvider cachingProvider = Caching.getCachingProvider();
    return cachingProvider.getCacheManager();
}
 
Example 19
Source File: AbstractJCacheTest.java    From caffeine with Apache License 2.0 4 votes vote down vote up
@BeforeClass(alwaysRun = true)
public void beforeClass() {
  CachingProvider provider = Caching.getCachingProvider(CaffeineCachingProvider.class.getName());
  cacheManager = provider.getCacheManager(
      provider.getDefaultURI(), provider.getDefaultClassLoader());
}
 
Example 20
Source File: SerializerTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  CachingProvider cachingProvider = Caching.getCachingProvider();
  cacheManager = cachingProvider.getCacheManager(getClass().getResource("/ehcache-107-serializer.xml")
      .toURI(), cachingProvider.getDefaultClassLoader());
}