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

     一:详细配置步骤

         1,添加ehcache.xml文件

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

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    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文件中添加

    [html] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    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工具方法

         

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    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,测试

          

    [java] view plain copy
     
     print?在CODE上查看代码片派生到我的代码片
    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

  • 相关阅读:
    pythonsys.exit()
    Python字符串格式化
    Json概述以及python对json的相关操作
    python学习笔记——异常
    Python:sys.argv[]用法
    python学习笔记——字符串,列表,字典,集合,数值,sorted
    python class 的属性
    Python模块——unittest 单元测试
    从sql2000 复制数据到sql2005
    abstract、virtual及override
  • 原文地址:https://www.cnblogs.com/yhtboke/p/5643294.html
Copyright © 2011-2022 走看看