EHCache

1、首先设置EhCache,导入相应的jar包。

2、建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下。

3、配置ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
	<diskStore path="java.io.tmpdir"/>
	<defaultCache
		maxElementsInMemory="10000"
		eternal="false"
		overflowToDisk="true"
		timeToIdleSeconds="120"
		timeToLiveSeconds="120"
		diskPersistent="false"/>
		
	<cache name="relationOfNameAndId"
		maxElementsInMemory="10000"
		eternal="true"
		overflowToDisk="true"
		timeToIdleSeconds="120"
		timeToLiveSeconds="120"
		diskPersistent="false"/>
		
	<cache name="mouseOverInfo"
		maxElementsInMemory="10000"
		eternal="true"
		overflowToDisk="true"
		timeToIdleSeconds="120"
		timeToLiveSeconds="120"
		diskPersistent="false"/>
</ehcache>

参数说明:
如果定义了name属性,在使用cache时按照name属性匹配。如果没有定义name属性,则按照默认的配置。

maxElementsInMemory :cache 中最多可以存放的元素的数量。如果放入cache中的元素超过这个数值,有两种情况:
1. 若overflowToDisk的属性值为true,会将cache中多出的元素放入磁盘文件中。
2. 若overflowToDisk的属性值为false,会根据memoryStoreEvictionPolicy的策略替换cache中原有的元素。

eternal :是否永驻内存。如果值是true,cache中的元素将一直保存在内存中,不会因为时间超时而丢失,所以在这个值为true的时候,timeToIdleSeconds和timeToLiveSeconds两个属性的值就不起作用了。
 3. timeToIdleSeconds :访问这个cache中元素的最大间隔时间。如果超过这个时间没有访问这个cache中的某个元素,那么这个元素将被从cache中清除。
4. timeToLiveSeconds : cache中元素的生存时间。意思是从cache中的某个元素从创建到消亡的时间,从创建开始计时,当超过这个时间,这个元素将被从cache中清除。

5. overflowToDisk :溢出是否写入磁盘。系统会根据标签<diskStore path="java.io.tmpdir"/> 中path的值查找对应的属性值,如果系统的java.io.tmpdir的值是 D:\temp,写入磁盘的文件就会放在这个文件夹下。文件的名称是cache的名称,后缀名的data。如:CACHE_FUNC.data。
6. diskExpiryThreadIntervalSeconds  :磁盘缓存的清理线程运行间隔.
7. memoryStoreEvictionPolicy :内存存储与释放策略。有三个值:
LRU -least recently used
LFU -least frequently used
FIFO-first in first out, the oldest element by creation time

4、基本操作

CacheManager manager=CacheManager.create();取默认路径下的配置文件,src中的ehcache.xml;也可以定义多个配置文件,给出路径。

manager.addCache("mouseOverInfo");                                                        如过没有在配置文件中给出name,则直接动态的添加,并且可以配置参数。如果不配置参数使用默认值。                              

mouseOverInfoCache=manager.getCache("mouseOverInfo");                如果在配置文件中定义了name属性,则直接get
mouseOverInfoCache.put(new Element(a.getKey(), modelName));

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值