<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="true" monitoring="autodetect">
updateCheck:是否检测更新,如果设置为true,它会到www.terracotta.org检测是否有新的包
monitoring: determines whether the CacheManager should
automatically register the SampledCacheMBean with the system MBean server.这个只是在使用Terracotta clustering才用到的,不是的话可以设置为off
测试代码
import net.sf.ehcache.CacheManager;
public class Test {
public void test() {
CacheManager manager = CacheManager.create();
net.sf.ehcache.Cache cache = manager.getCache("sampleCache1");
net.sf.ehcache.Element element = cache.get("date");
if(element==null){
element = new net.sf.ehcache.Element("date","aaaaaaaa");
cache.put(element);
//cache.flash();(对于cacahe.flash()下面要单独说明)
System.out.println("新的数据"+element.getValue());
}
else{
System.out.println("旧的数据"+element.getValue());
}
}
}
cacahe.flash() 有些文档说,只有程序显示调用cacahe.flash() ,才会将缓存写入到磁盘,这是错误的认识
其实cacahe.flash()是将内存中的缓存写入到磁盘,并将内存中的缓存清除,程序中并不需要显示的调用,
也就是说如果你设置了maxElementsInMemory的值大于0,同时程序中又显示调用flash(),那么内存中的数据将永远被清除,总是读取新的数据