use of org.cache2k.expiry.ExpiryPolicy in project cache2k by cache2k.
the class ListenerTest method asyncHalfExpiredAfterUpdate.
/**
* Expire time is load time if entry is modified, yields: Expiry listener is called. Entry
* stays in the cache, we need to implement the removal
*/
@Test
public void asyncHalfExpiredAfterUpdate() {
final AtomicInteger _expireCallCount = new AtomicInteger();
final Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.addAsyncListener(new CacheEntryExpiredListener<Integer, Integer>() {
@Override
public void onEntryExpired(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
_expireCallCount.incrementAndGet();
}
}).eternal(true).keepDataAfterExpired(false).expiryPolicy(new ExpiryPolicy<Integer, Integer>() {
@Override
public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) {
if (oldEntry != null) {
return loadTime;
}
return ETERNAL;
}
});
}
});
c.put(1, 1);
c.put(1, 2);
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return _expireCallCount.get() == 1;
}
});
assertEquals(0, latestInfo(c).getSize());
assertEquals(1, latestInfo(c).getExpiredCount());
}
use of org.cache2k.expiry.ExpiryPolicy in project cache2k by cache2k.
the class ListenerTest method asyncReallyExpiredAfterUpdate.
/**
* Expire time is 0 if entry is modified, yields: Expiry listener is called and entry
* is removed from cache.
*/
@Test
public void asyncReallyExpiredAfterUpdate() {
final AtomicInteger _expireCallCount = new AtomicInteger();
final Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.addAsyncListener(new CacheEntryExpiredListener<Integer, Integer>() {
@Override
public void onEntryExpired(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
_expireCallCount.incrementAndGet();
}
}).eternal(true).keepDataAfterExpired(false).expiryPolicy(new ExpiryPolicy<Integer, Integer>() {
@Override
public long calculateExpiryTime(final Integer key, final Integer value, final long loadTime, final CacheEntry<Integer, Integer> oldEntry) {
if (oldEntry != null) {
return 0;
}
return ETERNAL;
}
});
}
});
c.put(1, 1);
c.put(1, 2);
ConcurrencyHelper.await(new Condition() {
@Override
public boolean check() throws Exception {
return _expireCallCount.get() == 1;
}
});
assertEquals(0, latestInfo(c).getSize());
}
Aggregations