ehcache 使用方法

引用 :http://blog.csdn.net/zk_zhangkai/article/details/6921924

  1. 一种ehcache实现缓存的方式  
  1. /** 
  2.  *  
  3.  */  
  4. package com.datang.common.cache.flat;  
  5.   
  6. import java.net.URL;  
  7. import java.util.List;  
  8.   
  9. import net.sf.ehcache.CacheManager;  
  10. import net.sf.ehcache.Ehcache;  
  11. import net.sf.ehcache.Element;  
  12.   
  13. import org.slf4j.Logger;  
  14. import org.slf4j.LoggerFactory;  
  15.   
  16. import com.datang.common.tools.util.ResourceUtils;  
  17.   
  18. /**  
  19.  * 平面缓存,按缓存名区分缓存,key-value结构  
  20.  *   
  1.  */  
  2. public abstract class FlatCache {  
  3.     /** 
  4.      * 日志 
  5.      */  
  6.     private static final Logger LOGGER = LoggerFactory  
  7.             .getLogger(FlatCache.class);  
  8.   
  9.     /** 
  10.      * 缓存配置文件 
  11.      */  
  12.     private static String CACHE_CONFIG_FILE = "config/flat_cache.xml";  
  13.   
  14.     /** 
  15.      * Ehanche的缓存管理 
  16.      */  
  17.     private static CacheManager cacheManager = null;  
  18.   
  19.     /** 
  20.      * 设置缓存配置文件,最开始设置才有效果,一旦缓存加载则不能改变 
  21.      *  
  22.      * @param cacheConfigFile 
  23.      */  
  24.     public static void setCacheConfigFile(String cacheConfigFile) {  
  25.         CACHE_CONFIG_FILE = cacheConfigFile;  
  26.     }  
  27.   
  28.     /** 
  29.      * 按缺省配置创建缓存 
  30.      *  
  31.      * @param cacheName 
  32.      */  
  33.     public static void createCache(String cacheName) {  
  34.         getCacheManager().addCache(cacheName);  
  35.     }  
  36.   
  37.     /** 
  38.      * 添加缓存 
  39.      *  
  40.      * @param cacheName 
  41.      * @param key 
  42.      * @param value 
  43.      */  
  44.     public static void put(String cacheName, String key, Object value) {  
  45.         Ehcache cache = getCacheManager().getEhcache(cacheName);  
  46.         cache.put(new Element(key, value));  
  47.     }  
  48.   
  49.     /** 
  50.      * 根据缓存名与key获取值 
  51.      *  
  52.      * @param cacheName 
  53.      * @param key 
  54.      * @return 
  55.      */  
  56.     public static Object get(String cacheName, String key) {  
  57.         Ehcache cache = getCacheManager().getEhcache(cacheName);  
  58.         Element e = cache.get(key);  
  59.         return e == null ? null : e.getObjectValue();  
  60.     }  
  61.   
  62.     /** 
  63.      * 获取缓存名 
  64.      *  
  65.      * @return 
  66.      */  
  67.     public static String[] getCacheNames() {  
  68.         return getCacheManager().getCacheNames();  
  69.     }  
  70.   
  71.     /** 
  72.      * 获取缓存的Keys 
  73.      *  
  74.      * @param cacheName 
  75.      * @return 
  76.      */  
  77.     @SuppressWarnings("unchecked")  
  78.     public static List<String> getKeys(String cacheName) {  
  79.         Ehcache cache = getCacheManager().getEhcache(cacheName);  
  80.         return (List<String>) cache.getKeys();  
  81.     }  
  82.   
  83.     /** 
  84.      * 清除所有 
  85.      */  
  86.     public static void clearAll() {  
  87.         getCacheManager().clearAll();  
  88.     }  
  89.   
  90.     /** 
  91.      * 清空指定缓存 
  92.      *  
  93.      * @param cacheName 
  94.      */  
  95.     public static void clear(String cacheName) {  
  96.         getCacheManager().getCache(cacheName).removeAll();  
  97.     }  
  98.   
  99.     /** 
  100.      * 删除指定对象 
  101.      *  
  102.      * @param cacheName 
  103.      * @param key 
  104.      * @return 
  105.      */  
  106.     public static boolean remove(String cacheName, String key) {  
  107.         return getCacheManager().getCache(cacheName).remove(key);  
  108.     }  
  109.   
  110.     /** 
  111.      * 获取缓存大小 
  112.      *  
  113.      * @param cacheName 
  114.      * @return 
  115.      */  
  116.     public static int getSize(String cacheName) {  
  117.         return getCacheManager().getCache(cacheName).getSize();  
  118.     }  
  119.   
  120.     /** 
  121.      * 获取CacheManager 
  122.      *  
  123.      * @return 
  124.      */  
  125.     private static CacheManager getCacheManager() {  
  126.         if (cacheManager != null) {  
  127.             return cacheManager;  
  128.         }  
  129.   
  130.         try {  
  131.             URL url = ResourceUtils.getResource(CACHE_CONFIG_FILE);  
  132.             cacheManager = CacheManager.create(url);  
  133.         } catch (RuntimeException e) {  
  134.             LOGGER.error("init flat cache failed", e);  
  135.             throw e;  
  136.         }  
  137.   
  138.         return cacheManager;  
  139.     }  
  140. }  

xml的配置如下:

  1. <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.          xsi:noNamespaceSchemaLocation="ehcache.xsd"  
  3.          updateCheck="true" monitoring="autodetect"  
  4.          dynamicConfig="true">  
  5.      
  6.     <defaultCache  
  7.            maxElementsInMemory="20"  
  8.            eternal="false"  
  9.            overflowToDisk="false"  
  10.            timeToIdleSeconds="1800"  
  11.            timeToLiveSeconds="1800">  
  12.     </defaultCache>  
  13.       
  14.     <cache name="CDG-CACHE"  
  15.            maxElementsInMemory="20"  
  16.            overflowToDisk="false"  
  17.            eternal="false"  
  18.            timeToIdleSeconds="1800"  
  19.            timeToLiveSeconds="1800"  
  20.            memoryStoreEvictionPolicy="LRU"  
  21.            transactionalMode="off"  
  22.      />     
  23. </ehcache>  
原文地址:https://www.cnblogs.com/sode/p/2685350.html