Using 'getExpectedExpiration' method, we can get the expected expiration time. This method return the expected expiration time in milliseconds.
Example
long expectedExpiration1 = expiringMap.getExpectedExpiration(1);
Employee.java
package com.sample.app.model;
public class Employee {
private int id;
private String name;
public Employee(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
public void logOnExpire() {
System.out.println(this.toString() + " is expired");
}
}
ExpectedExpirationDemo.java
package com.sample.app;
import java.util.concurrent.TimeUnit;
import com.sample.app.model.Employee;
import net.jodah.expiringmap.ExpirationPolicy;
import net.jodah.expiringmap.ExpiringMap;
public class ExpectedExpirationDemo {
private static void sleepNSeconds(int n) throws InterruptedException {
System.out.println("\nSleeping for " + n + " seconds");
TimeUnit.SECONDS.sleep(n);
}
public static void main(String args[]) throws InterruptedException {
ExpiringMap<Integer, Employee> expiringMap = ExpiringMap.builder().expiration(5, TimeUnit.SECONDS)
.expirationPolicy(ExpirationPolicy.CREATED).build();
expiringMap.put(1, new Employee(1, "Employee" + 1));
sleepNSeconds(2);
expiringMap.put(2, new Employee(2, "Employee" + 2));
long expectedExpiration1 = expiringMap.getExpectedExpiration(1);
long expectedExpiration2 = expiringMap.getExpectedExpiration(2);
System.out.println("Expected exiry time for entry1 " + expectedExpiration1 + " milli seconds");
System.out.println("Expected exiry time for entry2 " + expectedExpiration2 + " milli seconds");
}
}
Output
Sleeping for 2 seconds
Expected exiry time for entry1 2981 milli seconds
Expected exiry time for entry2 4999 milli seconds
No comments:
Post a Comment