Search in sources :

Example 51 with Duration

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

the class CacheContinuousQueryFailoverAbstractSelfTest method testBackupQueueEvict.

/**
 * @throws Exception If failed.
 */
@Test
public void testBackupQueueEvict() throws Exception {
    startGridsMultiThreaded(2);
    Ignite qryClient = startClientGrid(2);
    CacheEventListener1 lsnr = new CacheEventListener1(false);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = qryClient.cache(DEFAULT_CACHE_NAME).query(qry);
    assertEquals(0, backupQueue(ignite(0)).size());
    long ttl = 100;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
    final IgniteCache<Object, Object> cache0 = ignite(2).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry);
    final List<Integer> keys = primaryKeys(ignite(1).cache(DEFAULT_CACHE_NAME), BACKUP_ACK_THRESHOLD);
    lsnr.latch = new CountDownLatch(keys.size());
    for (Integer key : keys) {
        log.info("Put: " + key);
        cache0.put(key, key);
    }
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 5000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    boolean wait = waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return cache0.localPeek(keys.get(0)) == null;
        }
    }, ttl + 1000);
    assertTrue("Entry evicted.", wait);
    GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return backupQueue(ignite(0)).isEmpty();
        }
    }, 2000);
    assertTrue("Backup queue is not cleared: " + backupQueue(ignite(0)), backupQueue(ignite(0)).size() < BACKUP_ACK_THRESHOLD);
    if (!backupQueue(ignite(0)).isEmpty()) {
        for (Object o : backupQueue(ignite(0))) {
            CacheContinuousQueryEntry e = (CacheContinuousQueryEntry) o;
            assertNotSame("Evicted entry added to backup queue.", -1L, e.updateCounter());
        }
    }
    cur.close();
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Duration(javax.cache.expiry.Duration) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Ignite(org.apache.ignite.Ignite) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 52 with Duration

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

the class ScanQueryOffheapExpiryPolicySelfTest method getConfiguration.

/**
 * {@inheritDoc}
 */
@Override
protected IgniteConfiguration getConfiguration(String gridName) throws Exception {
    IgniteConfiguration cfg = super.getConfiguration(gridName);
    CacheConfiguration ccfg = defaultCacheConfiguration();
    ccfg.setName(CACHE_NAME);
    ccfg.setAtomicityMode(CacheAtomicityMode.ATOMIC);
    ccfg.setCacheMode(CacheMode.PARTITIONED);
    ccfg.setBackups(1);
    ccfg.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MINUTES, 10)));
    cfg.setCacheConfiguration(ccfg);
    return cfg;
}
Also used : IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Duration(javax.cache.expiry.Duration) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration)

Example 53 with Duration

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

the class WebSessionFilter method cacheWithExpiryPolicy.

/**
 * @param maxInactiveInteval Interval to use in expiry policy.
 * @param cache Cache.
 * @param <T> Cached object type.
 * @return Cache with expiry policy if {@code maxInactiveInteval} greater than zero.
 */
private <T> IgniteCache<String, T> cacheWithExpiryPolicy(final int maxInactiveInteval, final IgniteCache<String, T> cache) {
    if (maxInactiveInteval > 0) {
        long ttl = maxInactiveInteval * 1000L;
        ExpiryPolicy plc = new ModifiedExpiryPolicy(new Duration(MILLISECONDS, ttl));
        return cache.withExpiryPolicy(plc);
    }
    return cache;
}
Also used : ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Duration(javax.cache.expiry.Duration) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Example 54 with Duration

use of javax.cache.expiry.Duration in project wcomponents by BorderTech.

the class PlainTextRendererImpl method getCache.

/**
 * @return the cache instance
 */
protected synchronized Cache<String, String> getCache() {
    Cache<String, String> cache = Caching.getCache(CACHE_NAME, String.class, String.class);
    if (cache == null) {
        final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
        MutableConfiguration<String, String> config = new MutableConfiguration<>();
        config.setTypes(String.class, String.class);
        config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
        // No need to serialize the result
        config.setStoreByValue(false);
        cache = mgr.createCache(CACHE_NAME, config);
    }
    return cache;
}
Also used : CacheManager(javax.cache.CacheManager) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration)

Example 55 with Duration

use of javax.cache.expiry.Duration in project wcomponents by BorderTech.

the class VelocityCacheImpl method getCache.

/**
 * @return the cache instance
 */
protected synchronized Cache<Object, Resource> getCache() {
    Cache<Object, Resource> cache = Caching.getCache(CACHE_NAME, Object.class, Resource.class);
    if (cache == null) {
        final CacheManager mgr = Caching.getCachingProvider().getCacheManager();
        MutableConfiguration<Object, Resource> config = new MutableConfiguration<>();
        config.setTypes(Object.class, Resource.class);
        config.setExpiryPolicyFactory(AccessedExpiryPolicy.factoryOf(new Duration(TimeUnit.HOURS, 12)));
        // Velocity template classes are not serializable so use by ref.
        config.setStoreByValue(false);
        cache = mgr.createCache(CACHE_NAME, config);
    }
    return cache;
}
Also used : Resource(org.apache.velocity.runtime.resource.Resource) CacheManager(javax.cache.CacheManager) Duration(javax.cache.expiry.Duration) MutableConfiguration(javax.cache.configuration.MutableConfiguration)

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