码迷,mamicode.com
首页 > 系统相关 > 详细

ehcache 缓存使用

时间:2015-07-09 11:31:09      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:

一:详细配置步骤

     1,添加ehcache.xml文件

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

[html] view plaincopyprint?技术分享技术分享

  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 plaincopyprint?技术分享技术分享

  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 plaincopyprint?技术分享技术分享

  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 plaincopyprint?技术分享技术分享

  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


版权声明:本文为博主原创文章,未经博主允许不得转载。


ehcache 缓存使用

标签:

原文地址:http://my.oschina.net/glenxu/blog/476414

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!