Search in sources :

Example 1 with CacheEntry

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();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheEntryCreatedListener(org.cache2k.event.CacheEntryCreatedListener) CacheRule(org.cache2k.test.util.CacheRule) CacheEntry(org.cache2k.CacheEntry) Cache(org.cache2k.Cache) Test(org.junit.Test)

Example 2 with CacheEntry

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());
}
Also used : ResiliencePolicy(org.cache2k.integration.ResiliencePolicy) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry) CacheEntry(org.cache2k.CacheEntry) IntCacheRule(org.cache2k.test.util.IntCacheRule) CacheRule(org.cache2k.test.util.CacheRule) NoSuchElementException(java.util.NoSuchElementException) EntryProcessingException(org.cache2k.processor.EntryProcessingException) CacheLoaderException(org.cache2k.integration.CacheLoaderException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) CacheLoaderException(org.cache2k.integration.CacheLoaderException) ExceptionInformation(org.cache2k.integration.ExceptionInformation) Test(org.junit.Test)

Example 3 with CacheEntry

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);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 4 with CacheEntry

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);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Example 5 with CacheEntry

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);
}
Also used : CacheEntry(org.cache2k.CacheEntry) ExpiryPolicy(org.cache2k.expiry.ExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) Test(org.junit.Test)

Aggregations

CacheEntry (org.cache2k.CacheEntry)18 Test (org.junit.Test)16 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 CacheRule (org.cache2k.test.util.CacheRule)9 Cache (org.cache2k.Cache)8 Condition (org.cache2k.test.util.Condition)8 IntCacheRule (org.cache2k.test.util.IntCacheRule)8 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)7 CacheEntryCreatedListener (org.cache2k.event.CacheEntryCreatedListener)4 CacheEntryUpdatedListener (org.cache2k.event.CacheEntryUpdatedListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 CacheEntryExpiredListener (org.cache2k.event.CacheEntryExpiredListener)2 CacheEntryRemovedListener (org.cache2k.event.CacheEntryRemovedListener)2 IOException (java.io.IOException)1 NoSuchElementException (java.util.NoSuchElementException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 CacheException (javax.cache.CacheException)1 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)1 Duration (javax.cache.expiry.Duration)1