Hi all,

We can easily set an expiry policy for any entry. Sorry to Anton  and Vladimir 
, you are right.


Talk is cheap, show you the code...


the test case
public static void testPut1() throws Exception {
    getCache().put("key","val", TimeUnit.SECONDS.toMillis(1));
    String val = (String)getCache().get("key");
    A.notNullOrEmpty(val, "get failed with " + val);
    A.ensure(val.equals("val"), "val failed with " + val);

    TimeUnit.SECONDS.sleep(1);
    A.ensure(null == getCache().get("key"), "key is not expired.");

    // five seconds
    getCache().put("key", "val", TimeUnit.SECONDS.toMillis(5));
    A.ensure("val".equals(getCache().get("key")), "put failed");
    getCache().remove("key"); // remove it immediately
    A.ensure(null == getCache().get("key"), "key is not removed.");
}
the implementation of put with expired time,public void put(Object key, Object 
value, long expireTime) {
   ExpiryPolicy plc = new CreatedExpiryPolicy(new 
Duration(TimeUnit.MILLISECONDS,expireTime));
   IgniteCache cache0 = cache.withExpiryPolicy(plc); // I have been made 
confused here, the previous implementation was cache = 
cache.withExpiryPolicy(plc);
   cache0.getAndPut(key, value);
}Thank you, Valdimir and Anton. Thanks everyone.
Regards,
Lin

Reply via email to