`
scholers
  • 浏览: 615986 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

EHcache 思考

阅读更多
  
  问题一:缓存的命中率低。如代码所示,如果getAllObject查询参数是一个VO对象,那么当你用new每次创建的时候,缓存系统都认为不是同一个对象,导致缓存失败,直接从数据库查询;
  但是当你用SPRING的载入bean的方式来加载bean的时候,默认方式,没有修改为多态模式,缓存可以找到,但是设置查询条件却没有用了;
   那么这样条件下的命中率很低,缓存还有意义吗?

下面的代码是设置缓存的例子:

String DEFAULT_CONTEXT_FILE = "/applicationContext.xml";   
	        ApplicationContext context =  new ClassPathXmlApplicationContext(DEFAULT_CONTEXT_FILE); 
	        //SeaBean seaBean = new SeaBean();
	        
	        TestServiceImpl testService = (TestServiceImpl)context.getBean("testService");   
	        SeaBean seaBean = (SeaBean)context.getBean("pair");  
	        seaBean.setName("LIQF");
	        //System.out.println("testService.getPaire().getName()===" + testService.getPaire().getName());   
	        //TestServiceImpl testService = new TestServiceImpl();  
	  
	        System.out.println("1--第一次查找并创建cache");   
	        
	        List<SeaBean> restList1 = testService.getAllObject(seaBean); 
	        for(SeaBean seaBeanTem : restList1){
	        	System.out.println("strTem1===" + seaBeanTem.getName()); 
	        }
	        //paire = new com.liqf.other.Paire();
	        SeaBean seaBean2 = (SeaBean)context.getBean("pair");  
	        seaBean2.setName("LIQF77");
	        System.out.println("2--在cache中查找");
	        System.out.println(seaBean2.getName());
	        List<SeaBean> restList = testService.getAllObject(seaBean2);   
	        for(SeaBean seaBeanTem : restList){
	        	System.out.println("strTem2===" +  seaBeanTem.getName()); 
	        }
	           
	        System.out.println("3--remove cache");   
	        testService.updateObject(null);   
	           
	        //System.out.println("4--需要重新查找并创建cache");   
	        List<SeaBean> restList2 = testService.getAllObject(seaBean);   
	        for(SeaBean seaBeanTem : restList2){
	        	System.out.println("strTem3===" +  seaBeanTem.getName()); 
	        }


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics