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"));
}
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);
}
}
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));
}
});
}
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));
}
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);
}
Aggregations