Search in sources :

Example 1 with Cache

use of javax.cache.Cache in project hibernate-orm by hibernate.

the class JCacheRegionTest method testSupportsToMap.

@Test
public void testSupportsToMap() {
    final Cache<Object, Object> cache = region.getCache();
    final Iterator mock = Mockito.mock(Iterator.class);
    when(mock.hasNext()).thenReturn(true).thenReturn(false);
    when(mock.next()).thenReturn(new Cache.Entry<Object, Object>() {

        @Override
        public Object getKey() {
            return "foo";
        }

        @Override
        public Object getValue() {
            return "bar";
        }

        @Override
        public <T> T unwrap(Class<T> clazz) {
            throw new UnsupportedOperationException("Implement me!");
        }
    });
    when(cache.iterator()).thenReturn(mock);
    final Map<String, String> map = region.toMap();
    assertThat(map.size(), is(1));
    assertThat(map.get("foo"), equalTo("bar"));
}
Also used : Iterator(java.util.Iterator) Cache(javax.cache.Cache) Test(org.junit.Test)

Example 2 with Cache

use of javax.cache.Cache in project pratilipi by Pratilipi.

the class MemcacheGaeImpl method flush.

@Override
public void flush() {
    try {
        Cache cache = CacheManager.getInstance().getCacheFactory().createCache(Collections.emptyMap());
        cache.clear();
    } catch (CacheException ex) {
        logger.log(Level.SEVERE, "Failed to create cache instance.", ex);
    }
}
Also used : GCacheException(com.google.appengine.api.memcache.stdimpl.GCacheException) CacheException(javax.cache.CacheException) Cache(javax.cache.Cache)

Example 3 with Cache

use of javax.cache.Cache in project ignite by apache.

the class CacheUtils method foreach.

/**
     * @param cacheName Cache name.
     * @param fun An operation that accepts a cache entry and processes it.
     * @param keyFilter Cache keys filter.
     * @param <K> Cache key object type.
     * @param <V> Cache value object type.
     */
public static <K, V> void foreach(String cacheName, IgniteConsumer<CacheEntry<K, V>> fun, IgnitePredicate<K> keyFilter) {
    bcast(cacheName, () -> {
        Ignite ignite = Ignition.localIgnite();
        IgniteCache<K, V> cache = ignite.getOrCreateCache(cacheName);
        int partsCnt = ignite.affinity(cacheName).partitions();
        // Use affinity in filter for scan query. Otherwise we accept consumer in each node which is wrong.
        Affinity affinity = ignite.affinity(cacheName);
        ClusterNode locNode = ignite.cluster().localNode();
        // Iterate over all partitions. Some of them will be stored on that local node.
        for (int part = 0; part < partsCnt; part++) {
            int p = part;
            // Query returns an empty cursor if this partition is not stored on this node.
            for (Cache.Entry<K, V> entry : cache.query(new ScanQuery<K, V>(part, (k, v) -> affinity.mapPartitionToNode(p) == locNode && (keyFilter == null || keyFilter.apply(k))))) fun.accept(new CacheEntry<>(entry, cache));
        }
    });
}
Also used : ClusterNode(org.apache.ignite.cluster.ClusterNode) IgniteConsumer(org.apache.ignite.ml.math.functions.IgniteConsumer) IgniteFunction(org.apache.ignite.ml.math.functions.IgniteFunction) Affinity(org.apache.ignite.cache.affinity.Affinity) SparseDistributedMatrix(org.apache.ignite.ml.math.impls.matrix.SparseDistributedMatrix) SparseDistributedMatrixStorage(org.apache.ignite.ml.math.impls.storage.matrix.SparseDistributedMatrixStorage) IgniteCallable(org.apache.ignite.lang.IgniteCallable) ClusterNode(org.apache.ignite.cluster.ClusterNode) IgnitePredicate(org.apache.ignite.lang.IgnitePredicate) ValueMapper(org.apache.ignite.ml.math.ValueMapper) Map(java.util.Map) Cache(javax.cache.Cache) ClusterGroup(org.apache.ignite.cluster.ClusterGroup) KeyMapper(org.apache.ignite.ml.math.KeyMapper) CacheEntryImpl(org.apache.ignite.internal.processors.cache.CacheEntryImpl) Collection(java.util.Collection) IgniteRunnable(org.apache.ignite.lang.IgniteRunnable) Ignite(org.apache.ignite.Ignite) BinaryOperator(java.util.function.BinaryOperator) IgniteCache(org.apache.ignite.IgniteCache) IgniteBiTuple(org.apache.ignite.lang.IgniteBiTuple) Ignition(org.apache.ignite.Ignition) IgniteBiFunction(org.apache.ignite.ml.math.functions.IgniteBiFunction) Collections(java.util.Collections) ScanQuery(org.apache.ignite.cache.query.ScanQuery) IgniteUuid(org.apache.ignite.lang.IgniteUuid) Affinity(org.apache.ignite.cache.affinity.Affinity) Ignite(org.apache.ignite.Ignite) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 4 with Cache

use of javax.cache.Cache in project ignite by apache.

the class IgniteDbPutGetAbstractTest method testPutGetLarge.

/**
     * @throws Exception if failed.
     */
public void testPutGetLarge() throws Exception {
    IgniteEx ig = grid(0);
    IgniteCache<Integer, byte[]> cache = ig.cache(DEFAULT_CACHE_NAME);
    final byte[] val = new byte[2048];
    ThreadLocalRandom.current().nextBytes(val);
    cache.put(0, val);
    Assert.assertArrayEquals(val, cache.get(0));
    final IgniteCache<Integer, LargeDbValue> cache1 = ig.cache("large");
    final LargeDbValue large = new LargeDbValue("str1", "str2", randomInts(1024));
    cache1.put(1, large);
    assertEquals(large, cache1.get(1));
    if (indexingEnabled()) {
        final List<Cache.Entry<Integer, LargeDbValue>> all = cache1.query(new SqlQuery<Integer, LargeDbValue>(LargeDbValue.class, "str1='str1'")).getAll();
        assertEquals(1, all.size());
        final Cache.Entry<Integer, LargeDbValue> entry = all.get(0);
        assertEquals(1, entry.getKey().intValue());
        assertEquals(large, entry.getValue());
    }
    cache.remove(0);
    cache1.remove(1);
    assertNull(cache.get(0));
    assertNull(cache1.get(1));
}
Also used : SqlQuery(org.apache.ignite.cache.query.SqlQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) Cache(javax.cache.Cache) IgniteCache(org.apache.ignite.IgniteCache)

Example 5 with Cache

use of javax.cache.Cache in project hazelcast by hazelcast.

the class BaseCacheEvictionPolicyComparatorTest method do_test_evictionPolicyComparator.

void do_test_evictionPolicyComparator(EvictionConfig evictionConfig, int iterationCount) {
    HazelcastInstance instance = createInstance(createConfig());
    CachingProvider cachingProvider = createCachingProvider(instance);
    CacheManager cacheManager = cachingProvider.getCacheManager();
    CacheConfig cacheConfig = createCacheConfig(CACHE_NAME);
    cacheConfig.setEvictionConfig(evictionConfig);
    Cache cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
    for (int i = 0; i < iterationCount; i++) {
        cache.put(i, "Value-" + i);
    }
    AtomicLong callCounter = (AtomicLong) getUserContext(instance).get("callCounter");
    assertTrue(callCounter.get() > 0);
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) CachingProvider(javax.cache.spi.CachingProvider) Cache(javax.cache.Cache)

Aggregations

Cache (javax.cache.Cache)332 Test (org.junit.Test)167 IgniteCache (org.apache.ignite.IgniteCache)157 Ignite (org.apache.ignite.Ignite)101 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)68 List (java.util.List)63 Map (java.util.Map)59 ArrayList (java.util.ArrayList)54 ScanQuery (org.apache.ignite.cache.query.ScanQuery)54 HashMap (java.util.HashMap)52 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)45 QueryCursor (org.apache.ignite.cache.query.QueryCursor)43 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)41 HashSet (java.util.HashSet)40 CacheManager (javax.cache.CacheManager)40 Collection (java.util.Collection)38 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)37 CacheException (javax.cache.CacheException)35 IgniteEx (org.apache.ignite.internal.IgniteEx)35 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)32