EHCache的使用随记

1、在src目录下(WEB-INF/classes目录下)创建一个新文件ehcache.xml,内容如下:

<?xml version="1.0" encoding="gbk"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
<diskStore path="java.io.tmpdir"/>

<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
maxElementsOnDisk="10000000"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" />

<!-- 监听本服务器的端口 -->
<cacheManagerPeerListenerFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
properties="hostName=192.168.1.103, port=40002,socketTimeoutMillis=2000" />

<!-- 同步另外一个服务器的cache -->
<cacheManagerPeerProviderFactory
class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
properties="peerDiscovery=manual,rmiUrls=//192.168.1.103:40001/userCache" />

<!--
必须的属性:
name:设置缓存的名称,用于标志缓存,惟一
maxElementsInMemory:在内存中最大的对象数量
maxElementsOnDisk:在DiskStore中的最大对象数量,如为0,则没有限制
eternal:设置元素是否永久的,如果为永久,则timeout忽略
overflowToDisk:是否当memory中的数量达到限制后,保存到Disk
可选的属性:
timeToIdleSeconds:设置元素过期前的空闲时间
timeToLiveSeconds:设置元素过期前的活动时间
diskPersistent:是否disk store在虚拟机启动时持久化。默认为false
diskExpiryThreadIntervalSeconds:运行disk终结线程的时间,默认为120秒
memoryStoreEvictionPolicy:策略关于Eviction
缓存子元素:
cacheEventListenerFactory:注册相应的的缓存监听类,用于处理缓存事件,如put,remove,update,和expire
bootstrapCacheLoaderFactory:指定相应的BootstrapCacheLoader,用于在初始化缓存,以及自动设置。
-->
<cache
name="userCache"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120">
<cacheEventListenerFactory
class="net.sf.ehcache.distribution.RMICacheReplicatorFactory" />
</cache>
</ehcache>


2、将ehcache-1.5.0.jar和backport-util-concurrent.jar放到lib目录下

3、测试代码

import java.util.ArrayList;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

public class EHCacheTest {

//创建cache的管理
private static CacheManager manager = CacheManager.create();
//得到ehcache中的userCache(在XML中定义的那个名称)
private static Cache cache = manager.getCache("userCache");

/**
* 将值加入到cache里
* @param param
*/
@SuppressWarnings("unchecked")
public static void addCache(String param){
ArrayList<String> list = null;
if(cache.get("list") != null){
Element element = cache.get("list");
if(element != null){
list = (ArrayList<String>)element.getValue();
list.add(param);
}
cache.put(element);
}
else{
list = new ArrayList<String>();
list.add(param);
Element element = new Element("list", list);
cache.put(element);
}
}

/**
* 展示cache中内容
*/
@SuppressWarnings("unchecked")
public static void showCache(){
if(cache.get("list") != null){
Element element = cache.get("list");
if(element != null){
ArrayList<String> list = (ArrayList<String>)element.getValue();
System.out.println("cacheList : " + list.toString());
}
}
}

public static void main(String[] args) {
addCache("param1");
addCache("param2");
showCache();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值