/**
* 本程序通过MyDB模拟数据库 通过HashMap模拟session缓存
* 具体过程参看代码注释
*/
package com.eh.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;
public class MyCache {
/**
* @param args
*/
private static Map<Integer,Student> map=new HashMap<Integer,Student>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=getStudentById(1);
System.out.println(s1.getName());
Student s3=getStudentById(1);
System.out.println(s3.getName());
Student s2=getStudentById(2);
System.out.println(s2.getName());
}
public static Student getStudentById(int id){
//首先从缓存中取
if(map.containsKey(id)){
System.out.println("从缓存中取出");
return map.get(id);
}else{
//从数据库中取出
System.out.println("从数据库中取出");
Student s=MyDB.getStudentById(id);
//放入缓存
map.put(id, s);
return s;
}
}
}
class MyDB{
private static List<Student> list=new ArrayList<Student>();
static{
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.setId(1);
s1.setName("大宝");
s2.setId(2);
s2.setName("老宝");
s3.setId(1);
s3.setName("小宝");
list.add(s1);
list.add(s2);
list.add(s3);
}
public static Student getStudentById(int id){
for(Student s:list){
if(id==s.getId()){
return s;
}
}
return null;
}
}
class Student{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
* 本程序通过MyDB模拟数据库 通过HashMap模拟session缓存
* 具体过程参看代码注释
*/
package com.eh.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.HashedMap;
public class MyCache {
/**
* @param args
*/
private static Map<Integer,Student> map=new HashMap<Integer,Student>();
public static void main(String[] args) {
// TODO Auto-generated method stub
Student s1=getStudentById(1);
System.out.println(s1.getName());
Student s3=getStudentById(1);
System.out.println(s3.getName());
Student s2=getStudentById(2);
System.out.println(s2.getName());
}
public static Student getStudentById(int id){
//首先从缓存中取
if(map.containsKey(id)){
System.out.println("从缓存中取出");
return map.get(id);
}else{
//从数据库中取出
System.out.println("从数据库中取出");
Student s=MyDB.getStudentById(id);
//放入缓存
map.put(id, s);
return s;
}
}
}
class MyDB{
private static List<Student> list=new ArrayList<Student>();
static{
Student s1=new Student();
Student s2=new Student();
Student s3=new Student();
s1.setId(1);
s1.setName("大宝");
s2.setId(2);
s2.setName("老宝");
s3.setId(1);
s3.setName("小宝");
list.add(s1);
list.add(s2);
list.add(s3);
}
public static Student getStudentById(int id){
for(Student s:list){
if(id==s.getId()){
return s;
}
}
return null;
}
}
class Student{
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}