EHCache 初步使用指南

Ehcache详细解读 [url]http://raychase.iteye.com/blog/1545906[/url],可以了解更多的之知识。

EhCache 的主要特性有:
快速;
简单;
多种缓存策略;
缓存数据有两级:内存和磁盘,因此无需担心容量问题;
缓存数据会在虚拟机重启的过程中写入磁盘;
可以通过 RMI、可插入 API 等方式进行分布式缓存;
具有缓存和缓存管理器的侦听接口;
支持多缓存管理器实例,以及一个实例的多个缓存区域;
提供 Hibernate 的缓存实现;
等等 …
由于 EhCache 是进程中的缓存系统,一旦将应用部署在集群环境中,每一个节点维护各自的缓存数据,当某个节点对缓存数据进行更新,这些更新的数据无法在其它节点中共享,这不仅会降低节点运行的效率,而且会导致数据不同步的情况发生。

在ehcache中,缓存有2个失效相关的配置即 timeToLiveSeconds 和 timeToIdleSeconds , 在通常的解释中,
[b]前者表示一条缓存自创建时间起多少秒后失效,
后者表示一条缓存自最后读取或更新起多少秒失效。[/b]
在2个同时配置时可能时间计算就不那么简单了。 简单说来 任何一方为0,则以另一方时间为准。否则就以最短时间为准。

ehcache是这样计算失效时间的
1 如果 timeToIdleSeconds 不为0并且 timeToLiveSeconds 为0, 如果缓存未被读过,失效时间=timeToIdleSeconds
2 如果 timeToLiveSeconds 不为0,失效时间= timeToLiveSeconds +读取时间
3 否则 失效时间=min(timeToIdleSeconds, timeToLiveSeconds +读取时间)

这两个参数很容易误解,看文档根本没用,我仔细分析了ehcache的代码。结论如下:
1、timeToLiveSeconds的定义是:以创建时间为基准开始计算的超时时长;
2、timeToIdleSeconds的定义是:在创建时间和最近访问时间中取出离现在最近的时间作为基准计算的超时时长;
3、如果仅设置了timeToLiveSeconds,则该对象的超时时间=创建时间+timeToLiveSeconds,假设为A;
4、如果没设置timeToLiveSeconds,则该对象的超时时间=max(创建时间,最近访问时间)+timeToIdleSeconds,假设为B;
5、如果两者都设置了,则取出A、B最少的值,即min(A,B),表示只要有一个超时成立即算超时。
[color=red]Ehcache 中ehcache.xml 配置详解和示例[/color]
[url]http://www.360doc.com/content/11/0525/19/4154133_119352895.shtml[/url]
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!--timeToIdleSeconds 当缓存闲置n秒后销毁 -->
<!--timeToLiveSeconds 当缓存存活n秒后销毁 -->
<!--
缓存配置
name:缓存名称。
maxElementsInMemory:缓存最大个数。
eternal:对象是否永久有效,一但设置了,timeout将不起作用。
timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。
timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。
overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。
maxElementsOnDisk:硬盘最大缓存个数。
diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false.
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。
clearOnFlush:内存数量最大时是否清除。
-->
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="500"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="1200"
overflowToDisk="true" />
<cache name="com.Menu" maxElementsInMemory="150" eternal="false" timeToLiveSeconds="36000" timeToIdleSeconds="3600" overflowToDisk="true"/>
</ehcache>


[color=red]EHCache 初步使用指南[/color]
[url]http://nakata-yf.iteye.com/blog/23536[/url]
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"
/>

API也挺简单,如下的api:
CacheManager主要的缓存管理类,一般一个应用为一个实例,如下
CacheManager.create();也可以使用new CacheManager的方式创建
默认的配置文件为ehcache.xml文件,也可以使用不同的配置:
CacheManager manager = new CacheManager("src/config/other.xml");



缓存的创建,采用自动的方式
CacheManager singletonManager = CacheManager.create();  
singletonManager.addCache("testCache");
Cache test = singletonManager.getCache("testCache");


或者直接创建Cache
CacheManager singletonManager = CacheManager.create();  
Cache memoryOnlyCache = new Cache("testCache", 5000, false, false, 5, 2);
manager.addCache(memoryOnlyCache);
Cache test = singletonManager.getCache("testCache");


删除cache
CacheManager singletonManager = CacheManager.create();  
singletonManager.removeCache("sampleCache1");



在使用ehcache后,需要关闭
CacheManager.getInstance().shutdown() 


caches 的使用
Cache cache = manager.getCache("sampleCache1"); 



执行crud操作
Cache cache = manager.getCache("sampleCache1");  
Element element = new Element("key1", "value1");
cache.put(element);


update
Cache cache = manager.getCache("sampleCache1");  
cache.put(new Element("key1", "value1");
//This updates the entry for "key1"
cache.put(new Element("key1", "value2");


get Serializable
Cache cache = manager.getCache("sampleCache1");  
Element element = cache.get("key1");
Serializable value = element.getValue();


get non serializable
Cache cache = manager.getCache("sampleCache1");  
Element element = cache.get("key1");
Object value = element.getObjectValue();


remove
Cache cache = manager.getCache("sampleCache1");  
Element element = new Element("key1", "value1"
cache.remove("key1");



不过缓存还是基本上以配置方式为主
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值