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