Ehcache的简单介绍和使用
2017-04-15 09:54
316 查看
ehcache是一个非常轻量级的缓存实现,而且从1.2之后就支持了集群,而且是hibernate默认的缓存provider。EhCache 是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。
Ehcache的分布式缓存有传统的RMI,1.5版的JGroups,1.6版的JMS。分布式缓存主要解决集群环境中不同的服务器间的数据的同步问题。
使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存。
CachingFilter功能可以对HTTP响应的内容进行缓存。
1、主要特性
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
2、配置文件介绍(普通缓存)
<?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:内存数量最大时是否清除。
-->
3、配置文件介绍(分布式缓存)
1)RMI集群模式
A、手工发现
需要指定节点发现模式peerDiscovery值为manual,rmiUrls设置为另一台服务器的IP、端口和缓存名等信息
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//192.168.0.12:4567/oschina_cache|//192.168.0.13:4567/oschina_cache" />
B、自动发现
需要指定节点发现模式peerDiscovery值为automatic自动,同时组播地址可以指定D类IP地址空间,范围从 224.0.1.0 到 238.255.255.255 中的任何一个地址。
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32" />
需要在每个cache属性中加入
<cacheEventListenerFactory
<cache name="demoCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
4、通过编程方式使用EhCache
Java代码
//从classes目录查找ehcache.xml配置文件
CacheManager cacheManager = CacheManager.getInstance();
//从classes目录查找指定名称的配置文件
//CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml"));
//根据配置文件获得Cache实例
Cache cache = cacheManager.getCache("CACHE1");
//清空Cache中的所有元素
cache.removeAll();
//往Cache中添加元素
cache.put(new Element("s1", "11111"));
cache.put(new Element("s2", "22222"));
cache.put(new Element("s3", "33333"));
//从Cache中取得元素
Element e = cache.get("s3");
System.out.println(e.getValue());
//卸载缓存管理器
cacheManager.shutdown();
5、页面缓存
在web.xml文件中配置过滤器。此处对test_tag.jsp页面进行缓存。
<filter>
<filter-name>testPageCachingFilter</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>testPageCachingFilter</filter-name>
<url-pattern>/test_tag.jsp</url-pattern>
</filter-mapping>
在ehcache.xml文件中配置Cache节点。注意:cache的name属性必需为SimplePageCachingFilter。
<cache name="SimplePageCachingFilter"
maxElementsInMemory="10"
overflowToDisk="true"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
memoryStoreEvictionPolicy="LFU" />
Ehcache的分布式缓存有传统的RMI,1.5版的JGroups,1.6版的JMS。分布式缓存主要解决集群环境中不同的服务器间的数据的同步问题。
使用Spring的AOP进行整合,可以灵活的对方法的返回结果对象进行缓存。
CachingFilter功能可以对HTTP响应的内容进行缓存。
1、主要特性
1. 快速.
2. 简单.
3. 多种缓存策略
4. 缓存数据有两级:内存和磁盘,因此无需担心容量问题
5. 缓存数据会在虚拟机重启的过程中写入磁盘
6. 可以通过RMI、可插入API等方式进行分布式缓存
7. 具有缓存和缓存管理器的侦听接口
8. 支持多缓存管理器实例,以及一个实例的多个缓存区域
9. 提供Hibernate的缓存实现
2、配置文件介绍(普通缓存)
<?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:内存数量最大时是否清除。
-->
3、配置文件介绍(分布式缓存)
1)RMI集群模式
A、手工发现
需要指定节点发现模式peerDiscovery值为manual,rmiUrls设置为另一台服务器的IP、端口和缓存名等信息
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,
rmiUrls=//192.168.0.12:4567/oschina_cache|//192.168.0.13:4567/oschina_cache" />
B、自动发现
需要指定节点发现模式peerDiscovery值为automatic自动,同时组播地址可以指定D类IP地址空间,范围从 224.0.1.0 到 238.255.255.255 中的任何一个地址。
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=automatic, multicastGroupAddress=230.0.0.1,
multicastGroupPort=4446, timeToLive=32" />
需要在每个cache属性中加入
<cacheEventListenerFactory
<cache name="demoCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true">
<cacheEventListenerFactory class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
</cache>
4、通过编程方式使用EhCache
Java代码
//从classes目录查找ehcache.xml配置文件
CacheManager cacheManager = CacheManager.getInstance();
//从classes目录查找指定名称的配置文件
//CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml"));
//根据配置文件获得Cache实例
Cache cache = cacheManager.getCache("CACHE1");
//清空Cache中的所有元素
cache.removeAll();
//往Cache中添加元素
cache.put(new Element("s1", "11111"));
cache.put(new Element("s2", "22222"));
cache.put(new Element("s3", "33333"));
//从Cache中取得元素
Element e = cache.get("s3");
System.out.println(e.getValue());
//卸载缓存管理器
cacheManager.shutdown();
5、页面缓存
在web.xml文件中配置过滤器。此处对test_tag.jsp页面进行缓存。
<filter>
<filter-name>testPageCachingFilter</filter-name>
<filter-class>net.sf.ehcache.constructs.web.filter.SimplePageCachingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>testPageCachingFilter</filter-name>
<url-pattern>/test_tag.jsp</url-pattern>
</filter-mapping>
在ehcache.xml文件中配置Cache节点。注意:cache的name属性必需为SimplePageCachingFilter。
<cache name="SimplePageCachingFilter"
maxElementsInMemory="10"
overflowToDisk="true"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="100"
memoryStoreEvictionPolicy="LFU" />
相关文章推荐
- Ehcache使用简单介绍
- linux file.src.rpm 使用方法的简单介绍
- 关于使用TreeView 控件的简单介绍
- 使用技巧:简单介绍JSP数据库高级操作
- Lucene的使用以及简单介绍
- Asp.net 2.0中Themes的使用简单介绍
- 通过一个简单的例子,介绍如何使用LDAP(转)
- Linux curl使用简单介绍
- Linux curl使用简单介绍
- Eclipse中使用MSN插件的简单介绍
- JavaBean的简单介绍与使用
- MFC控件 状态条的简单介绍及使用 CStatusBar
- 简单介绍vss工具command操作使用
- AJAX学习基础:简单介绍数据岛使用方法
- PDF iText 使用简单介绍
- Oracle游标使用的简单介绍
- [转]Java的开源项目:简单介绍Log4J的使用
- 简单介绍数据岛使用方法
- 【ABAP】LSMW使用IDoc方法的简单介绍
- Gravatar 全球通用头像简单介绍与使用教程