---
title: "EHcache 思考"
author: "scholers"
date: "2010-03-11T22:52:58Z"
tags: ["Bean", "Cache", "Spring", "XML"]
categories: ["企业架构"]
slug: "ehcache"
draft: false
lastmod: "2010-12-17T17:49:27Z"
id: 613694
url: "https://scholers.iteye.com/blog/613694"
---

问题一：缓存的命中率低。如代码所示，如果getAllObject查询参数是一个VO对象，那么当你用new每次创建的时候，缓存系统都认为不是同一个对象，导致缓存失败，直接从数据库查询； 但是当你用SPRING的载入bean的方式来加载bean的时候，默认方式，没有修改为多态模式，缓存可以找到，但是设置查询条件却没有用了； 那么这样条件下的命中率很低，缓存还有意义吗？ 下面的代码是设置缓存的例子： \[code="java"]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 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 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 restList2 = testService.getAllObject(seaBean); for(SeaBean seaBeanTem : restList2){ System.out.println("strTem3===" + seaBeanTem.getName()); }\[/code]