Search in sources :

Example 1 with CacheContext

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

the class CacheAddNearCacheInvalidationListenerTask method call.

@Override
protected Object call() {
    ClientEndpoint endpoint = getEndpoint();
    CacheService cacheService = getService(CacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext(parameters.name);
    NearCacheInvalidationListener listener = new NearCacheInvalidationListener(endpoint, cacheContext, nodeEngine.getLocalMember().getUuid());
    String registrationId = cacheService.addInvalidationListener(parameters.name, listener, parameters.localOnly);
    endpoint.addListenerDestroyAction(CacheService.SERVICE_NAME, parameters.name, registrationId);
    return registrationId;
}
Also used : ClientEndpoint(com.hazelcast.client.ClientEndpoint) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheService(com.hazelcast.cache.impl.CacheService)

Example 2 with CacheContext

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

the class Pre38CacheAddInvalidationListenerTask method call.

@Override
protected Object call() {
    ClientEndpoint endpoint = getEndpoint();
    CacheService cacheService = getService(CacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext(parameters.name);
    Pre38NearCacheInvalidationListener listener = new Pre38NearCacheInvalidationListener(endpoint, cacheContext, nodeEngine.getLocalMember().getUuid());
    String registrationId = cacheService.addInvalidationListener(parameters.name, listener, parameters.localOnly);
    endpoint.addListenerDestroyAction(CacheService.SERVICE_NAME, parameters.name, registrationId);
    return registrationId;
}
Also used : ClientEndpoint(com.hazelcast.client.ClientEndpoint) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheService(com.hazelcast.cache.impl.CacheService)

Example 3 with CacheContext

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

the class CacheContextTest method cacheEntryListenerCountIncreasedAndDecreasedCorrectly.

protected void cacheEntryListenerCountIncreasedAndDecreasedCorrectly(DecreaseType decreaseType) {
    CacheManager cacheManager = provider.getCacheManager();
    CacheEntryListenerConfiguration<String, String> cacheEntryListenerConfig = new MutableCacheEntryListenerConfiguration<String, String>(FactoryBuilder.factoryOf(new TestListener()), null, true, true);
    CompleteConfiguration<String, String> cacheConfig = new MutableConfiguration<String, String>();
    Cache<String, String> cache = cacheManager.createCache(CACHE_NAME, cacheConfig);
    cache.registerCacheEntryListener(cacheEntryListenerConfig);
    final CacheService cacheService1 = getCacheService(hazelcastInstance1);
    final CacheService cacheService2 = getCacheService(hazelcastInstance2);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotNull(cacheService1.getCacheContext(CACHE_NAME_WITH_PREFIX));
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotNull(cacheService2.getCacheContext(CACHE_NAME_WITH_PREFIX));
        }
    });
    final CacheContext cacheContext1 = cacheService1.getCacheContext(CACHE_NAME_WITH_PREFIX);
    final CacheContext cacheContext2 = cacheService2.getCacheContext(CACHE_NAME_WITH_PREFIX);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(1, cacheContext1.getCacheEntryListenerCount());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(1, cacheContext2.getCacheEntryListenerCount());
        }
    });
    switch(decreaseType) {
        case DEREGISTER:
            cache.deregisterCacheEntryListener(cacheEntryListenerConfig);
            break;
        case SHUTDOWN:
            driverInstance.getLifecycleService().shutdown();
            break;
        case TERMINATE:
            driverInstance.getLifecycleService().terminate();
            break;
        default:
            throw new IllegalArgumentException("Unsupported decrease type: " + decreaseType);
    }
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(0, cacheContext1.getCacheEntryListenerCount());
        }
    });
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(0, cacheContext2.getCacheEntryListenerCount());
        }
    });
}
Also used : MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CacheContext(com.hazelcast.cache.impl.CacheContext) MutableConfiguration(javax.cache.configuration.MutableConfiguration) CacheManager(javax.cache.CacheManager) AssertTask(com.hazelcast.test.AssertTask) CacheService(com.hazelcast.cache.impl.CacheService)

Example 4 with CacheContext

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

the class CacheAddNearCacheInvalidationListenerTask method processInternal.

@Override
protected CompletableFuture<UUID> processInternal() {
    CacheService cacheService = getService(CacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext(parameters.name);
    NearCacheInvalidationListener listener = new NearCacheInvalidationListener(endpoint, cacheContext, nodeEngine.getLocalMember().getUuid(), clientMessage.getCorrelationId());
    return parameters.localOnly ? newCompletedFuture(cacheService.registerLocalListener(parameters.name, listener)) : (CompletableFuture<UUID>) cacheService.registerListenerAsync(parameters.name, listener);
}
Also used : UUID(java.util.UUID) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheService(com.hazelcast.cache.impl.CacheService)

Example 5 with CacheContext

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

the class EvictionStrategyTest method evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy.

@Test
public void evictionPolicySuccessfullyEvaluatedOnSamplingBasedEvictionStrategy() {
    final int recordCount = 100;
    final int expectedEvictedRecordValue = recordCount / 2;
    Node node = getNode(instance);
    SerializationService serializationService = node.getSerializationService();
    ICacheService cacheService = node.getNodeEngine().getService(ICacheService.SERVICE_NAME);
    CacheContext cacheContext = cacheService.getOrCreateCacheContext("MyCache");
    SamplingEvictionStrategy<K, V, S> evictionStrategy = SamplingEvictionStrategy.INSTANCE;
    CacheRecordHashMap cacheRecordMap = new CacheRecordHashMap(serializationService, 1000, cacheContext);
    CacheObjectRecord expectedEvictedRecord = null;
    Data expectedData = null;
    for (int i = 0; i < recordCount; i++) {
        CacheObjectRecord record = new CacheObjectRecord(i, System.currentTimeMillis(), Long.MAX_VALUE);
        Data data = serializationService.toData(i);
        cacheRecordMap.put(data, record);
        if (i == expectedEvictedRecordValue) {
            expectedEvictedRecord = record;
            expectedData = data;
        }
    }
    assertNotNull(expectedEvictedRecord);
    assertNotNull(expectedData);
    final SimpleEvictionCandidate evictionCandidate = new SimpleEvictionCandidate((K) expectedData, (V) expectedEvictedRecord);
    // we are testing "EvictionStrategy" in this test, so we mock "EvictionPolicyEvaluator" (it's tested in another test)
    EvictionPolicyEvaluator evictionPolicyEvaluator = mock(EvictionPolicyEvaluator.class);
    when(evictionPolicyEvaluator.evaluate(Matchers.any(Iterable.class))).thenReturn(evictionCandidate);
    when(evictionPolicyEvaluator.getEvictionPolicyComparator()).thenReturn(null);
    assertEquals(recordCount, cacheRecordMap.size());
    assertTrue(cacheRecordMap.containsKey(expectedData));
    assertTrue(cacheRecordMap.containsValue(expectedEvictedRecord));
    boolean evicted = evictionStrategy.evict((S) cacheRecordMap, evictionPolicyEvaluator, EVICT_ALWAYS, NO_LISTENER);
    assertTrue(evicted);
    assertEquals(recordCount - 1, cacheRecordMap.size());
    assertFalse(cacheRecordMap.containsKey(expectedData));
    assertFalse(cacheRecordMap.containsValue(expectedEvictedRecord));
}
Also used : EvictionPolicyEvaluator(com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Node(com.hazelcast.instance.impl.Node) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) CacheObjectRecord(com.hazelcast.cache.impl.record.CacheObjectRecord) CacheContext(com.hazelcast.cache.impl.CacheContext) CacheRecordHashMap(com.hazelcast.cache.impl.record.CacheRecordHashMap) EVICT_ALWAYS(com.hazelcast.internal.eviction.EvictionChecker.EVICT_ALWAYS) ICacheService(com.hazelcast.cache.impl.ICacheService) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

CacheContext (com.hazelcast.cache.impl.CacheContext)5 CacheService (com.hazelcast.cache.impl.CacheService)4 ClientEndpoint (com.hazelcast.client.ClientEndpoint)2 ICacheService (com.hazelcast.cache.impl.ICacheService)1 CacheObjectRecord (com.hazelcast.cache.impl.record.CacheObjectRecord)1 CacheRecordHashMap (com.hazelcast.cache.impl.record.CacheRecordHashMap)1 Node (com.hazelcast.instance.impl.Node)1 EVICT_ALWAYS (com.hazelcast.internal.eviction.EvictionChecker.EVICT_ALWAYS)1 EvictionPolicyEvaluator (com.hazelcast.internal.eviction.impl.evaluator.EvictionPolicyEvaluator)1 Data (com.hazelcast.internal.serialization.Data)1 SerializationService (com.hazelcast.internal.serialization.SerializationService)1 Accessors.getNode (com.hazelcast.test.Accessors.getNode)1 AssertTask (com.hazelcast.test.AssertTask)1 QuickTest (com.hazelcast.test.annotation.QuickTest)1 UUID (java.util.UUID)1 CacheManager (javax.cache.CacheManager)1 MutableCacheEntryListenerConfiguration (javax.cache.configuration.MutableCacheEntryListenerConfiguration)1 MutableConfiguration (javax.cache.configuration.MutableConfiguration)1 Test (org.junit.Test)1