zoukankan      html  css  js  c++  java
  • 使用chcache 缓存

    在项目里碰到了表单提交和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();
  • 相关阅读:
    多数据源 + Configuration中bean依赖注入顺序问题
    mysql 示例数据库安装
    mysql隔离级别与锁,接口并发响应速度的关系(1)
    management & Actuator
    SpEL
    N/A的含义
    设置U盘图标
    c语言指针
    Decorator(装饰器)
    Lambda Expression
  • 原文地址:https://www.cnblogs.com/mytzq/p/5591652.html
Copyright © 2011-2022 走看看