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>