ehcache 缓存使用

来源:互联网 发布:apache for windows 编辑:程序博客网 时间:2024/05/02 11:45

 一:详细配置步骤

     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

0 0
原创粉丝点击