标签:
ehcache是一个用Java实现的使用简单,高速,实现线程安全的缓存管理类库,ehcache提供了用内存,磁盘文件存储,以及分布式存储方式等多种灵活的cache管理方案。同时ehcache作为开放源代码项目,采用限制比较宽松的Apache License V2.0作为授权方式,被广泛地用于Hibernate, Spring,Cocoon等其他开源系统。
Ehcache的类层次模型主要为三层,最上层的是CacheManager,他是操作Ehcache的入口。我们可以通过CacheManager.getInstance()获得一个单个的CacheManager,或者通过CacheManager的构造函数创建一个新的CacheManager。每个CacheManager都管理着多个Cache。而每个Cache都以一种类Hash的方式,关联着多个Elemenat。而Element则是我们用于存放要缓存内容的地方。
ehcache的刷新策略
ehcache的刷新策略是当缓存在放入的时候记录一个放入时间,它是用Lazy Evict的方式,在取的时候同设置的TTL比较
ehcache缓存的3种清空策略:
1 FIFO,先进先出
2 LFU,最少被使用,缓存的元素有一个hit属性,hit值最小的将会被清出缓存。
3 LRU,最近最少使用的,缓存的元素有一个时间戳,当缓存容量满了,而又需要腾出地方来缓存新的元素的时候,那么现有缓存元素中时间戳离当前时间最远的元素将被清出缓存。
事件处理
可以为CacheManager添加事件监听,当对CacheManager增删Cache时,事件处理器将会得到通知。要配置事件处理,需要通过ehcache的配置文件来完成。
可以为Cache添加事件监听,当对Cache增删Element时,事件处理器将会得到通知。要配置事件处理,需要通过ehcache的配置文件来完成。
ehcache参数配置:
maxInMemory - 设定内存中创建对象的最大值。
eternal - 设置元素(译注:内存中对象)是否永久驻留。如果是,将忽略超时限制且元素永不消亡。
timeToIdleSeconds - 设置某个元素消亡前的停顿时间。也就是在一个元素消亡之前,两次访问时间的最大时间间隔值。这只能在元素不是永久驻留时有效(译注:如果对象永恒不灭,则设置该属性也无用)。
如果该值是 0 就意味着元素可以停顿无穷长的时间。
timeToLiveSeconds - 为元素设置消亡前的生存时间。也就是一个元素从构建到消亡的最大时间间隔值。这只能在元素不是永久驻留时有效。
overflowToDisk - 设置当内存中缓存达到maxInMemory 限制时元素是否可写到磁盘上。
Cache-aside: 直接操作数据
Cache-as-sor:read-through、write-through和write-behind的结合
Read-through:
Write-through:
Write-behind:
• timeToLive
• timeToIdle
• maxElementsInMemory
• maxElementsOnDisk
• memory store eviciton policy
• CacheEventListeners can be added and removed dynamically []
当eternal属性为“true”时,timeToLive和timeToIdle会失效
This example shows how to dynamically modify the cache configuration of an already running cache:
Cache cache = manager.getCache("sampleCache");
CacheConfiguration config = cache.getCacheConfiguration();
config.setTimeToIdleSeconds(60);
config.setTimeToLiveSeconds(120);
config.setMaxElementsInMemory(10000);
config.setMaxElementsOnDisk(1000000);
Dynamic cache configurations can also be frozen to prevent future changes:
Cache cache = manager.getCache("sampleCache");
cache.disableDynamicFeatures();
The new cache attributes are:
• maxBytesOnHeap
• maxBytesOffHeap (formerly maxMemoryOffHeap)
• maxBytesOnDisk
甚至,还可以指定比例,如:maxBytesOnHeap="20%".
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.distribution.jgroups.JGroupsBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true"/>
DiskStoreBootstrapCacheLoaderFactory:从硬盘加载数据到堆栈
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.store.DiskStoreBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true"/>
TerracottaBootstrapCacheLoaderFactory:从terracotta的L2中加载数据
<bootstrapCacheLoaderFactory
class="net.sf.ehcache.store.TerracottaStoreBootstrapCacheLoaderFactory"
properties="bootstrapAsynchronously=true"/>
CacheConfiguration config = new CacheConfiguration("copyCache", 1000).copyOnRead(true)
Cache copyCache = new Cache(config);
默认此属性是false。
<cache name="copyCache"
maxElementsInMemory="10"
eternal="false"
timeToIdleSeconds="5"
timeToLiveSeconds="10"
copyOnRead="true"
copyOnWrite="true">
<copyStrategy class="com.company.ehcache.MyCopyStrategy"/>
</cache>
在ehcache启动的时候,可以设置缓存失效。命令行启动的时候如下:
java -Dnet.sf.ehcache.disabled=true
其他特殊的系统属性:
1)java-Dnet.sf.ehcache.use.classic.lru=true
当LRU被选中的时候,更老的LruMemoryStore实现策略将会被真正采用
1)通过系统参数:-Dnet.sf.ehcache.skipUpdateCheck=true
2)通过配置文件: <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd"
updateCheck="false" monitoring="autodetect"
dynamicConfig="true">
1) 内存存储
2) 非堆存储(大内存,企业ehcache才拥有)
3) 硬盘存储(两个版本:开源的和企业级ehcache)
其本质是使用java的LinkedHashMap来实现的。多线程安全、内存管理安全、速度快
Terracotta BigMemory是一个新增的功能,它允许系统占用堆以外的内存,速度是硬盘存储的100倍,允许很大的存储空间被创建(350G被测试过)
因为非堆数据是以字节流的形式存储,所以要求Element的key和value都要是可以序列化的。
因为序列化和反序列化的过程,这种存储形式比内存存储慢10倍
线程安全的
Considerations for guidance on how to safely shut the Virtual Machine down.
When a DiskStore is persisted, the following steps take place:
On startup the following steps take place:
把一个拥有8G机器内存的存储分配成各种存储。设想有一个7G的数据集,共7M个元素,每个元素1k大小。
我们设置1G的堆存储和7G的非堆存储:
java -Xms1G -Xmx1G -XX:maxDirectMemorySize=7G
对应的配置文件为:
<cache
maxElementsInMemory=100
overflowToOffHeap="true"
maxMemoryOffHeap="7G"
... />
• Glassfish V2/V3
• Tomcat 6
• Jetty 6
Tomcat 6通过了所有的继集成测试
支持Weblogic10.3.2,但是SOAP不兼容。
缓存属性:
缓存配置。
以下属性是必须的:
name - cache的标识符,在一个CacheManager中必须唯一
maxElementsInMemory - 在内存中缓存的element的最大数目
maxElementsOnDisk - 在磁盘上缓存的element的最大数目
eternal - 设定缓存的elements是否有有效期。如果为true,timeouts属性被忽略
overflowToDisk - 设定当内存缓存溢出的时候是否将过期的element缓存到磁盘上
以下属性是可选的:
timeToIdleSeconds - 缓存element在过期前的空闲时间。默认为0,表示可空闲无限时间. (如果指定了这个时间,是否在被hit的前超过了这个时间就会被remove?在内存缓存数目超限之前不会被remove)
timeToLiveSeconds - 缓存element的有效生命期。这个类似于timeouts,默认为0,不过期(是否通常情况下应该大于等于timeToIdleSeconds,小于会如何?idle时间也会减小和这个数值一样)
diskPersistent - 在VM重启的时候是否持久化磁盘缓存,默认是false。
(测试一下true的情况?重载vm的时候会从磁盘进行序列化到对象)
diskExpiryThreadIntervalSeconds - 磁盘缓存的清理线程运行间隔,默认是120秒. (测试一下0的时候会如何)
memoryStoreEvictionPolicy - 当内存缓存达到最大,有新的element加入的时候,移除缓存中element的策略。默认是LRU,可选的有LFU和FIFO可对缓存中的element配置诸如监听器和加载器。Ehcahe内建了一些
*cacheEventListenerFactory - 监听缓存中element的put, remove, update和expire事件
*bootstrapCacheLoaderFactory - 启动时加载缓存的element每个用来做分布式缓存都必须设定element的事件监听器,用来在各个CacheManager节点复制消息。
Ehcache内建了基于RMI的实现 - RMICacheReplicatorFactory
http://www.cnblogs.com/xiaoniba1024/archive/2013/04/15/3022707.html
标签:
原文地址:http://www.cnblogs.com/softidea/p/5147051.html