Ehcache缓存框架

地址:http://ehcache.sourceforge.net

Kernel: ehcache.jar

Xml:ehcache.xml

Xml代码

1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
2. <diskStore path="c:\\temp" />
3. <cacheManagerEventListenerFactory class="" properties="" />
4.
5. <!--
6. Uncomment the following in a clustered configuration.
7. -->
8.
9. <!--<cacheManagerPeerProviderFactory
10. class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
11. properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,timeToLive=1"
12. propertySeparator=","
13. />
14. <cacheManagerPeerListenerFactory
15. class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
16. />-->
17.
18. <!--
19. Hibernate will use the defaultCache unless custom configurations are defined
20. below for individual domain objects, collection, queries, etc.
21. -->
22.
23. <defaultCache
24. maxElementsInMemory="10000"
25. eternal="false"
26. timeToIdleSeconds="600"
27. overflowToDisk="true"
28. >
29. <!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
30. <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
31. </defaultCache>
32.
33. <!--
34. The cache name is the same as the class name specified in your Hibernate
35. mapping file.
36. -->
37.
38. <cache
39. name="sampleCache"
40. maxElementsInMemory="5"
41. maxElementsOnDisk="100"
42. eternal="false"
43. timeToIdleSeconds="2"
44. timeToLiveSeconds="2"
45. overflowToDisk="true"
46. >
47. <!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
48. <bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
49. </cache>
50. </ehcache>

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="c:\\temp" />
<cacheManagerEventListenerFactory class="" properties="" />

<!--
Uncomment the following in a clustered configuration.
-->

<!--<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic,multicastGroupAddress=230.0.0.1,multicastGroupPort=4446,timeToLive=1"
propertySeparator=","
/>
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
/>-->

<!--
Hibernate will use the defaultCache unless custom configurations are defined
below for individual domain objects, collection, queries, etc.
-->

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="600"
overflowToDisk="true"
>
<!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
</defaultCache>

<!--
The cache name is the same as the class name specified in your Hibernate
mapping file.
-->

<cache
name="sampleCache"
maxElementsInMemory="5"
maxElementsOnDisk="100"
eternal="false"
timeToIdleSeconds="2"
timeToLiveSeconds="2"
overflowToDisk="true"
>
<!--<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory" />-->
</cache>
</ehcache>



以上ehcache.xml是ehcache的配置文件,并且存放在应用的classpath中。下面是对该XML文件中的一些元素及其属性的相关说明:



<diskStore>元素:指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下。



<defaultCache>元素:设定缓存的默认数据过期策略。



<cache>元素:设定具体的命名缓存的数据过期策略。



<cache>元素的属性



name:缓存名称。通常为缓存对象的类名(非严格标准)。



maxElementsInMemory:设置基于内存的缓存可存放对象的最大数目。



maxElementsOnDisk:设置基于硬盘的缓存可存放对象的最大数目。



eternal:如果为true,表示对象永远不会过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false;



timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态。



timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期。当对象过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义。



overflowToDisk:如果为true,表示当基于内存的缓存中的对象数目达到了maxElementsInMemory界限后,会把益出的对象写到基于硬盘的缓存中。



使用:
Java代码

1. CacheManager cacheManager = EhcachePlugIn.getCacheManager();
2. Cache cache = cacheManager.getCache("sampleCache");

CacheManager cacheManager = EhcachePlugIn.getCacheManager();
Cache cache = cacheManager.getCache("sampleCache");

EhcachePlugIn可以自定义为任何工厂,作用是返回一个CacheManager实例。


Java代码

1. cacheManager.getCache("sampleCache");

cacheManager.getCache("sampleCache");

参数为ehcache文件中<cache>元素的name属性。



引入ehcache.xml
Java代码

1. URL url = getClass().getResource("/"+xmlPath);
2. cacheManager = new CacheManager(url);

URL url = getClass().getResource("/"+xmlPath);
cacheManager = new CacheManager(url);

xmlPath为ehcache.xml在classpath下的具体路径。



对象的存储
Java代码

1. CacheManager cacheManager = EhcachePlugIn.getCacheManager();
2. Cache cache = cacheManager.getCache("sampleCache");
3. System.out.println("The Key In Cache?:"+cache.isKeyInCache(EHCACHE_KEY));
4. System.out.println("Cache is :"+cache);
5.
6. Element result = cache.get(EHCACHE_KEY);
7.
8. if(null==result)
9. {
10. System.out.println("No Data In Ehcache");
11. List list = new ArrayList();
12.
13. for(int i=20;i<50;i++)
14. {
15. Student student = new Student(26,"kook"+i);
16. list.add(student);
17. }
18. cache.put(new Element(EHCACHE_KEY,list));
19. cache.flush();
20.
21. result = cache.get(EHCACHE_KEY);
22. }
23.
24. List ehcacheList = (List)result.getValue();
25.
26. Iterator iter = ehcacheList.iterator();
27.
28. while (iter.hasNext()) {
29. Student element = (Student) iter.next();
30. System.out.println("Studeng name is:"+element.getName());
31. }

CacheManager cacheManager = EhcachePlugIn.getCacheManager();
Cache cache = cacheManager.getCache("sampleCache");
System.out.println("The Key In Cache?:"+cache.isKeyInCache(EHCACHE_KEY));
System.out.println("Cache is :"+cache);

Element result = cache.get(EHCACHE_KEY);

if(null==result)
{
System.out.println("No Data In Ehcache");
List list = new ArrayList();

for(int i=20;i<50;i++)
{
Student student = new Student(26,"kook"+i);
list.add(student);
}
cache.put(new Element(EHCACHE_KEY,list));
cache.flush();

result = cache.get(EHCACHE_KEY);
}

List ehcacheList = (List)result.getValue();

Iterator iter = ehcacheList.iterator();

while (iter.hasNext()) {
Student element = (Student) iter.next();
System.out.println("Studeng name is:"+element.getName());
}



注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。



详细代码在附件中,为一个完成的ECLIPSE PROJECT.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值