Search in sources :

Example 6 with Duration

use of javax.cache.expiry.Duration in project cache2k by cache2k.

the class JCacheBuilder method setupExpiryPolicy.

/**
 * Register a expiry policy to cache2k.
 *
 * <p>JSR107 requires that null values are deleted from the cache. We register an expiry policy
 * to cache2k to provide this behavior.
 */
private void setupExpiryPolicy() {
    if (cache2kConfiguration.getExpiryPolicy() != null) {
        org.cache2k.expiry.ExpiryPolicy<K, V> ep0;
        try {
            ep0 = cache2kConfiguration.getExpiryPolicy().supply(manager.getCache2kManager());
        } catch (Exception ex) {
            throw new CacheException("couldn't initialize expiry policy", ex);
        }
        final org.cache2k.expiry.ExpiryPolicy<K, V> ep = ep0;
        cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

            @Override
            public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                if (value == null) {
                    return NO_CACHE;
                }
                return ep.calculateExpiryTime(key, value, loadTime, oldEntry);
            }
        }));
        return;
    }
    if (config.getExpiryPolicyFactory() != null) {
        expiryPolicy = config.getExpiryPolicyFactory().create();
    }
    if (expiryPolicy == null || expiryPolicy instanceof EternalExpiryPolicy) {
        cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

            @Override
            public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                if (value == null) {
                    return NO_CACHE;
                }
                return ETERNAL;
            }
        }));
        return;
    }
    if (expiryPolicy instanceof ModifiedExpiryPolicy) {
        Duration d = expiryPolicy.getExpiryForCreation();
        final long _millisDuration = d.getTimeUnit().toMillis(d.getDurationAmount());
        if (_millisDuration == 0) {
            cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

                @Override
                public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                    return NO_CACHE;
                }
            }));
            return;
        }
        cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

            @Override
            public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                if (value == null) {
                    return NO_CACHE;
                }
                return loadTime + _millisDuration;
            }
        }));
        return;
    }
    if (expiryPolicy instanceof CreatedExpiryPolicy) {
        cache2kConfiguration.setEternal(true);
        Duration d = expiryPolicy.getExpiryForCreation();
        final long _millisDuration = d.getTimeUnit().toMillis(d.getDurationAmount());
        if (_millisDuration == 0) {
            cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

                @Override
                public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                    return NO_CACHE;
                }
            }));
            return;
        }
        cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new org.cache2k.expiry.ExpiryPolicy<K, V>() {

            @Override
            public long calculateExpiryTime(final K key, final V value, final long loadTime, final CacheEntry<K, V> oldEntry) {
                if (value == null) {
                    return NO_CACHE;
                }
                if (oldEntry == null) {
                    return loadTime + _millisDuration;
                } else {
                    return NEUTRAL;
                }
            }
        }));
        return;
    }
    needsTouchyWrapper = true;
    cache2kConfiguration.setExpiryPolicy(new CustomizationReferenceSupplier<org.cache2k.expiry.ExpiryPolicy<K, V>>(new TouchyJCacheAdapter.ExpiryPolicyAdapter<K, V>(expiryPolicy)));
}
Also used : CacheException(javax.cache.CacheException) EternalExpiryPolicy(javax.cache.expiry.EternalExpiryPolicy) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CacheEntry(org.cache2k.CacheEntry) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) IOException(java.io.IOException) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) EternalExpiryPolicy(javax.cache.expiry.EternalExpiryPolicy) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 7 with Duration

use of javax.cache.expiry.Duration in project cache2k by cache2k.

the class CacheExpiryTest method testCacheStatisticsRemoveAll.

@Test
public void testCacheStatisticsRemoveAll() throws Exception {
    long _EXPIRY_MILLIS = 3;
    // cannot be zero or will not be added to the cache
    ExpiryPolicy policy = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, _EXPIRY_MILLIS));
    expiryPolicyServer.setExpiryPolicy(policy);
    MutableConfiguration<Integer, Integer> config = new MutableConfiguration<>();
    config.setExpiryPolicyFactory(FactoryBuilder.factoryOf(expiryPolicyClient)).setStatisticsEnabled(true);
    Cache<Integer, Integer> cache = getCacheManager().createCache(getTestCacheName(), config);
    for (int i = 0; i < 100; i++) {
        cache.put(i, i + 100);
    }
    // should work with all implementations
    Thread.sleep(_EXPIRY_MILLIS);
    cache.removeAll();
    assertEquals(100L, lookupManagementAttribute(cache, CacheStatistics, "CachePuts"));
    // Removals does not count expired entries
    assertEquals(0L, lookupManagementAttribute(cache, CacheStatistics, "CacheRemovals"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) MutableConfiguration(javax.cache.configuration.MutableConfiguration) Test(org.junit.Test)

Example 8 with Duration

use of javax.cache.expiry.Duration in project cache2k by cache2k.

the class CacheExpiryTest method nanosecondsInvalidDuration.

@Test(expected = IllegalArgumentException.class)
public void nanosecondsInvalidDuration() {
    Duration invalidDuration = new Duration(TimeUnit.NANOSECONDS, 0);
    assertTrue("expected IllegalArgumentException for TimeUnit below minimum of MILLISECONDS", invalidDuration == null);
}
Also used : Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 9 with Duration

use of javax.cache.expiry.Duration in project cache2k by cache2k.

the class CacheExpiryTest method microsecondsInvalidDuration.

/**
 * Assert "The minimum allowed TimeUnit is TimeUnit.MILLISECONDS.
 */
@Test(expected = IllegalArgumentException.class)
public void microsecondsInvalidDuration() {
    Duration invalidDuration = new Duration(TimeUnit.MICROSECONDS, 0);
    assertTrue("expected IllegalArgumentException for TimeUnit below minimum of MILLISECONDS", invalidDuration == null);
}
Also used : Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 10 with Duration

use of javax.cache.expiry.Duration 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

Duration (javax.cache.expiry.Duration)119 Test (org.junit.Test)49 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)36 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)31 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)31 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)19 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)15 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)13 ModifiedExpiryPolicy (javax.cache.expiry.ModifiedExpiryPolicy)12 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)12 Ignite (org.apache.ignite.Ignite)11 Transaction (org.apache.ignite.transactions.Transaction)11 CacheLoaderException (javax.cache.integration.CacheLoaderException)10 ArrayList (java.util.ArrayList)9 IgniteCache (org.apache.ignite.IgniteCache)9 MutableConfiguration (javax.cache.configuration.MutableConfiguration)8 CacheWriterException (javax.cache.integration.CacheWriterException)8 CacheNotExistsException (com.hazelcast.cache.CacheNotExistsException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7