ehcache配置备忘笔记
2010-11-04 15:04:17| 分类:
ehcache
| 标签:
|举报
|字号大中小 订阅
ehcache被hibernate用作默认的缓存工具,想必就是个好东西。配置使用都还蛮简单。
其官网配置说明见:http://ehcache.org/documentation/configuration.html
配置:
<cache
name="mmsIds"
maxElementsInMemory="9000000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="86400"
timeToLiveSeconds="86400"
diskPersistent="true"
memoryStoreEvictionPolicy="LFU"
/>
这里仅说说diskPersistent,这个如果设置为false,那么你的程序缓存了内容后,如果程序关闭了,那么缓存的东西就丢失了,因为这个如果设置为false,那么就不会持久化到硬盘,如果要下次启动程序还能访问上次的缓存那么就需要设置为true。
使用:
CacheManager manager=null;
Cache cache=null;
try {
manager=CacheManager.create(new FileInputStream(new File("./ehcache.xml")));
} catch (CacheException e) {
throw new RuntimeException("EHCache缓存构建失败:"+e.getMessage(),e);
} catch (FileNotFoundException e) {
throw new RuntimeException("EHCache配置文件未找到:"+e.getMessage(),e);
}
cache = manager.getCache("mmsIds");
String key="zhangshan";
int score=90;
cache.put(new Element(key,score));
//red
int score=Integer.parseInt(cache.get(zhangshan).getValue().toString());
评论这张
转发至微博
转发至微博
评论