ehcache 缓存配置

Ehcache 配合struts使用

import net.sf.ehcache.CacheManager;

import org.apache.struts.action.ActionServlet;
import org.apache.struts.action.PlugIn;
import org.apache.struts.config.ModuleConfig;

public class EhcachePlugIn implements PlugIn{
	
	private String xmlPath;

	public String getXmlPath() {
		return xmlPath;
	}

	public void setXmlPath(String xmlPath) {
		this.xmlPath = xmlPath;
	}

	public void destroy() {
		//System.out.println("Destroy EhcachePlugIn~~~~~~~~~~~~~~~~~~~");
		cacheManager.shutdown();
	}
	//初始化cache
	public void init(ActionServlet arg0, ModuleConfig arg1) throws ServletException {
		System.out.println("Init EhcachePlugIn~~~~~~~~~~~~~~~~~~~");
		URL url = getClass().getResource("/"+xmlPath);
		cacheManager = new CacheManager(url);
	}
	
	public static CacheManager getCacheManager()
	{
		return cacheManager;
	}
	
	private static CacheManager cacheManager;

}

 struts 配置文件中配置插件

<plug-in className="com.chinaclick.plugin.EhcachePlugIn">
  	<set-property value="xmlPath" property="ehcache.xml"/>
</plug-in>

 在根目录创建 ehcache.xml 配置文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd">
	<diskStore path="c:\\temp" />
	<cacheManagerEventListenerFactory class="" properties="" />


    <!-- 默认二级缓存 -->
	<defaultCache
	    name="MinuteCache"
		maxElementsInMemory="200"
		maxElementsOnDisk="1000" 
		eternal="false"
        timeToIdleSeconds="600"
		overflowToDisk="true"
	>
	</defaultCache>
	
	
    <!-- 永不过期的二级缓存 -->
	<cache
		name="sampleCache"
		maxElementsInMemory="100"
		maxElementsOnDisk="1000" 
		eternal="true"
        timeToIdleSeconds="2000" 
		overflowToDisk="true"
	>
	</cache>
	
	<!-- 五分钟失效的二级缓存 -->
	<cache
		name="fiveMinuteCache"
		maxElementsInMemory="200"
		maxElementsOnDisk="1000" 
		eternal="false"
        timeToIdleSeconds="300" 
		overflowToDisk="true"
	>
	</cache>
	
	<!-- 十分钟失效的二级缓存 -->
	<cache
		name="tenMinuteCache"
		maxElementsInMemory="200"
		maxElementsOnDisk="1000" 
		eternal="false"
        timeToIdleSeconds="600" 
		overflowToDisk="true"
	>
	</cache>
</ehcache>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值