Search in sources :

Example 6 with ExpiryPolicy

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

Example 7 with ExpiryPolicy

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

Aggregations

CacheEntry (org.cache2k.CacheEntry)7 ExpiryPolicy (org.cache2k.expiry.ExpiryPolicy)7 Test (org.junit.Test)7 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 CacheRule (org.cache2k.test.util.CacheRule)2 Condition (org.cache2k.test.util.Condition)2 IntCacheRule (org.cache2k.test.util.IntCacheRule)2