Search in sources :

Example 16 with Duration

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

the class CacheBasicAbstractTest method testRecordExpiryPolicyTakesPrecedence.

@Test
public void testRecordExpiryPolicyTakesPrecedence() {
    final int ttlMillis = 1000;
    Duration modifiedDuration = new Duration(TimeUnit.MILLISECONDS, ttlMillis);
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value");
    cache.setExpiryPolicy(1, TouchedExpiryPolicy.factoryOf(modifiedDuration).create());
    sleepAtLeastMillis(ttlMillis + 1);
    assertNull(cache.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 17 with Duration

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

the class CacheBasicAbstractTest method test_whenExpiryPolicyIsOverridden_thenNewPolicyIsInEffect.

@Test
public void test_whenExpiryPolicyIsOverridden_thenNewPolicyIsInEffect() {
    final int ttlMillis = 1000;
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value");
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttlMillis)));
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(Duration.ETERNAL));
    sleepAtLeastMillis(ttlMillis + 1);
    assertEquals("value", cache.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 18 with Duration

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

the class CacheBasicAbstractTest method testSetExpiryPolicyReturnsFalse_whenKeyIsAlreadyExpired.

@Test
public void testSetExpiryPolicyReturnsFalse_whenKeyIsAlreadyExpired() {
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value", new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 1)));
    sleepAtLeastSeconds(2);
    assertFalse(cache.setExpiryPolicy(1, new TouchedExpiryPolicy(Duration.FIVE_MINUTES)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(javax.cache.expiry.Duration) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Test(org.junit.Test)

Example 19 with Duration

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

the class AbstractCacheRecordStore method createRecordWithExpiry.

protected R createRecordWithExpiry(Data key, Object value, ExpiryPolicy expiryPolicy, long now, boolean disableWriteThrough, int completionId, UUID origin) {
    expiryPolicy = getExpiryPolicy(null, expiryPolicy);
    Duration expiryDuration;
    try {
        expiryDuration = expiryPolicy.getExpiryForCreation();
    } catch (Exception e) {
        expiryDuration = Duration.ETERNAL;
    }
    long expiryTime = getAdjustedExpireTime(expiryDuration, now);
    return createRecordWithExpiry(key, value, expiryTime, now, disableWriteThrough, completionId, origin);
}
Also used : Duration(javax.cache.expiry.Duration) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException)

Example 20 with Duration

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

the class AbstractCacheRecordStore method readThroughRecord.

public R readThroughRecord(Data key, long now) {
    Object value = readThroughCache(key);
    if (value == null) {
        return null;
    }
    Duration expiryDuration;
    try {
        expiryDuration = defaultExpiryPolicy.get().getExpiryForCreation();
    } catch (Exception e) {
        expiryDuration = Duration.ETERNAL;
    }
    long expiryTime = getAdjustedExpireTime(expiryDuration, now);
    if (isExpiredAt(expiryTime, now)) {
        return null;
    }
    return createRecord(key, value, expiryTime, IGNORE_COMPLETION);
}
Also used : Duration(javax.cache.expiry.Duration) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException)

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