By setting custom Expiry instance, we can set the expiry time at each entry level.
Example
Cache<Integer, Employee> cache = Caffeine.newBuilder().expireAfter(new Expiry<Integer, Employee>() {
@Override
public long expireAfterCreate(Integer key, Employee emp, long currentTime) {
return TimeUnit.SECONDS.toNanos(emp.getExpiryTime());
}
@Override
public long expireAfterUpdate(Integer key, Employee emp, long currentTime, long currentDuration) {
return currentDuration;
}
@Override
public long expireAfterRead(Integer key, Employee emp, long currentTime, long currentDuration) {
return currentDuration;
}
}).build();
Find the below working application.
Employee.java
package com.sample.app.model;
public class Employee {
private int id;
private String firstName;
private String lastName;
private long expiryTime;
public Employee(int id, String firstName, String lastName, long expiryTime) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.expiryTime = expiryTime;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public long getExpiryTime() {
return expiryTime;
}
public void setExpiryTime(long expiryTime) {
this.expiryTime = expiryTime;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", expiryTime="
+ expiryTime + "]";
}
}
App.java
package com.sample.app;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.github.benmanes.caffeine.cache.Expiry;
import com.sample.app.model.Employee;
import com.sample.app.service.EmployeeService;
public class App {
private static void printCache(Cache cache) {
System.out.println("Elements in the cache are");
Map map = cache.asMap();
for (Object key : map.keySet()) {
System.out.println(map.get(key));
}
}
public static void main(String args[]) throws InterruptedException {
Cache<Integer, Employee> cache = Caffeine.newBuilder().expireAfter(new Expiry<Integer, Employee>() {
@Override
public long expireAfterCreate(Integer key, Employee emp, long currentTime) {
return TimeUnit.SECONDS.toNanos(emp.getExpiryTime());
}
@Override
public long expireAfterUpdate(Integer key, Employee emp, long currentTime, long currentDuration) {
return currentDuration;
}
@Override
public long expireAfterRead(Integer key, Employee emp, long currentTime, long currentDuration) {
return currentDuration;
}
}).build();
Employee emp1 = new Employee(1, "Krishna", "Gurram", 5l);
Employee emp2 = new Employee(2, "Gopi", "Battu", 8l);
Employee emp3 = new Employee(3, "Saurav", "Sarkar", 15l);
cache.put(emp1.getId(), emp1);
cache.put(emp2.getId(), emp2);
cache.put(emp3.getId(), emp3);
printCache(cache);
System.out.println("\nAbout to sleep for 10 seconds");
TimeUnit.SECONDS.sleep(10);
/*
* Since cache eviction is done asynchronously, let's execute pending
* maintenance operations needed by the cache
*/
System.out.println("\nPerforming cleanup operations\n");
cache.cleanUp();
printCache(cache);
}
}
Output
Elements in the cache are Employee [id=1, firstName=Krishna, lastName=Gurram, expiryTime=5] Employee [id=2, firstName=Gopi, lastName=Battu, expiryTime=8] Employee [id=3, firstName=Saurav, lastName=Sarkar, expiryTime=15] About to sleep for 10 seconds Performing cleanup operations Elements in the cache are Employee [id=3, firstName=Saurav, lastName=Sarkar, expiryTime=15]
No comments:
Post a Comment