Java Code Examples for javax.cache.Caching#getCachingProvider()

The following examples show how to use javax.cache.Caching#getCachingProvider() . 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: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * The default CacheManager is the same as the CacheManager using the default
 * CachingProvider URI.
 */
@Test
public void getCacheManagerDefaultURI() {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  CacheManager manager = provider.getCacheManager();
  assertEquals(provider.getDefaultURI(), manager.getURI());

  // using a different ClassLoader
  ClassLoader otherLoader = new MyClassLoader(contextLoader);
  CachingProvider otherProvider = Caching.getCachingProvider(otherLoader);
  assertNotSame(provider, otherProvider);

  CacheManager otherManager = otherProvider.getCacheManager();
  assertNotSame(manager, otherManager);
  assertEquals(otherProvider.getDefaultURI(), otherManager.getURI());
}
 
Example 2
Source File: JCacheOAuthDataProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
protected static CacheManager createCacheManager(String configFile, Bus bus) {
    if (bus == null) {
        bus = BusFactory.getThreadDefaultBus(true);
    }

    CachingProvider provider = Caching.getCachingProvider();

    URI configFileURI;
    try {
        configFileURI = getClasspathResourceURL(configFile, JCacheOAuthDataProvider.class, bus).toURI();
    } catch (Exception ex) {
        configFileURI = provider.getDefaultURI();
    }

    return provider.getCacheManager(configFileURI, Thread.currentThread().getContextClassLoader());
}
 
Example 3
Source File: ExtraJCacheExtension.java    From commons-jcs with Apache License 2.0 6 votes vote down vote up
public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
{
    if (!ACTIVATED)
    {
        return;
    }

    if (cacheManagerFound && cacheProviderFound) {
        return;
    }

    cachingProvider = Caching.getCachingProvider();
    if (!cacheManagerFound)
    {
        cacheManager = cachingProvider.getCacheManager(
                cachingProvider.getDefaultURI(),
                cachingProvider.getDefaultClassLoader(),
                new Properties());
        afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
    }
    if (!cacheProviderFound)
    {
        afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
    }
}
 
Example 4
Source File: CachingProviderClassLoaderTest.java    From cache2k with Apache License 2.0 6 votes vote down vote up
/**
 * Close all CacheManagers from a CachingProvider, each CacheManager being
 * based on a different ClassLoader.
 */
@Test
public void closeAllCacheManagers() throws Exception {
  ClassLoader contextLoader = Thread.currentThread().getContextClassLoader();

  CachingProvider provider = Caching.getCachingProvider(contextLoader);

  URI uri = provider.getDefaultURI();

  ClassLoader loader1 = new MyClassLoader(contextLoader);
  CacheManager manager1 = provider.getCacheManager(uri, loader1);

  ClassLoader loader2 = new MyClassLoader(contextLoader);
  CacheManager manager2 = provider.getCacheManager(uri, loader2);

  ClassLoader loader3 = new MyClassLoader(contextLoader);
  CacheManager manager3 = provider.getCacheManager(uri, loader3);

  provider.close();

  assertNotSame(manager1, provider.getCacheManager(uri, loader1));
  assertNotSame(manager2, provider.getCacheManager(uri, loader2));
  assertNotSame(manager3, provider.getCacheManager(uri, loader3));
}
 
Example 5
Source File: ExtraJCacheExtension.java    From jcache-cdi with Apache License 2.0 6 votes vote down vote up
public void addJCacheBeans(final @Observes AfterBeanDiscovery afterBeanDiscovery)
{
    if (!ACTIVATED)
    {
        return;
    }

    if (cacheManagerFound && cacheProviderFound) {
        return;
    }

    cachingProvider = Caching.getCachingProvider();
    if (!cacheManagerFound)
    {
        cacheManager = cachingProvider.getCacheManager(
                cachingProvider.getDefaultURI(),
                cachingProvider.getDefaultClassLoader(),
                new Properties());
        afterBeanDiscovery.addBean(new CacheManagerBean(cacheManager));
    }
    if (!cacheProviderFound)
    {
        afterBeanDiscovery.addBean(new CacheProviderBean(cachingProvider));
    }
}
 
Example 6
Source File: XmlConfigurationTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void cache2kSemanticsIfEmptyConfigurationPresent() throws Exception {
  CachingProvider p = Caching.getCachingProvider();
  javax.cache.CacheManager cm = p.getCacheManager(new URI("empty"), null);
  javax.cache.Cache<String, BigDecimal> c =
    cm.createCache("test2", new MutableConfiguration<String, BigDecimal>());
  assertTrue(c instanceof JCacheAdapter);
}
 
Example 7
Source File: CacheConfiguration.java    From airsonic-advanced with GNU General Public License v3.0 5 votes vote down vote up
@Bean
public javax.cache.CacheManager jCacheCacheManager() {
    CachingProvider provider = Caching.getCachingProvider("org.ehcache.jsr107.EhcacheCachingProvider");
    EhcacheCachingProvider ehcacheCachingProvider = (EhcacheCachingProvider) provider;
    return ehcacheCachingProvider.getCacheManager(
            ehcacheCachingProvider.getDefaultURI(),
            createConfig(provider.getDefaultClassLoader()));
}
 
Example 8
Source File: IteratorTest.java    From ehcache3 with Apache License 2.0 5 votes vote down vote up
@Test
public void testIterateExpiredIsSkipped() throws Exception {
  EhcacheCachingProvider provider = (EhcacheCachingProvider) Caching.getCachingProvider();
  TestTimeSource testTimeSource = new TestTimeSource();
  TimeSourceConfiguration timeSourceConfiguration = new TimeSourceConfiguration(testTimeSource);
  CacheManager cacheManager = provider.getCacheManager(new URI("test://testIterateExpiredReturnsNull"), new DefaultConfiguration(getClass().getClassLoader(), timeSourceConfiguration));

  Cache<Number, CharSequence> testCache = cacheManager.createCache("testCache", new MutableConfiguration<Number, CharSequence>()
      .setExpiryPolicyFactory(() -> new ExpiryPolicy() {
        @Override
        public Duration getExpiryForCreation() {
          return Duration.ETERNAL;
        }

        @Override
        public Duration getExpiryForAccess() {
          return new Duration(TimeUnit.SECONDS, 1L);
        }

        @Override
        public Duration getExpiryForUpdate() {
          return Duration.ZERO;
        }
      })
      .setTypes(Number.class, CharSequence.class));

  testCache.put(1, "one");
  testCache.get(1);

  testTimeSource.advanceTime(1000);

  Iterator<Cache.Entry<Number, CharSequence>> iterator = testCache.iterator();
  assertThat(iterator.hasNext(), is(false));

  cacheManager.close();
}
 
Example 9
Source File: CachingTest.java    From cache2k with Apache License 2.0 5 votes vote down vote up
@Test
public void getCacheManager_nullUriParameter() {
  CachingProvider provider = Caching.getCachingProvider();
  final URI NULL_URI = null;
  CacheManager manager = provider.getCacheManager(NULL_URI, provider.getDefaultClassLoader(), null);
  assertNotNull(manager);
  assertEquals(provider.getDefaultURI(), manager.getURI());
}
 
Example 10
Source File: JCacheIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void instantiateCache() {
    CachingProvider cachingProvider = Caching.getCachingProvider("com.hazelcast.cache.HazelcastCachingProvider");
    CacheManager cacheManager = cachingProvider.getCacheManager();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    Cache<String, String> cache = cacheManager.createCache("simpleCache", config);
    cache.put("key1", "value1");
    cache.put("key2", "value2");
    assertEquals("value1", cache.get("key1"));
    assertEquals("value2", cache.get("key2"));
    cacheManager.close();
}
 
Example 11
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 12
Source File: EntryProcessorIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void instantiateCache() {
    CachingProvider cachingProvider = Caching.getCachingProvider(CACHE_PROVIDER_NAME);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    MutableConfiguration<String, String> config = new MutableConfiguration<>();
    this.cache = cacheManager.createCache(CACHE_NAME, config);
    this.cache.put("key", "value");
}
 
Example 13
Source File: CachingProviderTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void createCacheMgr()
{
    final CachingProvider cachingProvider = Caching.getCachingProvider();
    assertNotNull(cachingProvider.getCacheManager());
    cachingProvider.close();
}
 
Example 14
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 15
Source File: IteratorTest.java    From blazingcache with Apache License 2.0 5 votes vote down vote up
@Test
public void testConditionalReplace() 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);
        long hitCount = 0;
        long missCount = 0;
        long putCount = 0;

        boolean result = cache.replace(1L, "MissingNoReplace", "NewValue");
        missCount++;
        assertFalse(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());
        assertFalse(cache.containsKey(1L));

        cache.put(1l, "Sooty");
        putCount++;
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());

        assertTrue(cache.containsKey(1L));
        result = cache.replace(1L, "Sooty", "Replaced");
        hitCount++;
        putCount++;
        assertTrue(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());

        result = cache.replace(1L, "Sooty", "InvalidReplace");
        hitCount++;
        assertFalse(result);
        assertEquals((int) missCount, (int) blazingCache.getStatisticsMXBean().getCacheMisses());
        assertEquals((int) hitCount, (int) blazingCache.getStatisticsMXBean().getCacheHits());
        assertEquals((int) putCount, (int) blazingCache.getStatisticsMXBean().getCachePuts());
    }
}
 
Example 16
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 17
Source File: LoaderWriterConfigTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  MockitoAnnotations.initMocks(this);
  cachingProvider = Caching.getCachingProvider();
}
 
Example 18
Source File: EhCache107ConfigurationIntegrationDocTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  cachingProvider = Caching.getCachingProvider();
  cacheManager = cachingProvider.getCacheManager();
}
 
Example 19
Source File: CacheManagerTest.java    From cache2k with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testIllegalURI1() throws Exception {
  CachingProvider p = Caching.getCachingProvider();
  CacheManager cm = p.getCacheManager(new URI("file://hello.xml"), null);
}
 
Example 20
Source File: ConfigStatsManagementActivationTest.java    From ehcache3 with Apache License 2.0 4 votes vote down vote up
@Before
public void setUp() {
  provider = Caching.getCachingProvider();
}