use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class CacheLoaderWiredCacheTest method testLoaderWithListener.
@Test
public void testLoaderWithListener() {
final AtomicInteger _countCreated = new AtomicInteger();
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.loader(new CacheLoader<Integer, Integer>() {
@Override
public Integer load(final Integer key) throws Exception {
return key * 2;
}
}).addListener(new CacheEntryCreatedListener<Integer, Integer>() {
@Override
public void onEntryCreated(final Cache<Integer, Integer> c, final CacheEntry<Integer, Integer> e) {
_countCreated.incrementAndGet();
}
});
}
});
assertEquals(0, _countCreated.get());
assertEquals((Integer) 10, c.get(5));
assertEquals(1, _countCreated.get());
assertEquals((Integer) 20, c.get(10));
assertFalse(c.containsKey(2));
assertTrue(c.containsKey(5));
c.close();
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class EntryProcessorTest method setException_policy_called.
@Test
public void setException_policy_called() {
final String _TEXT = "set inside process";
final AtomicLong _retryLoadAfter = new AtomicLong();
final ResiliencePolicy<Integer, Integer> _policy = new ResiliencePolicy<Integer, Integer>() {
@Override
public long suppressExceptionUntil(final Integer key, final ExceptionInformation exceptionInformation, final CacheEntry<Integer, Integer> cachedContent) {
return 0;
}
@Override
public long retryLoadAfter(final Integer key, final ExceptionInformation exceptionInformation) {
_retryLoadAfter.incrementAndGet();
return ETERNAL;
}
};
Cache<Integer, Integer> c = target.cache(new CacheRule.Specialization<Integer, Integer>() {
@Override
public void extend(final Cache2kBuilder<Integer, Integer> b) {
b.resiliencePolicy(_policy);
}
});
c.invoke(KEY, new EntryProcessor<Integer, Integer, Object>() {
@Override
public Object process(final MutableCacheEntry<Integer, Integer> e) throws Exception {
e.setException(new IllegalStateException(_TEXT));
return null;
}
});
try {
c.get(KEY);
fail();
} catch (CacheLoaderException ex) {
assertTrue(ex.getCause().toString().contains(_TEXT));
}
assertEquals(1, _retryLoadAfter.get());
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class TimingHandlerTest method expireAfterWrite_policy_limit_nonSharp.
@Test
public void expireAfterWrite_policy_limit_nonSharp() {
long _DURATION = 1000000;
final long _POINT_IN_TIME = NOW + 5000000;
TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {
@Override
public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
return _POINT_IN_TIME;
}
}).expireAfterWrite(_DURATION, TimeUnit.MILLISECONDS).toConfiguration());
Entry e = new Entry();
long t = h.calculateNextRefreshTime(e, null, NOW);
assertNotEquals(Long.MAX_VALUE, t);
assertEquals("max expiry, but not sharp", NOW + _DURATION, t);
long _later = NOW + _DURATION;
t = h.calculateNextRefreshTime(e, null, _later);
assertEquals(_later + _DURATION, t);
_later = _POINT_IN_TIME - _DURATION / 2;
t = h.calculateNextRefreshTime(e, null, _later);
assertEquals("requested expiry via duration too close", _POINT_IN_TIME, t);
_later = _POINT_IN_TIME - _DURATION - 1;
t = h.calculateNextRefreshTime(e, null, _later);
assertTrue(t <= _later + _DURATION);
assertEquals(_later + _DURATION, t);
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class TimingHandlerTest method policy_sharp.
/**
* Sharp is honored in the calculation phase.
*/
@Test
public void policy_sharp() {
TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {
@Override
public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
return loadTime + 1;
}
}).sharpExpiry(true).toConfiguration());
Entry e = new Entry();
long t = h.calculateNextRefreshTime(e, null, NOW);
assertEquals(-NOW - 1, t);
}
use of org.cache2k.CacheEntry in project cache2k by cache2k.
the class TimingHandlerTest method expireAfterWrite_policy_limit_sharp_close.
/**
* Maximum expiry is limited when sharp expiry requested.
* Corner case if expiry will happen close to requested point in time.
*/
@Test
public void expireAfterWrite_policy_limit_sharp_close() {
long _DURATION = 100;
final long _SHARP_POINT_IN_TIME = NOW + 5000;
TimingHandler h = TimingHandler.of(CLOCK, Cache2kBuilder.forUnknownTypes().expiryPolicy(new ExpiryPolicy() {
@Override
public long calculateExpiryTime(Object key, Object value, long loadTime, CacheEntry oldEntry) {
return -_SHARP_POINT_IN_TIME;
}
}).expireAfterWrite(_DURATION, TimeUnit.MILLISECONDS).toConfiguration());
Entry e = new Entry();
long _later = _SHARP_POINT_IN_TIME - _DURATION - 1;
long t = h.calculateNextRefreshTime(e, null, _later);
assertTrue("expect gap bigger then duration", HeapCache.TUNABLE.sharpExpirySafetyGapMillis > _DURATION);
assertNotEquals(_SHARP_POINT_IN_TIME - HeapCache.TUNABLE.sharpExpirySafetyGapMillis, t);
assertTrue(Math.abs(t) > NOW);
assertTrue(Math.abs(t) < _SHARP_POINT_IN_TIME);
}
Aggregations