3、对象Bean
public class User {
private int id;
private String name;
private String sex;
private int age;
private Date accessTime; public User(int id) {
super();
this.id = id;
this.accessTime = new Date(System.currentTimeMillis());
}
public String toString() {
return "User info is : id=" + id + " accessTime="
+ accessTime.toString();
}
public User(String name, String sex, int age) {
super();
this.name = name;
this.sex = sex;
this.age = age;
}
public User() {
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getAccessTime() {
return accessTime;
}
public void setAccessTime(Date accessTime) {
this.accessTime = accessTime;
}
}
4、测试类
public class TestObjectCache {
public static void main(String[] args) {
CacheManager cm=CacheManager.getInstance();
TestObjectCache test=new TestObjectCache();
test.print(cm);
}
public void print(CacheManager cm){
User user=null;
for (int i = 0; i < 1000; i++) {
user=cm.getUser(100);
System.out.println("<<"+i+">>: "+user);
if(i==10){
//删除缓存id的对象
cm.removeUser(100+"");
}
if(i==20){
//删除所有缓存的对象
cm.removeAllNews();
}
// 睡眠部分
try {
Thread.sleep(30000);
} catch (Exception e) {
}
}
}
}