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));
}
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));
}
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)));
}
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);
}
}
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.");
}
Aggregations