use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class ListenerTest method manyAsyncUpdateListenerCalled.
/**
* Check that we do not miss events.
*/
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void manyAsyncUpdateListenerCalled() {
final AtomicInteger _callCount = new AtomicInteger();
final ConcurrentMap<Integer, Integer> _seenValues = new ConcurrentHashMap<Integer, Integer>();
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
_seenValues.put(entryWithNewData.getValue(), entryWithNewData.getValue());
_callCount.incrementAndGet();
}
});
}
});
c.put(1, 2);
assertEquals(0, _callCount.get());
final int _UPDATE_COUNT = 123;
for (int i = 0; i < _UPDATE_COUNT; i++) {
c.put(1, i);
}
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return _callCount.get() == _UPDATE_COUNT;
}
});
assertEquals("Event dispatching is using copied events", 123, _seenValues.size());
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class ListenerTest method asyncUpdateListenerCalled.
/**
* If the listener is not executed in separate thread, this would block
*/
@Test(timeout = TestingParameters.MAX_FINISH_WAIT_MILLIS)
public void asyncUpdateListenerCalled() {
final AtomicInteger _callCount = new AtomicInteger();
final CountDownLatch _fire = new CountDownLatch(1);
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.addAsyncListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onEntryUpdated(final Cache<Integer, Integer> cache, final CacheEntry<Integer, Integer> currentEntry, final CacheEntry<Integer, Integer> entryWithNewData) {
try {
_fire.await();
} catch (InterruptedException ignore) {
}
_callCount.incrementAndGet();
}
});
}
});
c.put(1, 2);
assertEquals(0, _callCount.get());
c.put(1, 2);
assertEquals(0, _callCount.get());
_fire.countDown();
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return _callCount.get() == 1;
}
});
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class EntryAction method callListeners.
public void callListeners() {
if (!mightHaveListeners()) {
mutationReleaseLockAndStartTimer();
return;
}
CacheEntry<K, V> _currentEntry = heapCache.returnEntry(entry);
if (expiredImmediately) {
if (storageDataValid || heapDataValid) {
if (entryExpiredListeners() != null) {
for (CacheEntryExpiredListener l : entryExpiredListeners()) {
try {
l.onEntryExpired(userCache, _currentEntry);
} catch (Throwable t) {
exceptionToPropagate = new ListenerException(t);
}
}
}
}
} else if (remove) {
if (storageDataValid || heapDataValid) {
if (entryRemovedListeners() != null) {
for (CacheEntryRemovedListener l : entryRemovedListeners()) {
try {
l.onEntryRemoved(userCache, _currentEntry);
} catch (Throwable t) {
exceptionToPropagate = new ListenerException(t);
}
}
}
}
} else {
if (storageDataValid || heapDataValid) {
if (entryUpdatedListeners() != null) {
CacheEntry<K, V> _previousEntry = heapCache.returnCacheEntry(entry.getKey(), oldValueOrException, previousModificationTime);
for (CacheEntryUpdatedListener l : entryUpdatedListeners()) {
try {
l.onEntryUpdated(userCache, _previousEntry, _currentEntry);
} catch (Throwable t) {
exceptionToPropagate = new ListenerException(t);
}
}
}
} else {
if (entryCreatedListeners() != null) {
for (CacheEntryCreatedListener l : entryCreatedListeners()) {
try {
l.onEntryCreated(userCache, _currentEntry);
} catch (Throwable t) {
exceptionToPropagate = new ListenerException(t);
}
}
}
}
}
mutationReleaseLockAndStartTimer();
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class JCacheBuilder method setupExpiryPolicy.
/**
* Register a expiry policy to cache2k.
*
* <p>JSR107 requires that null values are deleted from the cache. We register an expiry policy
* to cache2k to provide this behavior.
*/
private void setupExpiryPolicy() {
if (cache2kConfiguration.getExpiryPolicy() != null) {
org.cache2k.expiry.ExpiryPolicy<K, V> ep0;
try {
ep0 = cache2kConfiguration.getExpiryPolicy().supply(manager.getCache2kManager());
} catch (Exception ex) {
throw new CacheException("couldn't initialize expiry policy", ex);
}
final org.cache2k.expiry.ExpiryPolicy<K, V> ep = ep0;
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
if (value == null) {
return NO_CACHE;
}
return ep.calculateExpiryTime(key, value, loadTime, oldEntry);
}
}));
return;
}
if (config.getExpiryPolicyFactory() != null) {
expiryPolicy = config.getExpiryPolicyFactory().create();
}
if (expiryPolicy == null || expiryPolicy instanceof EternalExpiryPolicy) {
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
if (value == null) {
return NO_CACHE;
}
return ETERNAL;
}
}));
return;
}
if (expiryPolicy instanceof ModifiedExpiryPolicy) {
Duration d = expiryPolicy.getExpiryForCreation();
final long _millisDuration = d.getTimeUnit().toMillis(d.getDurationAmount());
if (_millisDuration == 0) {
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
return NO_CACHE;
}
}));
return;
}
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
if (value == null) {
return NO_CACHE;
}
return loadTime + _millisDuration;
}
}));
return;
}
if (expiryPolicy instanceof CreatedExpiryPolicy) {
cache2kConfiguration.setEternal(true);
Duration d = expiryPolicy.getExpiryForCreation();
final long _millisDuration = d.getTimeUnit().toMillis(d.getDurationAmount());
if (_millisDuration == 0) {
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
return NO_CACHE;
}
}));
return;
}
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {
@Override
public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
if (value == null) {
return NO_CACHE;
}
if (oldEntry == null) {
return loadTime + _millisDuration;
} else {
return NEUTRAL;
}
}
}));
return;
}
needsTouchyWrapper = true;
cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new TouchyJCacheAdapter.ExpiryPolicyAdapter<K, V>(expiryPolicy)));
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class OsgiIT method testEventPackage.
/**
* Simple test to see whether event package is exported.
*/
@Test
public void testEventPackage() {
CacheManager m = CacheManager.getInstance("testEventPackage");
final AtomicInteger _count = new AtomicInteger();
Cache<String, String> c = new Cache2kBuilder<String, String>() {
}.manager(m).eternal(true).addListener(new CacheEntryCreatedListener<String, String>() {
@Override
public void onEntryCreated(final Cache<String, String> cache, final CacheEntry<String, String> entry) {
_count.incrementAndGet();
}
}).build();
c.put("abc", "123");
assertTrue(c.containsKey("abc"));
assertEquals("123", c.peek("abc"));
assertEquals(1, _count.get());
c.close();
}
Aggregations