简介: EhCache是一个纯java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CachePRoviderOSCache。
适用场所:
特点:
FIFO:first in first out,这个是大家最熟的,先进先出。
LFU:Less Frequently Used,一直以来最少被使用的将被清除,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
LRU:Least Recently Used,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存
2.缓存数据存储位置有两级:内存和磁盘,因此无需担心容量问题
3.缓存数据会在虚拟机重启的过程中写入磁盘
深层原理: http://raychase.VEvb.com/blog/1545906,四火同学的讲解释的超详细 相关配置:
1.通过自己编码方式使用
xml文件配置:
<ehcache> <!-- 指定一个文件目录,当EHCache把数据写到硬盘上时,将把数据写到这个文件目录下 --> <diskStore path="java.io.tmpdir"/> <!-- 设定缓存的默认数据过期策略 --> <defaultCache maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="0" timeToLiveSeconds="0" diskPersistent="false" diskExpiryThreadIntervalSeconds="120"/> <cache name="TESTCACHE" maxElementsInMemory="1000" eternal="true" overflowToDisk="true"/> </ehcache>
ehcache.xml元素的属性: name:缓存名称
maxElementsInMemory:内存中最大缓存对象数
maxElementsOnDisk:硬盘中最大缓存对象数,若是0表示无穷大
eternal:true表示对象永不过期,此时会忽略timeToIdleSeconds和timeToLiveSeconds属性,默认为false
overflowToDisk:true表示当内存缓存的对象数目达到了maxElementsInMemory界限后,会把溢出的对象写到硬盘缓存中。注意:如果缓存的对象要写入到硬盘中的话,则该对象必须实现了Serializable接口才行。
diskSpoolBufferSizeMB:磁盘缓存区大小,默认为30MB。每个Cache都应该有自己的一个缓存区。
diskPersistent:是否缓存虚拟机重启期数据
diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认为120
timeToIdleSeconds: 设定允许对象处于空闲状态的最长时间,以秒为单位。当对象自从最近一次被访问后,如果处于空闲状态的时间超过了timeToIdleSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清空。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地处于空闲状态
timeToLiveSeconds:设定对象允许存在于缓存中的最长时间,以秒为单位。当对象自从被存放到缓存中后,如果处于缓存中的时间超过了 timeToLiveSeconds属性值,这个对象就会过期,EHCache将把它从缓存中清除。只有当eternal属性为false,该属性才有效。如果该属性值为0,则表示对象可以无限期地存在于缓存中。timeToLiveSeconds必须大于timeToIdleSeconds属性,才有意义
memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。
java编程代码:
//从classes目录查找ehcache.xml配置文件 CacheManager cacheManager = CacheManager.getInstance(); //从classes目录查找指定名称的配置文件 //CacheManager cacheManager = CacheManager.create(getClass().getResource("/ehcache.xml")); //根据配置文件获得Cache实例 Cache cache = cacheManager.getCache("TESTCACHE"); //清空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();
2.用于作为servlet缓存使用
在web.xml中的配置
<filter> <filter-name>SimplePageFragmentCachingFilter</filter-name> <filter-class> net.sf.ehcache.constructs.web.filter.SimplePageFragmentCachingFilter </filter-class> </filter> <filter-mapping> <filter-name>SimplePageFragmentCachingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
在ehcache.xml中的配置
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false" monitoring="on"> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="3600" timeToLiveSeconds="3600" overflowToDisk="true" diskPersistent="false" diskExpiryThreadIntervalSeconds="3600" memoryStoreEvictionPolicy="LRU"/> <cache name="SimplePageFragmentCachingFilter" maxElementsInMemory="10000" maxElementsOnDisk="100000" overflowToDisk="true" diskSpoolBufferSizeMB="5120" eternal="false" timeToIdleSeconds="86400" timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU" /> </ehcache>
此时需要额外导入jar包:ehcache-web-2.0.4.jar
下载地址: Ehcache 2.5.1 下载地址:http://sourceforge.net/projects/ehcache/files/ehcache/ehcache-2.5.1/
新闻热点
疑难解答