Search in sources :

Example 1 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class ClientCacheConfigTest method createCacheConfigOnAllNodes.

@Test
public void createCacheConfigOnAllNodes() {
    final String CACHE_NAME = "myCache";
    HazelcastInstance client = null;
    HazelcastInstance server1 = null;
    HazelcastInstance server2 = null;
    try {
        Config config = new Config();
        CacheSimpleConfig cacheSimpleConfig = new CacheSimpleConfig().setName(CACHE_NAME).setBackupCount(// Be sure that cache put operation is mirrored to backup node
        1);
        config.addCacheConfig(cacheSimpleConfig);
        // Create servers with configured caches
        server1 = Hazelcast.newHazelcastInstance(config);
        server2 = Hazelcast.newHazelcastInstance(config);
        ICacheService cacheService1 = getCacheService(server1);
        ICacheService cacheService2 = getCacheService(server2);
        // Create the hazelcast client instance
        client = HazelcastClient.newHazelcastClient();
        // Create the client cache manager
        CachingProvider cachingProvider = HazelcastClientCachingProvider.createCachingProvider(client);
        CacheManager cacheManager = cachingProvider.getCacheManager();
        Cache<String, String> cache = cacheManager.getCache(CACHE_NAME);
        assertNotNull(cache);
        CacheConfig cacheConfig = cache.getConfiguration(CacheConfig.class);
        assertNotNull(cacheService1.getCacheConfig(cacheConfig.getNameWithPrefix()));
        assertNotNull(cacheService2.getCacheConfig(cacheConfig.getNameWithPrefix()));
        // First attempt to use the cache will trigger to create its record store.
        // So, we are testing also this case. There should not be any exception.
        // In here, we are testing both of nodes since there is a backup,
        // put is also applied to other (backup node).
        cache.put("key", "value");
    } finally {
        if (client != null) {
            client.shutdown();
        }
        if (server1 != null) {
            server1.shutdown();
        }
        if (server2 != null) {
            server2.shutdown();
        }
    }
}
Also used : CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) CacheConfig(com.hazelcast.config.CacheConfig) CacheManager(javax.cache.CacheManager) CacheConfig(com.hazelcast.config.CacheConfig) HazelcastCachingProvider(com.hazelcast.cache.HazelcastCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) HazelcastClientCachingProvider(com.hazelcast.client.cache.impl.HazelcastClientCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 2 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheSizeOperation method run.

@Override
public void run() throws Exception {
    ICacheService service = getService();
    ICacheRecordStore cache = service.getRecordStore(name, getPartitionId());
    response = cache != null ? cache.size() : 0;
}
Also used : ICacheService(com.hazelcast.cache.impl.ICacheService) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore)

Example 3 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CacheNearCacheStateHolder method prepare.

void prepare(CachePartitionSegment segment) {
    ICacheService cacheService = segment.getCacheService();
    MetaDataGenerator metaData = getPartitionMetaDataGenerator(cacheService);
    int partitionId = segment.getPartitionId();
    partitionUuid = metaData.getUuidOrNull(partitionId);
    cacheNameSequencePairs = new ArrayList(segment.getCacheConfigs().size());
    Iterator<ICacheRecordStore> iter = segment.recordStoreIterator();
    while (iter.hasNext()) {
        ICacheRecordStore cacheRecordStore = iter.next();
        String cacheName = cacheRecordStore.getName();
        cacheNameSequencePairs.add(cacheName);
        cacheNameSequencePairs.add(metaData.currentSequence(cacheName, partitionId));
    }
}
Also used : ICacheService(com.hazelcast.cache.impl.ICacheService) ArrayList(java.util.ArrayList) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore) MetaDataGenerator(com.hazelcast.internal.nearcache.impl.invalidation.MetaDataGenerator)

Example 4 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CachePutAllOperation method run.

@Override
public void run() throws Exception {
    int partitionId = getPartitionId();
    String callerUuid = getCallerUuid();
    ICacheService service = getService();
    cache = service.getOrCreateRecordStore(name, partitionId);
    backupRecords = new HashMap<Data, CacheRecord>(entries.size());
    for (Map.Entry<Data, Data> entry : entries) {
        Data key = entry.getKey();
        Data value = entry.getValue();
        CacheRecord backupRecord = cache.put(key, value, expiryPolicy, callerUuid, completionId);
        backupRecords.put(key, backupRecord);
    }
}
Also used : CacheRecord(com.hazelcast.cache.impl.record.CacheRecord) ICacheService(com.hazelcast.cache.impl.ICacheService) Data(com.hazelcast.nio.serialization.Data) HashMap(java.util.HashMap) AbstractMap(java.util.AbstractMap) Map(java.util.Map)

Example 5 with ICacheService

use of com.hazelcast.cache.impl.ICacheService in project hazelcast by hazelcast.

the class CachePutBackupOperation method runInternal.

@Override
public void runInternal() throws Exception {
    ICacheService service = getService();
    ICacheRecordStore cache = service.getOrCreateRecordStore(name, getPartitionId());
    cache.putRecord(key, cacheRecord);
    response = Boolean.TRUE;
}
Also used : ICacheService(com.hazelcast.cache.impl.ICacheService) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore)

Aggregations

ICacheService (com.hazelcast.cache.impl.ICacheService)24 ICacheRecordStore (com.hazelcast.cache.impl.ICacheRecordStore)9 CacheConfig (com.hazelcast.config.CacheConfig)8 Data (com.hazelcast.nio.serialization.Data)8 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Test (org.junit.Test)7 Node (com.hazelcast.instance.Node)5 SerializationService (com.hazelcast.spi.serialization.SerializationService)5 HashMap (java.util.HashMap)5 Map (java.util.Map)5 InternalPartitionService (com.hazelcast.internal.partition.InternalPartitionService)4 ParallelTest (com.hazelcast.test.annotation.ParallelTest)4 CacheRecord (com.hazelcast.cache.impl.record.CacheRecord)3 HazelcastInstance (com.hazelcast.core.HazelcastInstance)3 AssertTask (com.hazelcast.test.AssertTask)3 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)2 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)2 Config (com.hazelcast.config.Config)2 NodeEngine (com.hazelcast.spi.NodeEngine)2 HashSet (java.util.HashSet)2