Ehcahe操作无效,希望各位能帮忙解决

10-12-14 tianhandigeng
我从xml中读取信息以后,放入到ehcache中,再次读取的时候,如果存在之前放入的key就从cache中读取,如果不存在就从xml文件中读取,代码是这样的

public Object get(Class classObj, String nodeName, String fileName) {

Object obj = null;

if (ehcacheVindicator.getCache().isKeyInCache(nodeName)) {

Element element = ehcacheVindicator.getCache().get(nodeName);

if (ehcacheVindicator.getCache().isExpired(element))

obj = readObject(classObj, fileName, nodeName);// read object

// from xml

// file

else

obj = getObject(nodeName); // get object from cache

} else {

obj = readObject(classObj, fileName, nodeName); // read object from

// xml file

addObject(nodeName, obj); // add object to cache

}

return obj;

}

创建cache是这样的

try {

this.cacheManager = CacheManager.create();

cache = cacheManager.getCache(cacheName);

} catch (Exception e) {

System.err

.println("Erroring creating CacheManager or isn't exisit the specifid cache");

}

测试代码是这样的:

public static void main(String[] args) {

// TODO Auto-generated method stub

EhcacheVindicatorProxy proxy=new EhcacheVindicatorProxy("menu");

List list=(List)proxy.get(FrontMenu.class, "frontmenus", "menus.xml");

for(int i=0;i<list.size();i++){

FrontMenu front=(FrontMenu)list.get(i);

System.out.println(front.getName());

System.out.println(front.getHref());

System.out.println(front.getDisplay());

}

}

奇怪的是每次读取都还是从xml文件中读取,压根就不从cache中读取,每次遮掩

this.cacheManager = CacheManager.create();

cache = cacheManager.getCache(cacheName);

System.out.println(cache.getSize());

输出cache的缓存都是0,也就是说没放入进去,但是上面第一段代码 第一次执行的时候我已经这样操作了

obj = readObject(classObj, fileName, nodeName); // read object from

// xml file

addObject(nodeName, obj); // add object to cache

我addObject了,进而操作就是cache.put(nodeName,obj);,这已经是放入了,为什么我第二次读取的时候cache还是空,还是从xml中读取?希望高手指教一下

猜你喜欢