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

     一:详细配置步骤

         1,添加ehcache.xml文件

          将ehcache.xml文件添加到src路径下面。ehcache.xml文件内容如下

    1. <ehcache>  
    2.     <diskStore path="java.io.tempdir" />  
    3.     <defaultCache maxElementsInMemory="1000" eternal="false"  
    4.         timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />  
    5.     <cache name="ehcacheName" maxElementsInMemory="10000"  
    6.         eternal="false" timeToIdleSeconds="300000" timeToLiveSeconds="600000"  
    7.         overflowToDisk="true" />  
    8. </ehcache>  

         2,添加spring配置文件

         在applicContext.xml文件中添加

    1.    <bean id="cacheManagerFactory"  
    2.     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"  
    3.     p:configLocation="classpath:ehcache.xml"></bean>  
    4.   
    5. <!-- 声明cacheManager -->  
    6. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"   
    7.     p:cacheManager-ref="cacheManagerFactory" ></bean>  

    二:使用

         1,定义EHCache工具方法

         

    1. public class EHCache {  
    2.     private static final CacheManager cacheManager = new CacheManager();  
    3.     private Cache cache;  
    4.     public EHCacheService(){  
    5.         this.cache=cacheManager.getCache("ehcacheName")  
    6.     }  
    7.   
    8.     public Cache getCache() {  
    9.         return cache;  
    10.     }  
    11.   
    12.     public void setCache(Cache cache) {  
    13.         this.cache = cache;  
    14.     }  
    15.   
    16.   
    17.   
    18.         /* 
    19.      * 通过名称从缓存中获取数据 
    20.      */  
    21.     public Object getCacheElement(String cacheKey) throws Exception {  
    22.             net.sf.ehcache.Element e = cache.get(cacheKey);  
    23.         if (e == null) {  
    24.             return null;  
    25.         }  
    26.         return e.getValue();  
    27.     }  
    28.     /* 
    29.      * 将对象添加到缓存中 
    30.      */  
    31.     public void addToCache(String cacheKey, Object result) throws Exception {  
    32.         Element element = new Element(cacheKey, result);  
    33.         cache.put(element);  
    34.     }  
    35.   
    36.   
    37. }  
        

          2,测试

          

    1. public class Test{  
    2.     EHCache ehCache = new EHCache();  
    3.     public void Test(){  
    4.         //测试将json对象存入缓存中  
    5.         JSONObject obj = new JSONObject();  
    6.         obj.put("name","lsz");  
    7.         ehCache.addToCache("cache_json",obj);  
    8.   
    9.         //从缓存中获取  
    10.         JSONObject getobj = (JSONObject)ehCache.getCacheElement("cache_json");  
    11.         System.out.println(getobj.toString());  
    12.     }  
    13. }  

     

    三:问题解决

          1,框架环境是自己搭建的,添加ehcache后运行出错:

    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/cache]
    Offending resource: class path resource [applicationContext.xml]

         

          出现这种问题,原因是因为在applicationContext.xml文件中 多加了 

        <cache:annotation-driven cache-manager="cacheManager" /> 将其去掉即可


         2,框架需要添加jar包

         spring-context-support-3.2.0.RELEASE.jar

         spring-context-3.2.0.RELEASE.jar

     

    原文出自:http://blog.csdn.net/lishuangzhe7047/article/details/41680921
  • 相关阅读:
    windows7 下 apache2.4 和 php5.5 及 mysql5.6 的安装与配置
    JavaScript高级编程 (1)
    关于解决乱码问题的一点探索之二(涉及Unicode(utf-16)和GBK)
    关于解决乱码问题的一点探索之一(涉及utf-8和GBK)
    Windows上安装、配置MySQL的常见问题
    解析DXF图形文件格式
    vue三十一:vue配置反向代理
    vue三十:eslint修复错误和打包用于生产
    vue二十九:多个文件组件集成
    vue二十八:Vue-Cli之环境搭建之node安装脚手架和使用脚手架创建vue项目
  • 原文地址:https://www.cnblogs.com/challengeof/p/4281882.html
Copyright © 2011-2022 走看看