Ehcache入门详解
一 基本概念
Ehcache是一个快速的、轻量级(体量小、依赖少)、易于使用、进程内的缓存。他支持read-only和read/write缓存,内存和磁盘缓存。是一个非常轻量级的缓存实现,支持集群。
主要特点:
1.快速、简单
2.提供多种(LRU、LFU、FIFO)缓存策略
3.缓存数据有两级:内存和磁盘,因此无需担心容量问题
4.缓存数据会在虚拟机重启的过程中写入磁盘
5.支持分布式缓存
6.支持多缓存管理实例(CacheManager),以及一个市里的多个缓存区域(cache)
二 引入
官网地址:http://www.ehcache.org
1.项目中添加ehcache的jar包
<!-- https://mvnrepository.com/artifact/net.sf.ehcache/ehcache --> <dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.3</version> </dependency>
2.将ehcache.xml放到classpath中
如果你是调用了CacheManager默认构造方法去创建CacheManager的实例,此方法会到classpath中找ehcache.xml文件,如果没有找到,他会找ehcache-failsafe.xml文件,这个文件包含在ehcache-core.jar包中,所以肯定可以找到。
ehcache-failsafe.xml提供了一个非常简单的默认配置,这样可以使用户在没有创建ehcache.xml的情况下使用Ehcache
三 配置
配置ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path="java.io.tmpdir"/> <cache name="sampleCache1" //缓存名称(唯一)
maxEntriesLocalHeap="10000"
maxEntriesLocalDisk="1000"
eternal="false"
overflowToDisk="true"
diskPersistent="true"
diskSpoolBufferSizeMB="20"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
memoryStoreEvictionPolicy="LFU"
diskExpiryThreadIntervalSeconds ="120"
transactionalMode="off">
<bootstrapCacheLoaderFactory class="net.sf.ehcache.distribution.RMIBootstrapCacheLoaderFactory"/>
<cacheEventListenerFactory
class="com.ehcache.EhcacheListenerTest"
properties="wxwtestkey=200,wxtestkey2=300"/>
<persistence strategy="localTempSwap"/> </cache> </ehcache>
配置项详解:
<diskStore path="java.io.tmpdir"/>
diskStore元素是可选的。磁盘存储配置,如果没有配置这个元素,当一个需要磁盘存储的缓存被创建时,会报告一个警告,并且java.io.tmpdir临时文件夹会被自动使用,作为磁盘存放.data/.index文件的目录。
1.user.home 用户home目录:C:\Users\Winclpt
2.user.dir 用户当前工作目录: G:\GwssiProject\product\product
3.java.io.tmpdir 默认的临时文件目录:C:\Users\Winclpt\AppData\Local\Temp\
子目录可以这样定义:java.io.tmpdir/mld
defaultCache:默认缓存配置
cache节点
必须属性:
name:设置缓存的名字,用于标志缓存,唯一性
maxElementsInMemory:在内存中缓存的element的最大数目
maxElementsOnDisk:在磁盘上缓存的element的最大数目
eternal:设置element是否为永久的,如果为true,则timeout将被忽略
overflowToDisk:是否当内存中的element数量达到最大值限制后,保存到磁盘
可选属性:
timeToIdleSeconds:element在失效前允许的闲置时间(多少时间没被用就失效了)。默认值0,也就是可闲置时间无穷大
timeToLiveSeconds:element在失效前允许存活时间(不管有没有被用,时间到了就失效,只能存活这么久)
diskPersistent:是否在磁盘上持久化。指重启jvm后,数据是否有效。默认为false。
diskExpiryThreadIntervalSeconds:检查磁盘上的缓存超期的线程的运行周期
memoryStoreEvictionPolicy:当内存中element到达最大限制时,将会按照设置的策略去清理内存
三种清空策略:
FIFO:先进先出
LFU:less frequently used,一直以来最少被使用的。缓存的element有一个hit属性,hit值最小的将被清除
LRU:least recently used,最近最少使用的。缓存的element有一个时间戳,当缓存容量满了,那么现有缓存元素中时间戳离当前时间最远的元素将被清除
四 使用
Ehcache的类层次结构

Ehcache基本用法
1.获取manager,如果ehcache.xml不是在src下,可以由create("config/ehcache.xml")来指定路径
CacheManager manager = CacheManager.create();
2.获取缓存区,根据ehcache.xml中配置的cache节点,指定name来获取cache
Cache cache = manager.getCache("sampleCache");
3.创建key-value形式的元素element,存入缓存区cache,或从缓存区取出
Element element = new Element("key","value");
cache.put(element);
4.关闭manager
manager.shutdown();
Ehcache还提供了监听机制
<cacheEventListenerFactory //Cache节点子节点 class="com.ehcache.EhcacheListenerTest" properties="wxwtestkey=200,wxtestkey2=300"/>
其中class是自定义类,继承CacheEventListenerFactory,覆盖createCacheEventListener(Properties props)方法,此方法返回一个CacheEventListener对象,返回的这个对象需要我们继承接口CacheEventListener实现里面的方法。
通过监听,在对cache做操作的时候,调用相应的监听类方法。
五 总结
1.一个ehcache.xml对应一个CacheManager,不同的缓存应对应不同的path,否则会报错
2.持久化可在Cache节点配置diskPersistent属性,如果配置false,在CacheManager.shutdown()或startup后,用来缓存元素的文件将被清除掉。如果设置为true,data和index文件将会被保存下来,存取必须显示调用cache.flush()才会将数据缓存到磁盘中,如果想JVM重启后还能从磁盘中读取之前缓存的数据必须在Cache节点中配置bootstrapCacheLoaderFactory子节点
3.cache.getSize()得到缓存中元素总个数;cache.getMemoryStoreSize()得到内存中元素个数;cache.getDiskStoreSize()得到磁盘中元素个数
4.对于想存储数据到磁盘,或者集群时复制到其他缓存区域的数据,必须序列化。如果没有那么在进行上述操作时会被丢弃,且没有报错。
5.当Ehcache刚启动时,ehcache提供一个机制可以先加载数据:BootstrapCacheLoader

浙公网安备 33010602011771号