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

The following examples show how to use javax.cache.Cache#unwrap() . 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: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsync() 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);

    CacheAsync<String, String> async = cache.unwrap(CacheAsync.class);
    async.putAsync("1", "2").get();
    assertThat(async.getAsync("1").get()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 2
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testReactive() 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);

    CacheReactive<String, String> reactive = cache.unwrap(CacheReactive.class);
    reactive.put("1", "2").block();
    assertThat(reactive.get("1").block()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 3
Source File: JCacheTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRx() 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);

    CacheRx<String, String> rx = cache.unwrap(CacheRx.class);
    rx.put("1", "2").blockingAwait();
    assertThat(rx.get("1").blockingGet()).isEqualTo("2");
    
    cache.close();
    runner.stop();
}
 
Example 4
Source File: JCacheManagerAdapter.java    From cache2k with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <K, V> Cache<K, V> getCache(String _cacheName) {
  checkClosed();
  checkNonNullCacheName(_cacheName);
  synchronized (getLockObject()) {
    Cache<K, V> c = name2adapter.get(_cacheName);
    if (c != null && manager.getCache(_cacheName) == c.unwrap(org.cache2k.Cache.class) && !c.isClosed()) {
      return c;
    }
    if (configuredCacheNames.contains(_cacheName)) {
      return createCache(_cacheName, new MutableConfiguration<K, V>());
    }
  }
  return null;
}
 
Example 5
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 6
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 7
Source File: JCacheManagerAdapter.java    From cache2k with Apache License 2.0 5 votes vote down vote up
public <K, V, C extends Configuration<K, V>> Cache<K, V> createCache(String _cacheName, C cfg)
  throws IllegalArgumentException {
  checkClosed();
  checkNonNullCacheName(_cacheName);
  synchronized (getLockObject()) {
    Cache _jsr107cache = name2adapter.get(_cacheName);
    if (_jsr107cache != null && !_jsr107cache.isClosed()) {
      throw new CacheException("cache already existing with name: " + _cacheName);
    }
    org.cache2k.Cache _existingCache = manager.getCache(_cacheName);
    if (_existingCache != null && !_existingCache.isClosed()) {
      throw new CacheException("A cache2k instance is already existing with name: " + _cacheName);
    }
    JCacheBuilder<K,V> _builder = new JCacheBuilder<K, V>(_cacheName, this);
    _builder.setConfiguration(cfg);
    Cache<K,V> _cache = _builder.build();
    org.cache2k.Cache _cache2k = _cache.unwrap(org.cache2k.Cache.class);
    Map<org.cache2k.Cache, Cache> _cloneC2k2jCache = new WeakHashMap<org.cache2k.Cache, Cache>(c2k2jCache);
    _cloneC2k2jCache.put(_cache2k, _cache);
    c2k2jCache = _cloneC2k2jCache;
    name2adapter.put(_cache.getName(), _cache);
    if (_builder.isStatisticsEnabled()) {
      enableStatistics(_cacheName, true);
    }
    if (_builder.isManagementEnabled()) {
      enableManagement(_cacheName, true);
    }
    return _cache;
  }
}
 
Example 8
Source File: InfinispanTest.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
private static FunctionalMap.ReadWriteMap<String, GridBucketState> toMap(Cache<String, GridBucketState> cache) {
    org.infinispan.Cache<String, GridBucketState> nativeCache = cache.unwrap(org.infinispan.Cache.class);
    FunctionalMapImpl<String, GridBucketState> functionalMap = FunctionalMapImpl.create(nativeCache.getAdvancedCache());
    return ReadWriteMapImpl.create(functionalMap);
}
 
Example 9
Source File: CacheUtils.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <K, V> ICache<K, V> getCache(HazelcastCacheManager cacheManager, String cacheName) {
    Cache<Object, Object> cache = cacheManager.getCache(cacheName);
    return cache.unwrap(ICache.class);
}
 
Example 10
Source File: CacheUtils.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
public static <K, V> ICache<K, V> getCache(HazelcastCacheManager cacheManager, String cacheName) {
    Cache<Object, Object> cache = cacheManager.getCache(cacheName);
    return cache.unwrap(ICache.class);
}