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)));
}
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"));
}
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);
}
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);
}
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);
}
Aggregations