Search in sources :

Example 1 with MutableCacheEntry

use of org.cache2k.processor.MutableCacheEntry in project cache2k by cache2k.

the class EntryProcessorTest method initial_Not_Existing.

@Test
public void initial_Not_Existing() {
    Cache<Integer, Integer> c = target.cache();
    final AtomicBoolean _reached = new AtomicBoolean(false);
    final int _KEY = 123;
    EntryProcessor p = new EntryProcessor() {

        @Override
        public Object process(MutableCacheEntry e) throws Exception {
            assertFalse(e.exists());
            assertEquals(0, e.getLastModification());
            assertEquals(_KEY, e.getKey());
            _reached.set(true);
            return null;
        }
    };
    Object _result = c.invoke(_KEY, p);
    assertNull(_result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntryProcessor(org.cache2k.processor.EntryProcessor) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry) Test(org.junit.Test)

Example 2 with MutableCacheEntry

use of org.cache2k.processor.MutableCacheEntry 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 MutableCacheEntry

use of org.cache2k.processor.MutableCacheEntry in project cache2k by cache2k.

the class EntryProcessorTest method intial_noop.

/*
  Cache<Integer, Integer> cache;
  @Before public void setup() { cache = target.cache(); }
  */
@Test
public void intial_noop() {
    Cache<Integer, Integer> c = target.cache();
    EntryProcessor p = new EntryProcessor() {

        @Override
        public Object process(MutableCacheEntry e) throws Exception {
            return null;
        }
    };
    Object _result = c.invoke(123, p);
    assertNull(_result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessor(org.cache2k.processor.EntryProcessor) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry) Test(org.junit.Test)

Example 4 with MutableCacheEntry

use of org.cache2k.processor.MutableCacheEntry in project cache2k by cache2k.

the class EntryProcessorTest method initial_Return.

@Test
public void initial_Return() {
    Cache<Integer, Integer> c = target.cache();
    EntryProcessor p = new EntryProcessor() {

        @Override
        public Object process(MutableCacheEntry e) throws Exception {
            return "abc";
        }
    };
    Object _result = c.invoke(123, p);
    assertEquals("abc", _result);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) EntryProcessor(org.cache2k.processor.EntryProcessor) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry) Test(org.junit.Test)

Example 5 with MutableCacheEntry

use of org.cache2k.processor.MutableCacheEntry in project cache2k by cache2k.

the class TouchyJCacheAdapter method remove.

@Override
public boolean remove(final K key, final V oldValue) {
    checkClosed();
    checkNullValue(oldValue);
    if (key == null) {
        throw new NullPointerException();
    }
    EntryProcessor<K, V, Boolean> ep = new EntryProcessor<K, V, Boolean>() {

        @Override
        public Boolean process(final MutableCacheEntry<K, V> e) throws Exception {
            if (!e.exists()) {
                return false;
            }
            V _existingValue = e.getValue();
            if (_existingValue.equals(oldValue)) {
                e.remove();
                return true;
            }
            Duration d = expiryPolicy.getExpiryForAccess();
            if (d != null) {
                e.setExpiry(calculateExpiry(d));
            }
            return false;
        }
    };
    return c2kCache.invoke(key, ep);
}
Also used : EntryProcessor(org.cache2k.processor.EntryProcessor) Duration(javax.cache.expiry.Duration) MutableCacheEntry(org.cache2k.processor.MutableCacheEntry)

Aggregations

MutableCacheEntry (org.cache2k.processor.MutableCacheEntry)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 EntryProcessor (org.cache2k.processor.EntryProcessor)8 Test (org.junit.Test)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 NoSuchElementException (java.util.NoSuchElementException)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Duration (javax.cache.expiry.Duration)1 CacheEntry (org.cache2k.CacheEntry)1 CacheLoaderException (org.cache2k.integration.CacheLoaderException)1 ExceptionInformation (org.cache2k.integration.ExceptionInformation)1 ResiliencePolicy (org.cache2k.integration.ResiliencePolicy)1 EntryProcessingException (org.cache2k.processor.EntryProcessingException)1 CacheRule (org.cache2k.test.util.CacheRule)1 IntCacheRule (org.cache2k.test.util.IntCacheRule)1