继续潜水

导航

 

在项目里碰到了表单提交和ajax访问后台取到的request对象不是同一个对象,所以不能够资源共享,问了大神决定配置一个缓存来处理这个问题。

引用jar :ehcache-core-2.5.2.jar,ehcache-web-2.0.4.jar

 

添加 ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>  
    <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">  
      
        <!-- 默认缓存 -->  
        <defaultCache  
               maxElementsInMemory="1000"  
               eternal="false"  
               timeToIdleSeconds="120"  
               timeToLiveSeconds="120"  
               overflowToDisk="false"/>  
                 
        <!-- 菜单缓存 -->      
        <cache name="menuCache"   
               maxElementsInMemory="1000"   
               eternal="false"  
               timeToIdleSeconds="120"  
               timeToLiveSeconds="120"  
               overflowToDisk="false"   
               memoryStoreEvictionPolicy="LRU"/>  
          
    </ehcache>  

页面引用:存

    // 获取ehcache配置文件中的一个cache
          CacheManager cacheManager = CacheManager.create();
          Cache sample = cacheManager.getCache("menuCache");
          // 获取页面缓存
          BlockingCache cache = new BlockingCache(cacheManager.getEhcache("menuCache"));
          // 添加数据到缓存中
          Element element = new Element("outList", outList);
          sample.put(element);

页面引用:取

       // 获取ehcache配置文件中的一个cache
         CacheManager cacheManager = CacheManager.create();
         Cache sample = cacheManager.getCache("menuCache");
         // 获取页面缓存
         // 添加数据到缓存中
        
         // 获取缓存中的对象,注意添加到cache中对象要序列化 实现Serializable接口
         Element result = sample.get("outList");
         sample.remove("outList");
         List<SystemsetVo> outList    =   (List<SystemsetVo>) result.getValue();

 

posted on 2016-06-16 17:19  继续潜水  阅读(924)  评论(0编辑  收藏  举报