Search in sources :

Example 6 with CreatedExpiryPolicy

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

the class GridCacheContinuousQueryAbstractSelfTest method testExpired.

/**
     * @throws Exception If failed.
     */
public void testExpired() throws Exception {
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
    final Map<Object, Object> map = new ConcurrentHashMap8<>();
    final CountDownLatch latch = new CountDownLatch(2);
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setIncludeExpired(true);
    qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

        @Override
        public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
            for (CacheEntryEvent<?, ?> e : evts) {
                if (e.getEventType() == EventType.EXPIRED) {
                    assertNull(e.getValue());
                    map.put(e.getKey(), e.getOldValue());
                    latch.countDown();
                }
            }
        }
    });
    try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
        cache.put(1, 1);
        cache.put(2, 2);
        // Wait for expiration.
        Thread.sleep(2000);
        assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
        assertEquals(2, map.size());
        assertEquals(1, (int) map.get(1));
        assertEquals(2, (int) map.get(2));
    }
}
Also used : ConcurrentHashMap8(org.jsr166.ConcurrentHashMap8) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CountDownLatch(java.util.concurrent.CountDownLatch) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery)

Example 7 with CreatedExpiryPolicy

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

the class JCacheCreationExpiryTest method getConfiguration.

@Override
protected CaffeineConfiguration<Integer, Integer> getConfiguration() {
    CaffeineConfiguration<Integer, Integer> configuration = new CaffeineConfiguration<>();
    configuration.setExpiryPolicyFactory(() -> new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, EXPIRY_DURATION)));
    configuration.setTickerFactory(() -> ticker::read);
    return configuration;
}
Also used : Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CaffeineConfiguration(com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)

Example 8 with CreatedExpiryPolicy

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

the class IgniteCacheExpiryStoreLoadSelfTest method checkLoad.

/**
     * @param async If {@code true} uses asynchronous method.
     * @throws Exception If failed.
     */
private void checkLoad(boolean async) throws Exception {
    IgniteCache<String, Integer> cache = jcache(0).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, TIME_TO_LIVE)));
    List<Integer> keys = new ArrayList<>();
    keys.add(primaryKey(jcache(0)));
    keys.add(primaryKey(jcache(1)));
    keys.add(primaryKey(jcache(2)));
    if (async)
        cache.loadCacheAsync(null, keys.toArray(new Integer[3])).get();
    else
        cache.loadCache(null, keys.toArray(new Integer[3]));
    assertEquals(3, cache.size(CachePeekMode.PRIMARY));
    Thread.sleep(TIME_TO_LIVE + WAIT_TIME);
    assertEquals(0, cache.size());
}
Also used : ArrayList(java.util.ArrayList) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy)

Example 9 with CreatedExpiryPolicy

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

the class IgniteCacheExpiryStoreLoadSelfTest method testLoadAllWithExpiry.

/**
     * @throws Exception If failed.
     */
public void testLoadAllWithExpiry() throws Exception {
    IgniteCache<Integer, Integer> cache = ignite(0).<Integer, Integer>cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, TIME_TO_LIVE)));
    Set<Integer> keys = new HashSet<>();
    keys.add(primaryKey(jcache(0)));
    keys.add(primaryKey(jcache(1)));
    keys.add(primaryKey(jcache(2)));
    CompletionListenerFuture fut = new CompletionListenerFuture();
    cache.loadAll(keys, false, fut);
    fut.get();
    assertEquals(3, cache.size(CachePeekMode.PRIMARY));
    Thread.sleep(TIME_TO_LIVE + WAIT_TIME);
    assertEquals(0, cache.size());
}
Also used : Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CompletionListenerFuture(javax.cache.integration.CompletionListenerFuture) HashSet(java.util.HashSet)

Example 10 with CreatedExpiryPolicy

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

the class IgniteCacheTtlCleanupSelfTest method testDeferredDeleteTtl.

/**
     * @throws Exception If failed.
     */
public void testDeferredDeleteTtl() throws Exception {
    IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)));
    int cnt = GridDhtLocalPartition.MAX_DELETE_QUEUE_SIZE / PART_NUM + 100;
    for (long i = 0; i < cnt; i++) grid(0).cache(DEFAULT_CACHE_NAME).put(i * PART_NUM, i);
    for (int i = 0; i < cnt; i++) cache.put(i * PART_NUM, i);
    // Wait 5 seconds.
    Thread.sleep(6_000);
    assertEquals(cnt, grid(0).cache(DEFAULT_CACHE_NAME).size());
    GridCacheAdapter<Object, Object> cacheAdapter = ((IgniteKernal) grid(0)).internalCache(DEFAULT_CACHE_NAME);
    IgniteCacheObjectProcessor cacheObjects = cacheAdapter.context().cacheObjects();
    CacheObjectContext cacheObjCtx = cacheAdapter.context().cacheObjectContext();
    for (int i = 0; i < 100; i++) assertNull(cacheAdapter.map().getEntry(cacheObjects.toCacheKeyObject(cacheObjCtx, null, i, true)));
}
Also used : IgniteKernal(org.apache.ignite.internal.IgniteKernal) IgniteCacheObjectProcessor(org.apache.ignite.internal.processors.cacheobject.IgniteCacheObjectProcessor) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CacheObjectContext(org.apache.ignite.internal.processors.cache.CacheObjectContext)

Aggregations

CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)14 Duration (javax.cache.expiry.Duration)14 Ignite (org.apache.ignite.Ignite)4 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashSet (java.util.HashSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 CacheEntryEvent (javax.cache.event.CacheEntryEvent)2 AccessedExpiryPolicy (javax.cache.expiry.AccessedExpiryPolicy)2 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)2 CaffeineConfiguration (com.github.benmanes.caffeine.jcache.configuration.CaffeineConfiguration)1 HazelcastCachingProvider (com.hazelcast.cache.HazelcastCachingProvider)1 ICache (com.hazelcast.cache.ICache)1 ClientConfig (com.hazelcast.client.config.ClientConfig)1 XmlClientConfigBuilder (com.hazelcast.client.config.XmlClientConfigBuilder)1 CacheConfig (com.hazelcast.config.CacheConfig)1 ClasspathXmlConfig (com.hazelcast.config.ClasspathXmlConfig)1 Config (com.hazelcast.config.Config)1 EvictionConfig (com.hazelcast.config.EvictionConfig)1 URI (java.net.URI)1