CacheStats class is used to track Statistics about the performance of a Cache. CacheStats instances are immutable.
To get statistics of a Cache instance, we need to enable the accumulation of statistics while creating Cache instance. you can do this using recordStats() method of CacheBuilder class.
Cache<Integer, Employee> empCache = CacheBuilder.newBuilder().maximumSize(500)
.expireAfterWrite(10, TimeUnit.SECONDS).recordStats().build();
How to get CacheStats instance?
You can get it by calling stats() method of Cache object.
CacheStats cacheStats = empCache.stats();
Find the below working application.
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 + "]";
}
}
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.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheStats;
import com.sample.app.cache.model.Employee;
public class HelloWorld {
private static void printEmployee(Employee emp, Integer id) {
if (emp == null) {
System.out.println("Employee not exist for the id " + id);
return;
}
System.out.println(emp);
}
private static void getAndPrintObjects(Cache<Integer, Employee> empCache, List<Integer> empIds)
throws ExecutionException {
for (Integer id : empIds) {
Employee emp = empCache.getIfPresent(id);
printEmployee(emp, id);
}
}
public static void main(String args[]) throws InterruptedException, ExecutionException {
Cache<Integer, Employee> empCache = CacheBuilder.newBuilder().maximumSize(500)
.expireAfterWrite(10, TimeUnit.SECONDS).recordStats().build();
empCache.put(1, new Employee(1, "Krishna", "Gurram"));
empCache.put(3, new Employee(3, "Chamu", "M"));
System.out.println("Getting employee with ids 1, 2 and 3 from empCache");
getAndPrintObjects(empCache, Arrays.asList(1, 2, 3));
CacheStats cacheStats = empCache.stats();
System.out.println("\n" + cacheStats);
}
}
Output
Getting employee with ids 1, 2 and 3 from empCache Employee [id=1, firstName=Krishna, lastName=Gurram] Employee not exist for the id 2 Employee [id=3, firstName=Chamu, lastName=M] CacheStats{hitCount=2, missCount=1, loadSuccessCount=0, loadExceptionCount=0, totalLoadTime=0, evictionCount=0}
No comments:
Post a Comment