在同一台电脑中使用数据库和应用程序的时候,不使用缓存的情况下数据读取速度更快(无论是第一次读取还是第二次读取)
测试案例:
1:使用缓存
@Test
public void testHibernate(){
AdminDAO ad=new AdminDAO();
Session s=ad.getSession();
try{
String queryString="from Admin";
Query queryObject = s.createQuery(queryString);
queryObject.setCacheable(true);
System.out.println("第一次读取");
List l=queryObject.list();
System.out.println(l.size());
s.close();
s=ad.getSession();
Long time1=System.currentTimeMillis();
Query q=s.createQuery("from Admin where id>"+9000);
q.setCacheable(true);
List l3=q.list();
Long time2=System.currentTimeMillis();
System.out.println(l3.size());
s.close();
System.out.println((time2-time1)+"ms");
s=ad.getSession();
time1=System.currentTimeMillis();
q=s.createQuery("from Admin where id>"+9000);
q.setCacheable(true);
l3=q.list();
time2=System.currentTimeMillis();
System.out.println(l3.size());
s.close();
System.out.println((time2-time1)+"ms");
}catch(Exception e){
e.printStackTrace();
}
}
输出结果:
第一次读取
2010-11-30 DEBUG org.hibernate.SQL - select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_
Hibernate: select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_
9999
2010-11-30 DEBUG org.hibernate.SQL - select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
Hibernate: select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
1000
15ms
1000
31ms
2:不使用缓存:
@Test
public void testHibernate(){
AdminDAO ad=new AdminDAO();
Session s=ad.getSession();
// Transaction ts=s.beginTransaction();
try{
String queryString="from Admin";
Query queryObject = s.createQuery(queryString);
// queryObject.setCacheable(true);
System.out.println("第一次读取");
List l=queryObject.list();
System.out.println(l.size());
s.close();
s=ad.getSession();
Long time1=System.currentTimeMillis();
Query q=s.createQuery("from Admin where id>"+9000);
// q.setCacheable(true);
List l3=q.list();
Long time2=System.currentTimeMillis();
System.out.println(l3.size());
s.close();
System.out.println((time2-time1)+"ms");
s=ad.getSession();
time1=System.currentTimeMillis();
q=s.createQuery("from Admin where id>"+9000);
// q.setCacheable(true);
l3=q.list();
time2=System.currentTimeMillis();
System.out.println(l3.size());
s.close();
System.out.println((time2-time1)+"ms");
}catch(Exception e){
e.printStackTrace();
}
}
输出结果:
第一次读取
2010-11-30 DEBUG org.hibernate.SQL - select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_
Hibernate: select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_
9999
2010-11-30 DEBUG org.hibernate.SQL - select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
Hibernate: select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
1000
15ms
2010-11-30 DEBUG org.hibernate.SQL - select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
Hibernate: select admin0_.id as id0_, admin0_.username as username0_, admin0_.password as password0_ from mdg.admin admin0_ where admin0_.id>9000
1000
16ms
从控制台的打印结果来看,没有使用缓存的情况下,读取速度更快!
建议:如果数据库和java应用在同一台服务器上时候不要用ehcache缓存
以上测试环境:mysql5+hibernate3+ehcache