想用缓存又不想用oscache觉得
麻烦,自己写了个简单的缓存,不小心发现getAllCity结果总是在第一次进去的时候数量加倍,第二次就好了,真实怪事.
代码如下:
public static List getAllCity(boolean bInit) throws XXException {
List cityList = new ArrayList();
if (null == CityInfos || CityInfos.size() == 0) bInit = true;
synchronized (CityInfos) {
if (bInit) { //retrieve city info from database
CityService service = new CityService();
cityList = service.getAllCitys();
CityInfos.clear();
if (null != cityList && cityList.size() > 0) {
log.info("========" + "cityList size : " + cityList.size() + "========");
for (Iterator it = cityList.iterator(); it.hasNext();) {
CityVo cityVo = (CityVo) it.next();
CityInfos.put(cityVo.getId(), cityVo);
}
}
}
cityList.addAll(CityInfos.values());
}
return cityList;
}
CityInfos是一个static Hashtable,查了下原来是共用了cityList,将
synchronized 里面的cityList 改成
List tmpCityList 就可以了 :) 为什么会犯低级错误呢? :(