0%
ehcache配置文件详解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| <?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true">
<diskStore path="java.io.tmpdir"/>
<defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/>
<cache name="cache_token" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" memoryStoreEvictionPolicy="FIFO" /> </ehcache>
|
ehcache的工具类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| public class CacheKit { private static CacheManager cacheManager; private static final Logger log = Logger.getLogger(CacheKit.class); static void init(CacheManager cacheManager) { CacheKit.cacheManager = cacheManager; } public static CacheManager getCacheManager() { return cacheManager; } static Cache getOrAddCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { synchronized(cacheManager) { cache = cacheManager.getCache(cacheName); if (cache == null) { log.warn("Could not find cache config [" + cacheName + "], using default."); cacheManager.addCacheIfAbsent(cacheName); cache = cacheManager.getCache(cacheName); log.debug("Cache [" + cacheName + "] started."); } } } return cache; } public static void put(String cacheName, Object key, Object value) { getOrAddCache(cacheName).put(new Element(key, value)); } @SuppressWarnings("unchecked") public static <T> T get(String cacheName, Object key) { Element element = getOrAddCache(cacheName).get(key); return element != null ? (T)element.getObjectValue() : null; } @SuppressWarnings("rawtypes") public static List getKeys(String cacheName) { return getOrAddCache(cacheName).getKeys(); } public static void remove(String cacheName, Object key) { getOrAddCache(cacheName).remove(key); } public static void removeAll(String cacheName) { getOrAddCache(cacheName).removeAll(); } }
|
参考文献