Search in sources :

Example 21 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.

the class IgniteCacheConfigVariationsFullApiTest method testPeekExpired.

/**
 * JUnit.
 *
 * @throws Exception If failed.
 */
public void testPeekExpired() throws Exception {
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(1).get(0);
    info("Using key: " + key);
    c.put(key, 1);
    assertEquals(Integer.valueOf(1), c.localPeek(key));
    int ttl = 500;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    c.withExpiryPolicy(expiry).put(key, 1);
    Thread.sleep(ttl + 100);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return c.localPeek(key) == null;
        }
    }, 2000);
    assertNull(c.localPeek(key));
    assert c.localSize() == 0 : "Cache is not empty.";
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration)

Example 22 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.

the class GridCacheAbstractFullApiSelfTest method checkTtl.

/**
 * @param inTx In tx flag.
 * @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
 * @throws Exception If failed.
 */
private void checkTtl(boolean inTx, boolean oldEntry) throws Exception {
    // TODO GG-11133.
    if (true)
        return;
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(jcache(), 1).get(0);
    IgnitePair<Long> entryTtl;
    if (oldEntry) {
        c.put(key, 1);
        entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals((Long) 0L, entryTtl.get1());
        assertEquals((Long) 0L, entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    if (inTx) {
        // Rollback transaction for the first time.
        Transaction tx = transactions().txStart();
        try {
            jcache().withExpiryPolicy(expiry).put(key, 1);
        } finally {
            tx.rollback();
        }
        if (oldEntry) {
            entryTtl = entryTtl(fullCache(), key);
            assertEquals((Long) 0L, entryTtl.get1());
            assertEquals((Long) 0L, entryTtl.get2());
        }
    }
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? transactions().txStart() : null;
    try {
        jcache().withExpiryPolicy(expiry).put(key, 1);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // And one more direct update to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().withExpiryPolicy(expiry).put(key, 3);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertTrue(curEntryTtl.get2() > startTime);
            expireTimes[i] = curEntryTtl.get2();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    log.info("Put 4");
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().put(key, 4);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    log.info("Put 4 done");
    for (int i = 0; i < gridCount(); i++) {
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
            IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
            assertNotNull(curEntryTtl.get1());
            assertNotNull(curEntryTtl.get2());
            assertEquals(ttl, (long) curEntryTtl.get1());
            assertEquals(expireTimes[i], (long) curEntryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @SuppressWarnings("unchecked")
        @Override
        public boolean applyx() {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                // Get "cache" field from GridCacheProxyImpl.
                GridCacheAdapter c0 = cacheFromCtx(c);
                if (!c0.context().deferredDelete()) {
                    GridCacheEntryEx e0 = c0.peekEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException e) {
                throw new RuntimeException(e);
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    IgniteCache fullCache = fullCache();
    if (!isMultiJvmObject(fullCache)) {
        GridCacheAdapter internalCache = internalCache(fullCache);
        if (internalCache.isLocal())
            return;
    }
    assert c.get(key) == null;
    // Ensure that old TTL and expire time are not longer "visible".
    entryTtl = entryTtl(fullCache(), key);
    assertNotNull(entryTtl.get1());
    assertNotNull(entryTtl.get2());
    assertEquals(0, (long) entryTtl.get1());
    assertEquals(0, (long) entryTtl.get2());
    // Ensure that next update will not pick old expire time.
    tx = inTx ? transactions().txStart() : null;
    try {
        jcache().put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    entryTtl = entryTtl(fullCache(), key);
    assertEquals((Integer) 10, c.get(key));
    assertNotNull(entryTtl.get1());
    assertNotNull(entryTtl.get2());
    assertEquals(0, (long) entryTtl.get1());
    assertEquals(0, (long) entryTtl.get2());
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy)

Example 23 with ExpiryPolicy

use of javax.cache.expiry.ExpiryPolicy in project caffeine by ben-manes.

the class TypesafeConfigurationTest method checkLazyExpiration.

static void checkLazyExpiration(CaffeineConfiguration<?, ?> config) {
    ExpiryPolicy expiry = config.getExpiryPolicyFactory().create();
    assertThat(expiry.getExpiryForCreation(), is(Duration.ONE_MINUTE));
    assertThat(expiry.getExpiryForUpdate(), is(Duration.FIVE_MINUTES));
    assertThat(expiry.getExpiryForAccess(), is(Duration.TEN_MINUTES));
}
Also used : ExpiryPolicy(javax.cache.expiry.ExpiryPolicy)

Example 24 with ExpiryPolicy

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

the class CacheReplaceMessageTask method prepareOperation.

@Override
protected Operation prepareOperation() {
    CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
    ExpiryPolicy expiryPolicy = serializationService.toObject(parameters.expiryPolicy);
    return operationProvider.createReplaceOperation(parameters.key, parameters.oldValue, parameters.newValue, expiryPolicy, parameters.completionId);
}
Also used : CacheOperationProvider(com.hazelcast.cache.impl.CacheOperationProvider) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy)

Example 25 with ExpiryPolicy

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

the class CacheGetMessageTask method prepareOperation.

@Override
protected Operation prepareOperation() {
    CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
    ExpiryPolicy expiryPolicy = (ExpiryPolicy) nodeEngine.toObject(parameters.expiryPolicy);
    return operationProvider.createGetOperation(parameters.key, expiryPolicy);
}
Also used : CacheOperationProvider(com.hazelcast.cache.impl.CacheOperationProvider) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy)

Aggregations

ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)54 Duration (javax.cache.expiry.Duration)23 TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)15 IgniteCacheExpiryPolicy (org.apache.ignite.internal.processors.cache.IgniteCacheExpiryPolicy)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 GridCacheEntryRemovedException (org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)11 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)11 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)10 CacheOperationContext (org.apache.ignite.internal.processors.cache.CacheOperationContext)9 GridCacheVersion (org.apache.ignite.internal.processors.cache.version.GridCacheVersion)9 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)9 CacheOperationProvider (com.hazelcast.cache.impl.CacheOperationProvider)7 UUID (java.util.UUID)7 AffinityTopologyVersion (org.apache.ignite.internal.processors.affinity.AffinityTopologyVersion)7 Transaction (org.apache.ignite.transactions.Transaction)7 Map (java.util.Map)5 GridCacheContext (org.apache.ignite.internal.processors.cache.GridCacheContext)5 GridCacheEntryEx (org.apache.ignite.internal.processors.cache.GridCacheEntryEx)5 IgniteTxTimeoutCheckedException (org.apache.ignite.internal.transactions.IgniteTxTimeoutCheckedException)5