Search in sources :

Example 71 with Duration

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

the class CacheBasicAbstractTest method entryShouldNotBeExpiredWhenTimeUnitIsNullAndDurationIsZero.

@Test
public void entryShouldNotBeExpiredWhenTimeUnitIsNullAndDurationIsZero() {
    Duration duration = new Duration(null, 0);
    CacheConfig<Integer, String> cacheConfig = new CacheConfig<>();
    cacheConfig.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(duration));
    Cache<Integer, String> cache = createCache(cacheConfig);
    cache.put(1, "value");
    sleepAtLeastMillis(1);
    assertNotNull(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 72 with Duration

use of javax.cache.expiry.Duration in project pdi-dataservice-server-plugin by pentaho.

the class ServiceCache method ttlMatches.

private boolean ttlMatches(Cache<CachedService.CacheKey, CachedService> cache, LogChannelInterface log) {
    CompleteConfiguration config = cache.getConfiguration(CompleteConfiguration.class);
    if (getTimeToLive() == null) {
        // ttl has not been modified
        return true;
    }
    if (config != null) {
        try {
            Duration duration = getConfigDuration(config);
            long ttl = Long.parseLong(getTimeToLive());
            return ttl == duration.getDurationAmount();
        } catch (NumberFormatException nfe) {
            log.logError(String.format("Failed to determine configured TTL value for cache '%s'.  TTL value = '%s'", cache.getName(), getTimeToLive()));
            throw nfe;
        }
    }
    log.logError(String.format("Failed to check TTL consistency with cache for name '%s'.\n  Assuming cache can be used", cache.getName()));
    return true;
}
Also used : CompleteConfiguration(javax.cache.configuration.CompleteConfiguration) Duration(javax.cache.expiry.Duration)

Example 73 with Duration

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

the class ListenerICacheTest method putExpiryAsync.

@TimeStep(prob = 0)
public void putExpiryAsync(ThreadState state) {
    int expiryDuration = state.randomInt(maxExpiryDurationMs);
    ExpiryPolicy expiryPolicy = new CreatedExpiryPolicy(new Duration(MILLISECONDS, expiryDuration));
    int key = state.randomInt(keyCount);
    cache.putAsync(key, state.randomLong(), expiryPolicy);
    state.putAsyncExpiry++;
}
Also used : CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) TimeStep(com.hazelcast.simulator.test.annotations.TimeStep)

Example 74 with Duration

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

the class JCacheTest method testCreatedExpiryPolicy.

@Test
public void testCreatedExpiryPolicy() throws Exception {
    RedisProcess runner = new RedisRunner().nosave().randomDir().port(6311).run();
    URL configUrl = getClass().getResource("redisson-jcache.yaml");
    Config cfg = Config.fromYAML(configUrl);
    MutableConfiguration c = new MutableConfiguration();
    c.setExpiryPolicyFactory(CreatedExpiryPolicy.factoryOf(new Duration(MILLISECONDS, 500)));
    Configuration<String, String> config = RedissonConfiguration.fromConfig(cfg, c);
    Cache<String, String> cache = Caching.getCachingProvider().getCacheManager().createCache("test", config);
    cache.put("1", "2");
    Thread.sleep(500);
    assertThat(cache.get("1")).isNull();
    cache.put("1", "3");
    assertThat(cache.get("1")).isEqualTo("3");
    Thread.sleep(500);
    assertThat(cache.get("1")).isNull();
    cache.put("1", "4");
    assertThat(cache.get("1")).isEqualTo("4");
    Thread.sleep(100);
    cache.put("1", "5");
    assertThat(cache.get("1")).isEqualTo("5");
    cache.close();
    runner.stop();
}
Also used : RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Duration(javax.cache.expiry.Duration) RedisRunner(org.redisson.RedisRunner) URL(java.net.URL) MutableConfiguration(javax.cache.configuration.MutableConfiguration) BaseTest(org.redisson.BaseTest) Test(org.junit.jupiter.api.Test)

Example 75 with Duration

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

the class CacheExpirationPromotionTest method promoted_replica_should_send_eviction_to_other_backup.

@Test
public void promoted_replica_should_send_eviction_to_other_backup() {
    final CachingProvider provider = createServerCachingProvider(instances[0]);
    provider.getCacheManager().createCache(cacheName, getCacheConfig());
    HazelcastCacheManager cacheManager = (HazelcastServerCacheManager) provider.getCacheManager();
    final String keyOwnedByLastInstance = generateKeyOwnedBy(instances[nodeCount - 1]);
    ICache<String, String> cache = cacheManager.getCache(cacheName).unwrap(ICache.class);
    cache.put(keyOwnedByLastInstance, "dummyVal", new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 5)));
    final BackupAccessor<String, String> backupAccessor = TestBackupUtils.newCacheAccessor(instances, cacheName, 1);
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertNotNull(backupAccessor.get(keyOwnedByLastInstance));
        }
    });
    instances[nodeCount - 1].shutdown();
    // the backup replica became the primary, now the backup is the other node.
    // we check if the newly appointed replica sent expiration to backups
    assertTrueEventually(new AssertTask() {

        @Override
        public void run() {
            assertEquals(0, backupAccessor.size());
        }
    });
}
Also used : HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) HazelcastServerCacheManager(com.hazelcast.cache.impl.HazelcastServerCacheManager) AssertTask(com.hazelcast.test.AssertTask) Duration(javax.cache.expiry.Duration) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) CacheTestSupport.createServerCachingProvider(com.hazelcast.cache.CacheTestSupport.createServerCachingProvider) CachingProvider(javax.cache.spi.CachingProvider) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) 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