-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmployeeCache.java
More file actions
70 lines (60 loc) · 1.9 KB
/
EmployeeCache.java
File metadata and controls
70 lines (60 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package th.about.reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.Hashtable;
/**
* 雇员缓存类
* @author Administrator
* 2015年9月16日
*/
public class EmployeeCache {
static private EmployeeCache employeeCache; // 一个Cache实例 ,单例
private Hashtable<String,EmployeeRef> employeeRef;// 用于Chche内容的存储
private ReferenceQueue<Employee> queue; // 垃圾Reference的队列
private class EmployeeRef extends SoftReference<Employee>{
private String key="";
public EmployeeRef(Employee referent,ReferenceQueue<Employee> queue) {
super(referent,queue);
key=referent.getId();
}
}
// 构建一个缓存器实例
private EmployeeCache(){
employeeRef=new Hashtable<String,EmployeeRef>();
queue=new ReferenceQueue<Employee>();
}
//获取缓存器实例
public static EmployeeCache getInstance(){
if(employeeCache==null){
employeeCache=new EmployeeCache();
}
return employeeCache;
}
// 清除那些所软引用的Employee对象已经被回收的EmployeeRef对象
private void clearnCache(){
EmployeeRef ref=null;
System.out.println("queue.poll()"+queue.poll());
while((ref=(EmployeeRef) queue.poll())!=null){
employeeRef.remove(ref.key);
}
}
// 以软引用的方式对一个Employee对象的实例进行引用并保存该引用
private void cacheEmployee(Employee emp){
this.clearnCache();
EmployeeRef ref=new EmployeeRef(emp, queue);
employeeRef.put(emp.getId(), ref);
}
public Employee getEmployee(String id){
Employee emp=null;
if(employeeRef.containsKey(id)){
emp=employeeRef.get(id).get();
System.out.println("缓存中的emp对象:"+emp.getName());
}
if(emp==null){
emp=new Employee(id,"李四","销售部","15998556330");
this.cacheEmployee(emp);
System.out.println("Retrieve From EmployeeInfoCenter.id=" + id);
}
return emp;
}
}