ehcache 


ehcache.xml

  1. <ehcache> 
  2.  <diskStore path="java.io.tmpdir" /> 
  3.  <defaultCache maxElementsInMemory="10000" eternal="false" 
  4.   timeToIdleSeconds="1800" timeToLiveSeconds="1800" 
  5.   overflowToDisk="false" /> 
  6.  <cache name="shortcache" maxElementsInMemory="10000" eternal="false" 
  7.   overflowToDisk="false" timeToIdleSeconds="600" 
  8.   timeToLiveSeconds="600" /> 
  9.  <cache name="middlecache" maxElementsInMemory="50000" 
  10.   eternal="false" overflowToDisk="false" timeToIdleSeconds="1800" 
  11.   timeToLiveSeconds="1800" /> 
  12.  <cache name="longcache" maxElementsInMemory="10000" eternal="false" 
  13.   overflowToDisk="false" timeToIdleSeconds="3600" 
  14.   timeToLiveSeconds="3600" /> 
  15.  <cache name="morelongcache" maxElementsInMemory="50000" 
  16.   eternal="false" overflowToDisk="false" timeToIdleSeconds="864000" 
  17.   timeToLiveSeconds="864000" /> 
  18. </ehcache> 




web.xml 配置监听 

  1. <listener> 
  2.   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
  3.  </listener> 
  4.     <listener> 
  5.         <listener-class>com.dadi.oa.init.InitListener</listener-class> 
  6.   </listener>




InitListener 类  监听初始化类

  1. package com.dadi.oa.init; 
  2. import java.util.ArrayList; 
  3. import java.util.List; 
  4. import javax.servlet.ServletContextEvent; 
  5. import javax.servlet.ServletContextListener; 
  6. public class InitListener implements ServletContextListener { 
  7.  private static List<InitHandler> initHandlerList=new ArrayList<InitHandler>(); 
  8.  static { 
  9.   initHandlerList.add(new InitEhcacheHandler()); 
  10.  } 
  11.   
  12.  public void contextDestroyed(ServletContextEvent arg0) { 
  13.   // TODO Auto-generated method stub 
  14.    
  15.  } 
  16.  public void contextInitialized(ServletContextEvent servletContextEvent) { 
  17.   if (initHandlerList != null && initHandlerList.size() > 0) { 
  18.    for (InitHandler initHandler : initHandlerList) { 
  19.     try { 
  20.      initHandler.init(servletContextEvent.getServletContext()); 
  21.     } catch (RuntimeException e) { 
  22.       
  23.      e.printStackTrace(); 
  24.     } 
  25.    } 
  26.   } 
  27.    
  28.  } 


InitHandler 接口 初始化接口

  1. package com.dadi.oa.init; 
  2. import javax.servlet.ServletContext; 
  3. public interface InitHandler { 
  4.  public void init(ServletContext servletContext); 


InitEhcacheHandler 类    ehcache初始化类实现初始化接口

  1. package com.dadi.oa.init; 
  2. import java.io.File; 
  3. import javax.servlet.ServletContext; 
  4. import com.dadi.oa.util.CacheFactory; 
  5. import net.sf.ehcache.CacheManager; 
  6. public class InitEhcacheHandler implements InitHandler { 
  7.  public void init(ServletContext servletContext) { 
  8.   String realpath = servletContext.getRealPath("/WEB-INF/ehcache.xml").replace('/',File.separatorChar); 
  9.   CacheManager cacheManager = new CacheManager(realpath); 
  10.   CacheFactory.setCacheManager(cacheManager); 
  11.  } 




CacheFactory  类   获取各类缓存  

  1. package com.dadi.oa.util; 
  2. import net.sf.ehcache.Cache; 
  3. import net.sf.ehcache.CacheManager; 
  4. import org.apache.commons.lang.StringUtils; 
  5. public class CacheFactory { 
  6.  private  static CacheManager cacheManager ; 
  7.   
  8.  public static Cache getCacheByName(String cachename) { 
  9.   if (StringUtils.isEmpty(cachename)) { 
  10.    System.out.println("cachename must be not empty"); 
  11.    return null; 
  12.   } 
  13.   Cache cache = cacheManager.getCache(cachename); 
  14.   if (cache == null) { 
  15.    System.out.println("no cache named : " + cachename + "has defined. "); 
  16.    return null; 
  17.   } 
  18.   return cache; 
  19.  } 
  20.     /** 
  21.      *<pre><cache name="cachekey" maxElementsInMemory="50000" 
  22.      eternal="false" overflowToDisk="false" timeToIdleSeconds="864000" 
  23.      timeToLiveSeconds="864000" /></pre> 
  24.      功能:获取cache,不存在时,自动创建,options为可选配置参数 
  25.      * @param options [memory,liveSeconds, idleSeconds] 
  26.      * @param cacheName 
  27.      * @return 
  28.      */ 
  29.     public static Cache getCache(String cacheName,int...options) { 
  30.         int memory=10000, liveSeconds=864000, idleSeconds=864000;//shortcache 
  31.         if (options!=null&&options.length>0) { 
  32.             memory = options[0]; 
  33.             liveSeconds=options.length>1?options[1]:liveSeconds; 
  34.             idleSeconds=options.length>2?options[1]:idleSeconds; 
  35.         } 
  36.         Cache cache = getCacheByName(cacheName); 
  37.         if(cache==null){ 
  38.             cache = new Cache(cacheName, memory, false, false, liveSeconds, idleSeconds); 
  39.             cacheManager.addCache(cache); 
  40.             System.out.println("the cache " + cacheName + " has created dynamically. "); 
  41.         } 
  42.         return cache; 
  43.     } 
  44.  public synchronized static void setCacheManager(CacheManager cacheManager) { 
  45.   CacheFactory.cacheManager = cacheManager; 
  46.  } 



使用ehcahe

 
  1. /** 
  2.    * 获取公文证明列表 
  3.    * @param mapping 
  4.    * @param form 
  5.    * @param request 
  6.    * @param response 
  7.    * @return 
  8.    * @throws Exception 
  9.    */ 
  10.   public ActionForward provelist(ActionMapping mapping, ActionForm form, 
  11.           HttpServletRequest request, HttpServletResponse response) 
  12.           throws Exception { 
  13.    String msgid = RequestUtil.getRequestInfo(request, "msgid"); 
  14.    String resultList_en_ = RequestUtil.getRequestInfo(request, "resultList_en_"); 
  15.    CommonListVo commonListVo = CacheFactory.getCache("shortcache").get("commonListVo"+msgid) == null  
  16.      ? null : 
  17. //获取缓存
  18. (CommonListVo)BeanUtils.cloneBean(CacheFactory.getCache("shortcache").get("commonListVo"+msgid).getObjectValue());   
  19.    if(null !=resultList_en_ && resultList_en_.equalsIgnoreCase("output.xls")){ 
  20.     logger.info("---------------prove list execl export .......-----------"); 
  21.    // modfiedExportData(commonListVo.getDataList()); 
  22.    }else{ 
  23.     logger.info("---------------get prove list begin -----------"); 
  24.     if(null == commonListVo){ 
  25.      Map retMap = oaProveManageService.getUserProveList(msgid); 
  26.      if(retMap.get("resultStr").toString().equalsIgnoreCase("fail")){ 
  27.       request.setAttribute("info", "数据库繁忙,请稍候再试!"); 
  28.       request.setAttribute("closepage", "yes"); 
  29.      } 
  30.      commonListVo = new CommonListVo(); 
  31.      commonListVo.setColumnList((ArrayList)genECColumnsList()); 
  32.      commonListVo.setDataList((ArrayList)retMap.get("provelist")); 
  33.      commonListVo.setExportFlag(true); 
  34.      commonListVo.setActionTo("/oaproverpt.do"); 
  35.      commonListVo.setPagesize(100); 
  36. //设置缓存
  37.      CacheFactory.getCacheByName("shortcache").put(new Element("commonListVo"+msgid, BeanUtils.cloneBean(commonListVo))); 
  38.     } 
  39.     commonListVo.setDataList((ArrayList)genECDataList((List)commonListVo.getDataList(),request.getServerName())); 
  40.     
  41.     logger.info("---------------get prove list end -----------"); 
  42.    } 
  43.    request.setAttribute("commonListVo", commonListVo); 
  44.    return mapping.findForward("success"); 
  45.   }


ehcache jar:


附件列表

ehcache 缓存管理工具的更多相关文章

  1. Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存

    一  基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 -->    <bean cl ...

  2. 业务逻辑:五、完成认证用户的动态授权功能 六、完成Shiro整合Ehcache缓存权限数据

    一. 完成认证用户的动态授权功能 提示:根据当前认证用户查询数据库,获取其对应的权限,为其授权 操作步骤: 在realm的授权方法中通过使用principals对象获取到当前登录用户 创建一个授权信息 ...

  3. Spring Boot 2.x基础教程:EhCache缓存的使用

    上一篇我们学会了如何使用Spring Boot使用进程内缓存在加速数据访问.可能大家会问,那我们在Spring Boot中到底使用了什么缓存呢? 在Spring Boot中通过@EnableCachi ...

  4. 简单聊聊Ehcache缓存

    最近工作没有那么忙,有时间来写写东西.今年的系统分析师报名已经开始了,面对历年的真题,真的难以入笔,所以突然对未来充满了担忧,还是得抓紧时间学习技术. 同事推了一篇软文,看到了这个Ehcache,感觉 ...

  5. Spring自定义缓存管理及配置Ehcache缓存

    spring自带缓存.自建缓存管理器等都可解决项目部分性能问题.结合Ehcache后性能更优,使用也比较简单. 在进行Ehcache学习之前,最好对Spring自带的缓存管理有一个总体的认识. 这篇文 ...

  6. JAVAEE——BOS物流项目12:角色、用户管理,使用ehcache缓存,系统菜单根据登录人展示

    1 学习计划 1.角色管理 n 添加角色功能 n 角色分页查询 2.用户管理 n 添加用户功能 n 用户分页查询 3.修改Realm中授权方法(查询数据库) 4.使用ehcache缓存权限数据 n 添 ...

  7. SpringMVC + Mybatis + Shiro + ehcache时缓存管理器报错。

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shiroFilter' ...

  8. SpringBoot2 整合Ehcache组件,轻量级缓存管理

    本文源码:GitHub·点这里 || GitEE·点这里 一.Ehcache缓存简介 1.基础简介 EhCache是一个纯Java的进程内缓存框架,具有快速.上手简单等特点,是Hibernate中默认 ...

  9. Hibernate性能优化之EHCache缓存

    像Hibernate这种ORM框架,相较于JDBC操作,需要有更复杂的机制来实现映射.对象状态管理等,因此在性能和效率上有一定的损耗. 在保证避免映射产生低效的SQL操作外,缓存是提升Hibernat ...

随机推荐

  1. eclipse 将javaWeb项目转化成maven项目

      eclipse 将javaWeb项目转化成maven项目 CreateTime--2018年4月18日16:04:18 Author:Marydon 1.首先,maven项目的标准目录 2.web ...

  2. TP3.2之引入第三方类库文件和普通.php文件

    1.引入第三方类库 .class.php文件 1.1 类库有写namespace命名空间 namespace Org\Util; class Auth { } 保存到ThinkPHP/Library/ ...

  3. 通过配置CPU参数 worker_cpu_affinity 提升nginx性能

    简介 Nginx默认没有开启利用多核cpu,我们可以通过增加worker_cpu_affinity配置参数来充分利用多核cpu的性能.cpu是任务处理,计算最关键的资源,cpu核越多,性能就越好. 规 ...

  4. IDEA+SpringMVC+Spring+Mybatis

    详细参照: SSM框架——详细整合教程(Spring+SpringMVC+MyBatis) 这里只说一下注意的地方: 1.上面那篇是用的eclipse, 但IDEA的目录结构和eclipse稍有不同. ...

  5. HDUOJ--1865 1string

    1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  6. 傅立叶级数(Fourier Series)和周期现象

    一.前言 如果你仔细观察,工作和生活中充满了周期现象:旁边linux driver工程师在调试audio driver的时候播放的1kHz的正弦信号,周末去公园游玩,游船推开水面的波纹,硬件工程师调试 ...

  7. DB2中三个有关锁变量DB2_EVALUNCOMMITTED,DB2_SKIPDELETED和DB2_SKIPINSERTED的使用

    本文主要解释下DB2中三个有关锁变量DB2_EVALUNCOMMITTED,DB2_SKIPDELETED和DB2_SKIPINSERTED的使用 实验环境: DB2 v9.7.0.6 AIX 6.1 ...

  8. SQL 语句递归查询 With AS 查找所有子节点

    create table #EnterPrise (   Department nvarchar(50),--部门名称   ParentDept nvarchar(50),--上级部门   Depar ...

  9. 本地服务器能ping通,但是ssh及各种端口都访问不到---待整理

    情况描述: 情况一:后来发现,开机需要很长一段时间之后才能正常的访问那台机器 原因:系统启动时加载的程序多,所以开机需要很长的时间,关掉不用的就可以了 情况二:服务器位于局域网内,有的时候可以访问到, ...

  10. django post和get 比较

    当我们提交表单仅仅需要获取数据时就可以用GET: 而当我们提交表单时需要更改服务器数据的状态,或者说发送e-mail,或者其他不仅仅是获取并显示数据的时候就使用POST. 在这个搜索书籍的例子里,我们 ...