ehcache缓存使用策略

在同一台电脑中使用数据库和应用程序的时候,不使用缓存的情况下数据读取速度更快(无论是第一次读取还是第二次读取)

测试案例:

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值