posts - 2,  comments - 0,  trackbacks - 0

EHCache 初步使用指南- -

1. EHCache 的特点,系统要求及安装
是一个纯Java ,过程中(也可以理解成插入式)缓存实现
Hibernate2.1,Spring支持EHcache嵌入,自我感觉Hibernate2.1 + EHCache 很过瘾。。(测试结果比Hibernate+JCS 好多了)
支持多CPU服务器
其版本发布前进行了产品级测试
支持:运行环境jdk1.2到5版本(源代码编译需1.4或1.5 版本)
jdk1.4和1.5 版本,需加入apache 的 commons-logging类库 http://jakarta.apache.org/commons/logging.html
jdk1.2和1.3 版本,需加入commons-collections 2.1 版本 http://jakarta.apache.org/commons/collections.html
和 xerces (xml-apis.jar and xercesImpl.jar), 2.5 版本 http://xml.apache.org/xerces2-j/

如果运行环境为IBM JDK1.4版本上的IBM Websphere 5.1,也需加入 commons-collections 类库(如需要)

单独安装Ehcache ,需把ehcache-X.X.jar 和相关类库方到classpath中。
如项目已安装了Hibernate2.1 ,则不需要做什么。。直接可以使用Ehcache

Cache 存储方式 :内存或磁盘

2. 单独使用 EHCache

使用CacheManager 创建并管理Cache
使用默认配置文件创建
CacheManager manager = CacheManager.create();
使用指定配置文件创建
CacheManager manager = CacheManager.create("src/config/ehcache.xml");
从classpathq找寻配置文件并创建
URL url = getClass().getResource("/anothername.xml");
CacheManager manager = CacheManager.create(url);
通过输入流创建
InputStream fis = new FileInputStream(new File("src/config/ehcache.xml").getAbsolutePath());
try {
manager = CacheManager.create(fis);
} finally {
fis.close();
}

卸载CacheManager ,关闭Cache

manager.shutdown();

使用Caches

取得配置文件中预先 定义的sampleCache1设置,生成一个Cache
Cache cache = manager.getCache("sampleCache1");

设置一个名为test 的新cache,test属性为默认
CacheManager manager = CacheManager.create();
manager.addCache("test");

设置一个名为test 的新cache,并定义其属性
CacheManager manager = CacheManager.create();
Cache cache = new Cache("test", 1, true, false, 5, 2);
manager.addCache(cache);

往cache中加入元素
Element element = new Element("key1", "value1");
cache.put(new Element(element);

从cache中取得元素
Element element = cache.get("key1");

3. 在 Hibernate 中运用EHCache

hibernate.cfg.xml中需设置如下:
2.1版本加入
net.sf.ehcache.hibernate.Provider
2.1以下版本加入
net.sf.hibernate.cache.EhCache

在 Hibernate 映射文件的每个需要Cache的Domain中

name="com.somecompany.someproject.domain.Country"
table="ut_Countries"
dynamic-update="false"
dynamic-insert="false"
]
...

加入类似如下格式信息:

比如:

然后在ehcache.xml中加入

maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="false"
/]

posted on 2009-02-12 10:43 lvjava 阅读(931) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(1)

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜