Search in sources :

Example 1 with Duration

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

the class JCacheLoaderAdapter method expireTimeMS.

private long expireTimeMS() {
    try {
        Duration duration = expiry.getExpiryForCreation();
        if (duration.isZero()) {
            return 0;
        } else if (duration.isEternal()) {
            return Long.MAX_VALUE;
        }
        long millis = TimeUnit.NANOSECONDS.toMillis(ticker.read());
        return duration.getAdjustedTime(millis);
    } catch (Exception e) {
        return Long.MAX_VALUE;
    }
}
Also used : Duration(javax.cache.expiry.Duration) CacheLoaderException(javax.cache.integration.CacheLoaderException)

Example 2 with Duration

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

the class CacheProxy method setAccessExpirationTime.

/**
   * Sets the access expiration time.
   *
   * @param expirable the entry that was operated on
   * @param currentTimeMS the current time, or 0 if not read yet
   */
protected final void setAccessExpirationTime(Expirable<?> expirable, long currentTimeMS) {
    try {
        Duration duration = expiry.getExpiryForAccess();
        if (duration == null) {
            return;
        } else if (duration.isZero()) {
            expirable.setExpireTimeMS(0L);
        } else if (duration.isEternal()) {
            expirable.setExpireTimeMS(Long.MAX_VALUE);
        } else {
            if (currentTimeMS == 0) {
                currentTimeMS = ticker.read();
            }
            long expireTimeMS = duration.getAdjustedTime(currentTimeMS);
            expirable.setExpireTimeMS(expireTimeMS);
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Failed to set the entry's expiration time", e);
    }
}
Also used : Duration(javax.cache.expiry.Duration) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 3 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, String origin) {
    expiryPolicy = getExpiryPolicy(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 4 with Duration

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

the class JCacheAccessExpiryTest method getConfiguration.

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

Example 5 with Duration

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

the class IgniteEventsTest method testConsumeAllEvents.

@Test
public void testConsumeAllEvents() throws Exception {
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("ignite:events:abc").to("mock:test1");
        }
    });
    getMockEndpoint("mock:test1").expectedMinimumMessageCount(9);
    IgniteCache<String, String> cache = ignite().getOrCreateCache("abc");
    // Generate cache activity.
    cache.put("abc", "123");
    cache.get("abc");
    cache.remove("abc");
    cache.withExpiryPolicy(CreatedExpiryPolicy.factoryOf(new Duration(TimeUnit.MILLISECONDS, 100)).create()).put("abc", "123");
    Thread.sleep(150);
    cache.get("abc");
    assertMockEndpointsSatisfied();
    List<Integer> eventTypes = receivedEventTypes("mock:test1");
    assert_().that(eventTypes).containsAllOf(EventType.EVT_CACHE_STARTED, EventType.EVT_CACHE_ENTRY_CREATED, EventType.EVT_CACHE_OBJECT_PUT, EventType.EVT_CACHE_OBJECT_READ, EventType.EVT_CACHE_OBJECT_REMOVED, EventType.EVT_CACHE_OBJECT_PUT, EventType.EVT_CACHE_OBJECT_EXPIRED).inOrder();
}
Also used : RouteBuilder(org.apache.camel.builder.RouteBuilder) 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