ehcache缓存使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ITlyng/article/details/78958025

在当下主流的开发缓存技术当中,memcached/redis/ehcache最常用,本文主要介绍其中ehcahce缓存的常用配置和简单实用:

1.在工程的src目录下添加ehcache.xml配置文件:

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

其中主要的配置参数详解如下:

name:缓存名称.

maxElementsInMemory:缓存中的最大个数.

eternal:对象是否永久有效,如果为true,timeout将不起作用,一般设置为false.

timeToIdleSeconds:对象在失效前的闲置时间,默认为0,eternal为false时设置有效,

timeToLiveSeconds:对象在失效前的存活时间,默认为0,eternal为false时设置有效,

overflowToDisk:内存中的对象数量达到maxElementsInMemory时,将会将超过的对象写入磁盘.

diskSpoolBufferSizeMB:磁盘缓存区大小,默认30M,

maxElementsOnDisk:磁盘上的最大缓存个数,

clearOnFlush:内存数量最大时是否清楚.

2.在spring配置文件中配置ehcache(声明cacheManagerFactory和cacheManager)

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

3.定义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.   

4.测试

  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. }  



猜你喜欢

转载自blog.csdn.net/ITlyng/article/details/78958025