Search in sources :

Example 66 with Duration

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

the class AbstractCacheRecordStore method updateAccessDuration.

protected long updateAccessDuration(Data key, R record, ExpiryPolicy expiryPolicy, long now) {
    long expiryTime = TIME_NOT_AVAILABLE;
    try {
        Duration expiryDuration = expiryPolicy.getExpiryForAccess();
        if (expiryDuration != null) {
            expiryTime = getAdjustedExpireTime(expiryDuration, now);
            record.setExpirationTime(expiryTime);
            if (isEventsEnabled()) {
                CacheEventContext cacheEventContext = createBaseEventContext(CacheEventType.EXPIRATION_TIME_UPDATED, toEventData(key), toEventData(record.getValue()), expiryTime, null, IGNORE_COMPLETION);
                cacheEventContext.setAccessHit(record.getHits());
                publishEvent(cacheEventContext);
            }
        }
    } catch (Exception e) {
        ignore(e);
    }
    return expiryTime;
}
Also used : Duration(javax.cache.expiry.Duration) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException)

Example 67 with Duration

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

the class AbstractCacheRecordStore method updateRecordWithExpiry.

@SuppressWarnings("checkstyle:parameternumber")
protected boolean updateRecordWithExpiry(Data key, Object value, R record, ExpiryPolicy expiryPolicy, long now, boolean disableWriteThrough, int completionId, UUID source, UUID origin) {
    expiryPolicy = getExpiryPolicy(record, expiryPolicy);
    long expiryTime = TIME_NOT_AVAILABLE;
    try {
        Duration expiryDuration = expiryPolicy.getExpiryForUpdate();
        if (expiryDuration != null) {
            expiryTime = getAdjustedExpireTime(expiryDuration, now);
        }
    } catch (Exception e) {
        ignore(e);
    }
    return updateRecordWithExpiry(key, value, record, expiryTime, now, disableWriteThrough, completionId, source, origin);
}
Also used : Duration(javax.cache.expiry.Duration) CacheWriterException(javax.cache.integration.CacheWriterException) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheNotExistsException(com.hazelcast.cache.CacheNotExistsException)

Example 68 with Duration

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

the class CacheExpirationTest method test_whenEntryIsRemovedBackupIsCleaned.

@Test
public void test_whenEntryIsRemovedBackupIsCleaned() {
    int ttlSeconds = 3;
    Duration duration = new Duration(TimeUnit.SECONDS, ttlSeconds);
    HazelcastExpiryPolicy expiryPolicy = new HazelcastExpiryPolicy(duration, duration, duration);
    CacheConfig<Integer, Integer> cacheConfig = createCacheConfig(expiryPolicy);
    Cache<Integer, Integer> cache = createCache(cacheConfig);
    for (int i = 0; i < KEY_RANGE; i++) {
        cache.put(i, i);
    }
    for (int i = 0; i < KEY_RANGE; i++) {
        cache.remove(i, i);
    }
    for (int i = 1; i < CLUSTER_SIZE; i++) {
        BackupAccessor backupAccessor = TestBackupUtils.newCacheAccessor(instances, cache.getName(), i);
        assertBackupSizeEventually(0, backupAccessor);
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BackupAccessor(com.hazelcast.test.backup.BackupAccessor) Duration(javax.cache.expiry.Duration) HazelcastExpiryPolicy(com.hazelcast.cache.HazelcastExpiryPolicy) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 69 with Duration

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

the class CacheBasicAbstractTest method expiryTimeShouldNotBeChangedOnUpdateWhenCreatedExpiryPolicyIsUsed.

// https://github.com/hazelcast/hazelcast/issues/7236
@Test
public void expiryTimeShouldNotBeChangedOnUpdateWhenCreatedExpiryPolicyIsUsed() {
    final int createdExpiryTimeMillis = 100;
    Duration duration = new Duration(TimeUnit.MILLISECONDS, createdExpiryTimeMillis);
    CacheConfig<Integer, String> cacheConfig = new CacheConfig<>();
    cacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(duration));
    Cache<Integer, String> cache = createCache(cacheConfig);
    cache.put(1, "value");
    cache.put(1, "value");
    sleepAtLeastMillis(createdExpiryTimeMillis + 1);
    assertNull(cache.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(javax.cache.expiry.Duration) CacheConfig(com.hazelcast.config.CacheConfig) Test(org.junit.Test)

Example 70 with Duration

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

the class CacheBasicAbstractTest method testRecordExpiryPolicyTakesPrecedenceOverPolicyAtCreation.

@Test
public void testRecordExpiryPolicyTakesPrecedenceOverPolicyAtCreation() {
    final int updatedTtlMillis = 1000;
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value", new TouchedExpiryPolicy(Duration.ONE_DAY));
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, updatedTtlMillis)));
    sleepAtLeastMillis(updatedTtlMillis + 1);
    assertNull(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)

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