一、创建Employee员工类
package cn.codeduck.guavatest.pojo;
import com.google.common.base.MoreObjects;
public class Employee {
private String name;
private String dept;
private String empID;
public Employee() {
}
public Employee(String name, String dept, String empID) {
this.name = name;
this.dept = dept;
this.empID = empID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getEmpID() {
return empID;
}
public void setEmpID(String empID) {
this.empID = empID;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(Employee.class)
.add("name",name)
.add("dept",dept)
.add("empID", empID).toString();
}
}
二、Guava Cache使用示例:
package cn.codeduck.guavatest.basic;
import cn.codeduck.guavatest.pojo.Employee;
import com.google.common.cache.*;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.ListenableFuture;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
public class LoadingCacheTest {
@Test
public void test(){
//创建一个员工LoadingCache对象
LoadingCache<String, Employee> employeeLoadingCache = CacheBuilder.newBuilder()
.maximumSize(100) //基于容量回收:总数量100个
// .maximumWeight(1000) //基于容量回收:总权重1000
// .weigher(new Weigher<String, Employee>() { //配置权重
// @Override
// public int weigh(String key, Employee employee) {
// return key.length();
// }
// })
.expireAfterAccess(30, TimeUnit.MINUTES) //定时回收:没有读写访问30分钟后失效清理
.expireAfterWrite(30, TimeUnit.MINUTES) //定时回收:没有写访问30分钟后失效清理
.refreshAfterWrite(3, TimeUnit.SECONDS) //定时刷新缓存,和expireAfterWrite使用,只有检索时刷新缓存。
.weakKeys() //基于引用回收:弱引用key
.weakValues() //基于引用回收:弱引用value
// .softValues() //基于引用回收:软引用value
.removalListener(RemovalListeners.asynchronous(new RemovalListener<String, Employee>() { //异步移除监听器
@Override
public void onRemoval(RemovalNotification<String, Employee> removalNotification) {
System.out.println("removalNotification = " + removalNotification);
}
}, new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}))
.recordStats() //开启Guava Cache 统计功能
.build(CacheLoader.asyncReloading(new CacheLoader<String, Employee>() { //异步缓存加载器
// get 缓存获取不到值时候加载
@Override
public Employee load(String key) throws Exception {
//数据库中加载
return getFromDatabase(key);
}
// refresh 扩展刷新时的行为
@Override
public ListenableFuture<Employee> reload(String key, Employee oldValue) throws Exception {
return super.reload(key, oldValue);
}
// getAll 缓存批量获取不到值时候加载
@Override
public Map<String, Employee> loadAll(Iterable<? extends String> keys) throws Exception {
return super.loadAll(keys);
}
}, new Executor() {
@Override
public void execute(Runnable command) {
command.run();
}
}));
// 1.单个查询
System.out.println("1.单个查询");
try {
System.out.println("#第一次调用:");
// 在第一次调用时,缓存将从数据库中填充相应的员工记录
System.out.println(employeeLoadingCache.get("100"));
System.out.println(employeeLoadingCache.get("103"));
System.out.println(employeeLoadingCache.get("110"));
System.out.println("#第二次调用:");
// 在第二次调用时,会直接从缓存中读取
System.out.println(employeeLoadingCache.get("100"));
System.out.println(employeeLoadingCache.get("103"));
System.out.println(employeeLoadingCache.get("110"));
} catch (ExecutionException e) {
e.printStackTrace();
}
System.out.println("#第三次调用:");
// 第三次调用时 getUnchecked 不执行检查
System.out.println(employeeLoadingCache.getUnchecked("100"));
System.out.println(employeeLoadingCache.getUnchecked("103"));
System.out.println(employeeLoadingCache.getUnchecked("110"));
//1.单个查询
//#第一次调用:
//数据库命中100
//Employee{name=Mahesh, dept=Finance, empID=100}
//数据库命中103
//Employee{name=Rohan, dept=IT, empID=103}
//数据库命中110
//Employee{name=Sohan, dept=Admin, empID=110}
//#第二次调用:
//Employee{name=Mahesh, dept=Finance, empID=100}
//Employee{name=Rohan, dept=IT, empID=103}
//Employee{name=Sohan, dept=Admin, empID=110}
//#第三次调用:
//Employee{name=Mahesh, dept=Finance, empID=100}
//Employee{name=Rohan, dept=IT, empID=103}
//Employee{name=Sohan, dept=Admin, empID=110}
// 2.批量查询
System.out.println("\n2.批量查询");
try {
List<String> list = Lists.newArrayList("100", "103", "110");
ImmutableMap<String, Employee> cacheAll = employeeLoadingCache.getAll(list);
cacheAll.forEach((k,v)->{
System.out.println("key: "+k+", value: "+v);
});
} catch (ExecutionException e) {
e.printStackTrace();
}
//2.批量查询
//key: 100, value: Employee{name=Mahesh, dept=Finance, empID=100}
//key: 103, value: Employee{name=Rohan, dept=IT, empID=103}
//key: 110, value: Employee{name=Sohan, dept=Admin, empID=110}
// 3.回调查询
System.out.println("\n3.回调查询");
try {
Object employee = employeeLoadingCache.get("001", new Callable<Employee>() {
@Override
public Employee call() throws Exception {
return getFromDatabase("001");
}
});
System.out.println(employee);
} catch (ExecutionException e) {
e.printStackTrace();
}
//3.回调查询
//数据库命中001
//Employee{name=Tingfeng, dept=Admin, empID=001}
// 4.显示插入
System.out.println("\n4.显示插入");
employeeLoadingCache.put("001", new Employee("若风","总裁部","001"));
System.out.println(employeeLoadingCache.getUnchecked("001"));
//4.显示插入
//removalNotification = 001=Employee{name=Tingfeng, dept=Admin, empID=001}
//Employee{name=若风, dept=总裁部, empID=001}
// 5.更改视图
System.out.println("\n5.更改视图");
try {
ConcurrentMap<String, Employee> asMap = employeeLoadingCache.asMap();
asMap.put("100",new Employee("codeduck","CTO","100"));
ImmutableMap<String, Employee> cacheAll = employeeLoadingCache.getAll(Lists.newArrayList("100", "103", "110"));
cacheAll.forEach((k,v)->{
System.out.println("key: "+k+", value:"+v);
});
} catch (ExecutionException e) {
e.printStackTrace();
}
//5.更改视图
//removalNotification = 100=Employee{name=Mahesh, dept=Finance, empID=100}
//key: 100, value:Employee{name=codeduck, dept=CTO, empID=100}
//key: 103, value:Employee{name=Rohan, dept=IT, empID=103}
//key: 110, value:Employee{name=Sohan, dept=Admin, empID=110}
// 6.清除缓存
System.out.println("\n6.清除所有缓存,调用移除监听器");
employeeLoadingCache.invalidateAll();
//6.清除所有缓存,调用移除监听器
//removalNotification = 100=Employee{name=codeduck, dept=CTO, empID=100}
//removalNotification = 001=Employee{name=若风, dept=总裁部, empID=001}
//removalNotification = 110=Employee{name=Sohan, dept=Admin, empID=110}
//removalNotification = 103=Employee{name=Rohan, dept=IT, empID=103}
}
// 模拟数据库中数据
private static Employee getFromDatabase(String empId) {
Map<String, Employee> database = Maps.newHashMap();
database.put("100", new Employee("Mahesh", "Finance", "100"));
database.put("103", new Employee("Rohan", "IT", "103"));
database.put("110", new Employee("Sohan", "Admin", "110"));
database.put("001", new Employee("Tingfeng", "Admin", "001"));
System.out.println("数据库命中" + empId);
return database.get(empId);
}
}