use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testCompactExpired.
/**
* TODO GG-11133.
*
* @throws Exception If failed.
*/
public void testCompactExpired() throws Exception {
final IgniteCache<String, Integer> cache = jcache();
final String key = F.first(primaryKeysForCache(cache, 1));
cache.put(key, 1);
long ttl = 500;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
return cache.localPeek(key) == null;
}
}, ttl + 1000);
// Peek will actually remove entry from cache.
assertNull(cache.localPeek(key));
assertEquals(0, cache.localSize());
// Clear readers, if any.
cache.remove(key);
}
use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class IgniteCacheReadThroughEvictionSelfTest method testReadThroughWithExpirePolicy.
/**
* @throws Exception if failed.
*/
public void testReadThroughWithExpirePolicy() throws Exception {
Ignite ig = testedGrid();
CacheConfiguration<Object, Object> cc = variationConfig("expire");
IgniteCache<Object, Object> cache = ig.createCache(cc);
try {
ExpiryPolicy exp = new CreatedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, TIMEOUT));
for (int i = 0; i < KEYS; i++) cache.withExpiryPolicy(exp).put(key(i), value(i));
U.sleep(TIMEOUT);
waitEmpty(cc.getName());
exp = new AccessedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, TIMEOUT));
for (int i = 0; i < KEYS; i++) {
assertEquals(value(i), cache.get(key(i)));
cache.withExpiryPolicy(exp).get(key(i));
}
U.sleep(TIMEOUT);
waitEmpty(cc.getName());
for (int i = 0; i < KEYS; i++) assertEquals(value(i), cache.get(key(i)));
} finally {
destroyCacheSafe(ig, cc.getName());
}
}
use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class GridCacheAbstractFullApiSelfTest method testEvictExpired.
/**
* TODO GG-11133.
* @throws Exception In case of error.
*/
public void testEvictExpired() throws Exception {
final IgniteCache<String, Integer> cache = jcache();
final String key = primaryKeysForCache(cache, 1).get(0);
cache.put(key, 1);
assertEquals((Integer) 1, cache.get(key));
long ttl = 500;
final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, ttl));
grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(expiry).put(key, 1);
final Affinity<String> aff = ignite(0).affinity(DEFAULT_CACHE_NAME);
boolean wait = waitForCondition(new GridAbsPredicate() {
@Override
public boolean apply() {
for (int i = 0; i < gridCount(); i++) {
if (peek(jcache(i), key) != null)
return false;
}
return true;
}
}, ttl + 1000);
assertTrue("Failed to wait for entry expiration.", wait);
// Expired entry should not be swapped.
cache.localEvict(Collections.singleton(key));
assertNull(peek(cache, "key"));
assertNull(cache.localPeek(key, ONHEAP));
assertTrue(cache.localSize() == 0);
load(cache, key, true);
for (int i = 0; i < gridCount(); i++) {
if (aff.isPrimary(grid(i).cluster().localNode(), key))
assertEquals((Integer) 1, peek(jcache(i), key));
if (aff.isBackup(grid(i).cluster().localNode(), key))
assertEquals((Integer) 1, peek(jcache(i), key));
}
}
use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class GridCacheBasicApiAbstractTest method testPutWithExpiration.
/**
* @throws Exception In case of error.
*/
public void testPutWithExpiration() throws Exception {
IgniteCache<Integer, String> cache = ignite.cache(DEFAULT_CACHE_NAME);
CacheEventListener lsnr = new CacheEventListener(new CountDownLatch(1));
ignite.events().localListen(lsnr, EVTS_CACHE);
ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(MILLISECONDS, 200L));
try {
int key = (int) System.currentTimeMillis();
cache.withExpiryPolicy(expiry).put(key, "val");
assert cache.get(key) != null;
cache.withExpiryPolicy(expiry).put(key, "val");
Thread.sleep(500);
assert cache.get(key) == null;
} finally {
ignite.events().stopLocalListen(lsnr, EVTS_CACHE);
}
}
use of javax.cache.expiry.ExpiryPolicy in project ignite by apache.
the class GridCacheAdapter method localLoadCache.
/**
* {@inheritDoc}
*/
@Override
public void localLoadCache(final IgniteBiPredicate<K, V> p, Object[] args) throws IgniteCheckedException {
final boolean replicate = ctx.isDrEnabled();
final AffinityTopologyVersion topVer = ctx.affinity().affinityTopologyVersion();
CacheOperationContext opCtx = ctx.operationContextPerCall();
ExpiryPolicy plc0 = opCtx != null ? opCtx.expiry() : null;
final ExpiryPolicy plc = plc0 != null ? plc0 : ctx.expiry();
final boolean keepBinary = opCtx != null && opCtx.isKeepBinary();
if (p != null)
ctx.kernalContext().resource().injectGeneric(p);
try {
if (ctx.store().isLocal()) {
DataStreamerImpl ldr = ctx.kernalContext().dataStream().dataStreamer(ctx.name());
try {
ldr.skipStore(true);
ldr.receiver(new IgniteDrDataStreamerCacheUpdater());
ldr.keepBinary(keepBinary);
LocalStoreLoadClosure c = new LocalStoreLoadClosure(p, ldr, plc);
ctx.store().loadCache(c, args);
c.onDone();
} finally {
ldr.closeEx(false);
}
} else {
// Version for all loaded entries.
final GridCacheVersion ver0 = ctx.versions().nextForLoad();
ctx.store().loadCache(new CIX3<KeyCacheObject, Object, GridCacheVersion>() {
@Override
public void applyx(KeyCacheObject key, Object val, @Nullable GridCacheVersion ver) throws IgniteException {
assert ver == null;
long ttl = CU.ttlForLoad(plc);
if (ttl == CU.TTL_ZERO)
return;
loadEntry(key, val, ver0, (IgniteBiPredicate<Object, Object>) p, topVer, replicate, ttl);
}
}, args);
}
} finally {
if (p instanceof PlatformCacheEntryFilter)
((PlatformCacheEntryFilter) p).onClose();
}
}
Aggregations