CacheLoader class provides ‘refresh’ method to refresh the value associated with given key.
Example
empCache.refresh(2);
Above statement refresh the value associated with id 2.
What will happen when an exception thrown while refreshing the value?
if an exception is thrown while refreshing the previous value will remain exists.
Find the below working applictation.
Employee.java
package com.sample.app.cache.model;
public class Employee {
private Integer id;
private String firstName;
private String lastName;
public Employee(Integer id, String firstName, String lastName) {
super();
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public Integer getId() {
return id;
}
public void setId(Integer 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;
}
@Override
public String toString() {
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + "]";
}
}
EmployeeService.java
package com.sample.app.cache.service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import com.sample.app.cache.model.Employee;
public class EmployeeService {
private static Map<Integer, Employee> data = new HashMap<>();
static {
data.put(1, new Employee(1, "Krishna", "Gurram"));
data.put(2, new Employee(2, "Bomma", "Srikanth"));
}
public static Employee getObject(Integer key) throws InterruptedException {
System.out.println("Loading object with id " + key + " from database....");
TimeUnit.SECONDS.sleep(2);
return data.get(key);
}
public static void updateEmp(Integer id, String firstName, String lastName) {
Employee emp = new Employee(id, firstName, lastName);
data.put(id, emp);
}
}
HelloWorld.java
package com.sample.app.cache;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.sample.app.cache.model.Employee;
import com.sample.app.cache.service.EmployeeService;
public class HelloWorld {
private static void printEmployee(Employee emp, Integer id) {
System.out.println(emp + "\n");
}
private static void getAndPrintObjects(LoadingCache<Integer, Employee> empCache, List<Integer> empIds)
throws ExecutionException {
for (Integer id : empIds) {
try {
Employee emp = empCache.get(id);
printEmployee(emp, id);
} catch (Exception e) {
System.out.println(e.getMessage() + "\n");
}
}
}
public static void main(String args[]) throws InterruptedException, ExecutionException {
CacheLoader<Integer, Employee> cacheLoader = new CacheLoader<Integer, Employee>() {
@Override
public Employee load(Integer key) throws Exception {
Employee emp = EmployeeService.getObject(key);
if (emp == null) {
throw new RuntimeException("Object not found with key " + key);
}
return emp;
}
};
LoadingCache<Integer, Employee> empCache = CacheBuilder.newBuilder().maximumSize(500)
.expireAfterWrite(60, TimeUnit.SECONDS).recordStats().build(cacheLoader);
System.out.println("Getting employee with ids 1, 2 from empCache\n");
getAndPrintObjects(empCache, Arrays.asList(1, 2));
System.out.println("Updating the information associated with id 2");
EmployeeService.updateEmp(2, "Bomma_Updated", "Srikanth_Updated");
getAndPrintObjects(empCache, Arrays.asList(2));
System.out.println("Refreshing the cache with id 2");
empCache.refresh(2);
getAndPrintObjects(empCache, Arrays.asList(2));
}
}
Output
Getting employee with ids 1, 2 from empCache Loading object with id 1 from database.... Employee [id=1, firstName=Krishna, lastName=Gurram] Loading object with id 2 from database.... Employee [id=2, firstName=Bomma, lastName=Srikanth] Updating the information associated with id 2 Employee [id=2, firstName=Bomma, lastName=Srikanth] Refreshing the cache with id 2 Loading object with id 2 from database.... Employee [id=2, firstName=Bomma_Updated, lastName=Srikanth_Updated]
No comments:
Post a Comment