use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class IgniteCacheConfigVariationsFullApiTest method testPeekExpired.
/**
* JUnit.
*
* @throws Exception If failed.
*/
public void testPeekExpired() throws Exception {
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(1).get(0);
info("Using key: " + key);
c.put(key, 1);
assertEquals(Integer.valueOf(1), c.localPeek(key));
int ttl = 500;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
c.withExpiryPolicy(expiry).put(key, 1);
Thread.sleep(ttl + 100);
GridTestUtils.waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return c.localPeek(key) == null;
}
}, 2000);
assertNull(c.localPeek(key));
assert c.localSize() == 0 : "Cache is not empty.";
}
use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method checkTtl.
/**
* @param inTx In tx flag.
* @param oldEntry {@code True} to check TTL on old entry, {@code false} on new.
* @throws Exception If failed.
*/
private void checkTtl(boolean inTx, boolean oldEntry) throws Exception {
// TODO GG-11133.
if (true)
return;
int ttl = 1000;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
final IgniteCache<String, Integer> c = jcache();
final String key = primaryKeysForCache(jcache(), 1).get(0);
IgnitePair<Long> entryTtl;
if (oldEntry) {
c.put(key, 1);
entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
long startTime = System.currentTimeMillis();
if (inTx) {
// Rollback transaction for the first time.
Transaction tx = transactions().txStart();
try {
jcache().withExpiryPolicy(expiry).put(key, 1);
} finally {
tx.rollback();
}
if (oldEntry) {
entryTtl = entryTtl(fullCache(), key);
assertEquals((Long) 0L, entryTtl.get1());
assertEquals((Long) 0L, entryTtl.get2());
}
}
// Now commit transaction and check that ttl and expire time have been saved.
Transaction tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 1);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
long[] expireTimes = new long[gridCount()];
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// One more update from the same cache entry to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 2);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more direct update to ensure that expire time is shifted forward.
U.sleep(100);
tx = inTx ? transactions().txStart() : null;
try {
jcache().withExpiryPolicy(expiry).put(key, 3);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertTrue(curEntryTtl.get2() > startTime);
expireTimes[i] = curEntryTtl.get2();
}
}
// And one more update to ensure that ttl is not changed and expire time is not shifted forward.
U.sleep(100);
log.info("Put 4");
tx = inTx ? transactions().txStart() : null;
try {
jcache().put(key, 4);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
log.info("Put 4 done");
for (int i = 0; i < gridCount(); i++) {
if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key)) {
IgnitePair<Long> curEntryTtl = entryTtl(jcache(i), key);
assertNotNull(curEntryTtl.get1());
assertNotNull(curEntryTtl.get2());
assertEquals(ttl, (long) curEntryTtl.get1());
assertEquals(expireTimes[i], (long) curEntryTtl.get2());
}
}
// Avoid reloading from store.
storeStgy.removeFromStore(key);
assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {
@SuppressWarnings("unchecked")
@Override
public boolean applyx() {
try {
Integer val = c.get(key);
if (val != null) {
info("Value is in cache [key=" + key + ", val=" + val + ']');
return false;
}
// Get "cache" field from GridCacheProxyImpl.
GridCacheAdapter c0 = cacheFromCtx(c);
if (!c0.context().deferredDelete()) {
GridCacheEntryEx e0 = c0.peekEx(key);
return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
} else
return true;
} catch (GridCacheEntryRemovedException e) {
throw new RuntimeException(e);
}
}
}, Math.min(ttl * 10, getTestTimeout())));
IgniteCache fullCache = fullCache();
if (!isMultiJvmObject(fullCache)) {
GridCacheAdapter internalCache = internalCache(fullCache);
if (internalCache.isLocal())
return;
}
assert c.get(key) == null;
// Ensure that old TTL and expire time are not longer "visible".
entryTtl = entryTtl(fullCache(), key);
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
// Ensure that next update will not pick old expire time.
tx = inTx ? transactions().txStart() : null;
try {
jcache().put(key, 10);
if (tx != null)
tx.commit();
} finally {
if (tx != null)
tx.close();
}
U.sleep(2000);
entryTtl = entryTtl(fullCache(), key);
assertEquals((Integer) 10, c.get(key));
assertNotNull(entryTtl.get1());
assertNotNull(entryTtl.get2());
assertEquals(0, (long) entryTtl.get1());
assertEquals(0, (long) entryTtl.get2());
}
use of javax.cache.expiry.ExpiryPolicy in project caffeine by ben-manes.
the class TypesafeConfigurationTest method checkLazyExpiration.
static void checkLazyExpiration(CaffeineConfiguration<?, ?> config) {
ExpiryPolicy expiry = config.getExpiryPolicyFactory().create();
assertThat(expiry.getExpiryForCreation(), is(Duration.ONE_MINUTE));
assertThat(expiry.getExpiryForUpdate(), is(Duration.FIVE_MINUTES));
assertThat(expiry.getExpiryForAccess(), is(Duration.TEN_MINUTES));
}
use of javax.cache.expiry.ExpiryPolicy in project hazelcast by hazelcast.
the class CacheReplaceMessageTask method prepareOperation.
@Override
protected Operation prepareOperation() {
CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
ExpiryPolicy expiryPolicy = serializationService.toObject(parameters.expiryPolicy);
return operationProvider.createReplaceOperation(parameters.key, parameters.oldValue, parameters.newValue, expiryPolicy, parameters.completionId);
}
use of javax.cache.expiry.ExpiryPolicy in project hazelcast by hazelcast.
the class CacheGetMessageTask method prepareOperation.
@Override
protected Operation prepareOperation() {
CacheOperationProvider operationProvider = getOperationProvider(parameters.name);
ExpiryPolicy expiryPolicy = (ExpiryPolicy) nodeEngine.toObject(parameters.expiryPolicy);
return operationProvider.createGetOperation(parameters.key, expiryPolicy);
}
Aggregations