use of javax.cache.expiry.Duration in project hazelcast by hazelcast.
the class CacheBasicAbstractTest method testRecordExpiryPolicyTakesPrecedence.
@Test
public void testRecordExpiryPolicyTakesPrecedence() {
final int ttlMillis = 1000;
Duration modifiedDuration = new Duration(TimeUnit.MILLISECONDS, ttlMillis);
ICache<Integer, String> cache = createCache();
cache.put(1, "value");
cache.setExpiryPolicy(1, TouchedExpiryPolicy.factoryOf(modifiedDuration).create());
sleepAtLeastMillis(ttlMillis + 1);
assertNull(cache.get(1));
}
use of javax.cache.expiry.Duration 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.Duration 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.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, UUID origin) {
expiryPolicy = getExpiryPolicy(null, 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);
}
use of javax.cache.expiry.Duration in project hazelcast by hazelcast.
the class AbstractCacheRecordStore method readThroughRecord.
public R readThroughRecord(Data key, long now) {
Object value = readThroughCache(key);
if (value == null) {
return null;
}
Duration expiryDuration;
try {
expiryDuration = defaultExpiryPolicy.get().getExpiryForCreation();
} catch (Exception e) {
expiryDuration = Duration.ETERNAL;
}
long expiryTime = getAdjustedExpireTime(expiryDuration, now);
if (isExpiredAt(expiryTime, now)) {
return null;
}
return createRecord(key, value, expiryTime, IGNORE_COMPLETION);
}
Aggregations