Search in sources :

Example 1 with TouchedExpiryPolicy

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

the class CacheBasicAbstractTest method testRecordExpiryPolicyTakesPrecedenceOverCachePolicy.

@Test
public void testRecordExpiryPolicyTakesPrecedenceOverCachePolicy() {
    final int updatedTtlMillis = 1000;
    CacheConfig<Integer, String> cacheConfig = new CacheConfig<>();
    cacheConfig.setExpiryPolicyFactory(TouchedExpiryPolicy.factoryOf(Duration.ONE_DAY));
    ICache<Integer, String> cache = createCache(cacheConfig);
    cache.put(1, "value");
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, updatedTtlMillis)));
    sleepAtLeastMillis(updatedTtlMillis + 1);
    assertNull(cache.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) CacheConfig(com.hazelcast.config.CacheConfig) Test(org.junit.Test)

Example 2 with TouchedExpiryPolicy

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

the class CacheBasicAbstractTest method test_whenExpiryPolicyIsOverridden_thenNewPolicyIsInEffect.

@Test
public void test_whenExpiryPolicyIsOverridden_thenNewPolicyIsInEffect() {
    final int ttlMillis = 1000;
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value");
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttlMillis)));
    cache.setExpiryPolicy(1, new TouchedExpiryPolicy(Duration.ETERNAL));
    sleepAtLeastMillis(ttlMillis + 1);
    assertEquals("value", cache.get(1));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Duration(javax.cache.expiry.Duration) Test(org.junit.Test)

Example 3 with TouchedExpiryPolicy

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

the class CacheBasicAbstractTest method testSetExpiryPolicyReturnsFalse_whenKeyIsAlreadyExpired.

@Test
public void testSetExpiryPolicyReturnsFalse_whenKeyIsAlreadyExpired() {
    ICache<Integer, String> cache = createCache();
    cache.put(1, "value", new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, 1)));
    sleepAtLeastSeconds(2);
    assertFalse(cache.setExpiryPolicy(1, new TouchedExpiryPolicy(Duration.FIVE_MINUTES)));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(javax.cache.expiry.Duration) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) Test(org.junit.Test)

Example 4 with TouchedExpiryPolicy

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

the class CacheRecordStoreTestSupport method putAndSetExpiryPolicyFromRecordStore.

protected void putAndSetExpiryPolicyFromRecordStore(ICacheRecordStore cacheRecordStore, InMemoryFormat inMemoryFormat) {
    SerializationService serializationService = new DefaultSerializationServiceBuilder().build();
    ExpiryPolicy expiryPolicy = new TouchedExpiryPolicy(Duration.ETERNAL);
    Data policyData = serializationService.toData(expiryPolicy);
    for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
        Data keyData = serializationService.toData(i);
        cacheRecordStore.put(keyData, "value-" + i, null, null, -1);
        cacheRecordStore.setExpiryPolicy(Collections.singleton(keyData), policyData, null);
    }
    if (inMemoryFormat == InMemoryFormat.BINARY || inMemoryFormat == InMemoryFormat.NATIVE) {
        for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
            assertTrue(Data.class.isAssignableFrom(cacheRecordStore.getExpiryPolicy(serializationService.toData(i)).getClass()));
        }
    } else if (inMemoryFormat == InMemoryFormat.OBJECT) {
        for (int i = 0; i < CACHE_RECORD_COUNT; i++) {
            assertTrue(ExpiryPolicy.class.isAssignableFrom(cacheRecordStore.getExpiryPolicy(serializationService.toData(i)).getClass()));
        }
    } else {
        throw new IllegalArgumentException("Unsupported in-memory format: " + inMemoryFormat);
    }
}
Also used : DefaultSerializationServiceBuilder(com.hazelcast.internal.serialization.impl.DefaultSerializationServiceBuilder) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) SerializationService(com.hazelcast.internal.serialization.SerializationService) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) Data(com.hazelcast.internal.serialization.Data)

Example 5 with TouchedExpiryPolicy

use of javax.cache.expiry.TouchedExpiryPolicy in project gora by apache.

the class JCacheStore method initialize.

@Override
public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException {
    super.initialize(keyClass, persistentClass, properties);
    String cachingProviderKey = properties.getProperty(GORA_DEFAULT_JCACHE_PROVIDER_KEY);
    if (cachingProviderKey != null) {
        cachingProvider = Caching.getCachingProvider(cachingProviderKey);
    } else {
        cachingProvider = Caching.getCachingProvider();
    }
    try {
        this.persistentDataStore = DataStoreFactory.getDataStore(keyClass, persistentClass, new Configuration());
    } catch (GoraException ex) {
        LOG.error("Couldn't initialize persistent DataStore.", ex);
        throw ex;
    }
    manager = cachingProvider.getCacheManager();
    if (((properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY) != null) && Boolean.valueOf(properties.getProperty(JCACHE_AUTO_CREATE_CACHE_PROPERTY_KEY))) || ((manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass) == null))) {
        cacheEntryList = new ConcurrentSkipListSet<>();
        MutableConfiguration mutableCacheConfig = new MutableConfiguration<>();
        mutableCacheConfig.setTypes(keyClass, persistentClass);
        if (properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY) != null) {
            mutableCacheConfig.setReadThrough(Boolean.valueOf(properties.getProperty(JCACHE_READ_THROUGH_PROPERTY_KEY)));
        } else {
            mutableCacheConfig.setReadThrough(true);
        }
        if (properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY) != null) {
            mutableCacheConfig.setWriteThrough(Boolean.valueOf(properties.getProperty(JCACHE_WRITE_THROUGH_PROPERTY_KEY)));
        } else {
            mutableCacheConfig.setWriteThrough(true);
        }
        if (properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY) != null) {
            mutableCacheConfig.setStoreByValue(Boolean.valueOf(properties.getProperty(JCACHE_STORE_BY_VALUE_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY) != null) {
            mutableCacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_STATISTICS_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY) != null) {
            mutableCacheConfig.setStatisticsEnabled(Boolean.valueOf(properties.getProperty(JCACHE_MANAGEMENT_PROPERTY_KEY)));
        }
        if (properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY) != null) {
            String expiryPolicyIdentifier = properties.getProperty(JCACHE_EXPIRE_POLICY_PROPERTY_KEY);
            if (expiryPolicyIdentifier.equals(JCACHE_ACCESSED_EXPIRY_IDENTIFIER)) {
                mutableCacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new AccessedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_CREATED_EXPIRY_IDENTIFIER)) {
                mutableCacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new CreatedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_MODIFIED_EXPIRY_IDENTIFIER)) {
                mutableCacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new ModifiedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            } else if (expiryPolicyIdentifier.equals(JCACHE_TOUCHED_EXPIRY_IDENTIFIER)) {
                mutableCacheConfig.setExpiryPolicyFactory(FactoryBuilder.factoryOf(new TouchedExpiryPolicy(new Duration(TimeUnit.SECONDS, Integer.valueOf(properties.getProperty(JCACHE_EXPIRE_POLICY_DURATION_PROPERTY_KEY))))));
            }
        }
        mutableCacheConfig.setCacheLoaderFactory(JCacheCacheFactoryBuilder.factoryOfCacheLoader(this.persistentDataStore, keyClass, persistentClass));
        mutableCacheConfig.setCacheWriterFactory(JCacheCacheFactoryBuilder.factoryOfCacheWriter(this.persistentDataStore, keyClass, persistentClass));
        cache = manager.createCache(persistentClass.getSimpleName(), mutableCacheConfig);
        cacheConfig = mutableCacheConfig;
    } else {
        cache = manager.getCache(super.getPersistentClass().getSimpleName(), keyClass, persistentClass);
        this.populateLocalCacheEntrySet(cache);
    }
    cache.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(JCacheCacheFactoryBuilder.factoryOfEntryListener(new JCacheCacheEntryListener<K, T>(cacheEntryList)), null, true, true));
    LOG.info("JCache Gora datastore initialized successfully.");
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) MutableConfiguration(javax.cache.configuration.MutableConfiguration) MutableCacheEntryListenerConfiguration(javax.cache.configuration.MutableCacheEntryListenerConfiguration) CompleteConfiguration(javax.cache.configuration.CompleteConfiguration) Duration(javax.cache.expiry.Duration) AccessedExpiryPolicy(javax.cache.expiry.AccessedExpiryPolicy) CreatedExpiryPolicy(javax.cache.expiry.CreatedExpiryPolicy) MutableConfiguration(javax.cache.configuration.MutableConfiguration) GoraException(org.apache.gora.util.GoraException) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ModifiedExpiryPolicy(javax.cache.expiry.ModifiedExpiryPolicy)

Aggregations

TouchedExpiryPolicy (javax.cache.expiry.TouchedExpiryPolicy)36 Duration (javax.cache.expiry.Duration)35 Test (org.junit.Test)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)19 ExpiryPolicy (javax.cache.expiry.ExpiryPolicy)18 GridAbsPredicate (org.apache.ignite.internal.util.lang.GridAbsPredicate)13 Transaction (org.apache.ignite.transactions.Transaction)9 IgniteCache (org.apache.ignite.IgniteCache)8 GridAbsPredicateX (org.apache.ignite.internal.util.lang.GridAbsPredicateX)4 IgniteCacheConfigVariationsAbstractTest (org.apache.ignite.testframework.junits.IgniteCacheConfigVariationsAbstractTest)4 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)4 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)3 IgniteKernal (org.apache.ignite.internal.IgniteKernal)3 Ignore (org.junit.Ignore)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 CreatedExpiryPolicy (javax.cache.expiry.CreatedExpiryPolicy)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 IgniteException (org.apache.ignite.IgniteException)2 SqlQuery (org.apache.ignite.cache.query.SqlQuery)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2