ehcache 缓存管理工具
ehcache
ehcache.xml
<ehcache><diskStore path="java.io.tmpdir" /><defaultCache maxElementsInMemory="10000" eternal="false"timeToIdleSeconds="1800" timeToLiveSeconds="1800"overflowToDisk="false" /><cache name="shortcache" maxElementsInMemory="10000" eternal="false"overflowToDisk="false" timeToIdleSeconds="600"timeToLiveSeconds="600" /><cache name="middlecache" maxElementsInMemory="50000"eternal="false" overflowToDisk="false" timeToIdleSeconds="1800"timeToLiveSeconds="1800" /><cache name="longcache" maxElementsInMemory="10000" eternal="false"overflowToDisk="false" timeToIdleSeconds="3600"timeToLiveSeconds="3600" /><cache name="morelongcache" maxElementsInMemory="50000"eternal="false" overflowToDisk="false" timeToIdleSeconds="864000"timeToLiveSeconds="864000" /></ehcache>
web.xml 配置监听
<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>com.dadi.oa.init.InitListener</listener-class></listener>
InitListener 类 监听初始化类
package com.dadi.oa.init;import java.util.ArrayList;import java.util.List;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class InitListener implements ServletContextListener {private static List<InitHandler> initHandlerList=new ArrayList<InitHandler>();static {initHandlerList.add(new InitEhcacheHandler());}public void contextDestroyed(ServletContextEvent arg0) {// TODO Auto-generated method stub}public void contextInitialized(ServletContextEvent servletContextEvent) {if (initHandlerList != null && initHandlerList.size() > 0) {for (InitHandler initHandler : initHandlerList) {try {initHandler.init(servletContextEvent.getServletContext());} catch (RuntimeException e) {e.printStackTrace();}}}}}
InitHandler 接口 初始化接口
package com.dadi.oa.init;import javax.servlet.ServletContext;public interface InitHandler {public void init(ServletContext servletContext);}
InitEhcacheHandler 类 ehcache初始化类实现初始化接口
package com.dadi.oa.init;import java.io.File;import javax.servlet.ServletContext;import com.dadi.oa.util.CacheFactory;import net.sf.ehcache.CacheManager;public class InitEhcacheHandler implements InitHandler {public void init(ServletContext servletContext) {String realpath = servletContext.getRealPath("/WEB-INF/ehcache.xml").replace('/',File.separatorChar);CacheManager cacheManager = new CacheManager(realpath);CacheFactory.setCacheManager(cacheManager);}}
CacheFactory 类 获取各类缓存
package com.dadi.oa.util;import net.sf.ehcache.Cache;import net.sf.ehcache.CacheManager;import org.apache.commons.lang.StringUtils;public class CacheFactory {private static CacheManager cacheManager ;public static Cache getCacheByName(String cachename) {if (StringUtils.isEmpty(cachename)) {System.out.println("cachename must be not empty");return null;}Cache cache = cacheManager.getCache(cachename);if (cache == null) {System.out.println("no cache named : " + cachename + "has defined. ");return null;}return cache;}/***<pre><cache name="cachekey" maxElementsInMemory="50000"eternal="false" overflowToDisk="false" timeToIdleSeconds="864000"timeToLiveSeconds="864000" /></pre>功能:获取cache,不存在时,自动创建,options为可选配置参数* @param options [memory,liveSeconds, idleSeconds]* @param cacheName* @return*/public static Cache getCache(String cacheName,int...options) {int memory=10000, liveSeconds=864000, idleSeconds=864000;//shortcacheif (options!=null&&options.length>0) {memory = options[0];liveSeconds=options.length>1?options[1]:liveSeconds;idleSeconds=options.length>2?options[1]:idleSeconds;}Cache cache = getCacheByName(cacheName);if(cache==null){cache = new Cache(cacheName, memory, false, false, liveSeconds, idleSeconds);cacheManager.addCache(cache);System.out.println("the cache " + cacheName + " has created dynamically. ");}return cache;}public synchronized static void setCacheManager(CacheManager cacheManager) {CacheFactory.cacheManager = cacheManager;}}
使用ehcahe
/*** 获取公文证明列表* @param mapping* @param form* @param request* @param response* @return* @throws Exception*/public ActionForward provelist(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws Exception {String msgid = RequestUtil.getRequestInfo(request, "msgid");String resultList_en_ = RequestUtil.getRequestInfo(request, "resultList_en_");CommonListVo commonListVo = CacheFactory.getCache("shortcache").get("commonListVo"+msgid) == null? null ://获取缓存(CommonListVo)BeanUtils.cloneBean(CacheFactory.getCache("shortcache").get("commonListVo"+msgid).getObjectValue());if(null !=resultList_en_ && resultList_en_.equalsIgnoreCase("output.xls")){logger.info("---------------prove list execl export .......-----------");// modfiedExportData(commonListVo.getDataList());}else{logger.info("---------------get prove list begin -----------");if(null == commonListVo){Map retMap = oaProveManageService.getUserProveList(msgid);if(retMap.get("resultStr").toString().equalsIgnoreCase("fail")){request.setAttribute("info", "数据库繁忙,请稍候再试!");request.setAttribute("closepage", "yes");}commonListVo = new CommonListVo();commonListVo.setColumnList((ArrayList)genECColumnsList());commonListVo.setDataList((ArrayList)retMap.get("provelist"));commonListVo.setExportFlag(true);commonListVo.setActionTo("/oaproverpt.do");commonListVo.setPagesize(100);//设置缓存CacheFactory.getCacheByName("shortcache").put(new Element("commonListVo"+msgid, BeanUtils.cloneBean(commonListVo)));}commonListVo.setDataList((ArrayList)genECDataList((List)commonListVo.getDataList(),request.getServerName()));logger.info("---------------get prove list end -----------");}request.setAttribute("commonListVo", commonListVo);return mapping.findForward("success");}
ehcache jar:
附件列表

浙公网安备 33010602011771号