參考:https://www.jianshu.com/p/154c82073b07
依賴:
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.2</version>
</dependency>
配置文件:
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd"> <!--緩存的路徑 當前配置為系統的臨時目錄 可以任意修改為任意盤符下的文件夾--> <!-- <diskStore path="java.io.tmpdir/ehcache"/> --> <diskStore path="/tmp/sea/"/> <!-- maxElementsInMemory:內存存儲數據的個數 eternal:緩存數據是否永久有效 建議false timeToIdleSeconds:最大空閑時間 (s) 空閑時間超出配置,清理內存數據 timeToLiveSeconds:存活時間(s) overflowToDisk: 溢出到磁盤(磁盤最多存儲多少個對象) 如果內存超過maxElementsInMemory配置那么放置到配置的磁盤路徑上 diskPersistent: 服務器重啟時是否保留磁盤數據 diskExpiryThreadIntervalSeconds: 每隔多長時間進行線程掃描 memoryStoreEvictionPolicy:淘汰策略 LRU(最近最少) FIFO(先進先出 Frist in Frist out) --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" maxElementsOnDisk="10000000" diskPersistent="false" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" > </defaultCache> <!-- 換存10 min --> <cache name="MIN10" eternal="false" maxElementsInMemory="1000" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU" />
<!-- 緩存2小時,保存在磁盤 -->
<cache name="HOUR2" eternal="false" maxElementsInMemory="100" overflowToDisk="true" diskPersistent="true" timeToIdleSeconds="0" timeToLiveSeconds="7200" memoryStoreEvictionPolicy="LRU" />
<!-- 系統緩存 會一直保存,掉電也會存在 需要 System.setProperty("net.sf.ehcache.enableShutdownHook","true");-->
<cache name="sysCache" maxEntriesLocalHeap="100" eternal="true" diskPersistent="true" overflowToDisk="true" memoryStoreEvictionPolicy="LRU" />
</ehcache>
EhCacheUtils:(spring 版本請看最下面)
package com.icil.collect.common; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; public class EhCacheUtils { private static CacheManager cacheManager=null; public static final String SYS_CACHE = "sysCache"; public static final String MIN10 = "MIN10"; public static final String HOUR2 = "HOUR2"; public final static Byte[] locks = new Byte[0]; /** * 獲取SYS_CACHE緩存 * @param key * @return */ public static Object get(String key) { return get(SYS_CACHE, key); } /** * 寫入SYS_CACHE緩存 * @param key * @return */ public static void put(String key, Object value) { put(SYS_CACHE, key, value); } /** * @param key * @return */ public static void remove(String key) { remove(SYS_CACHE, key); } /** * 獲取緩存 * @param cacheName * @param key * @return */ public static Object get(String cacheName, String key) { Element element = getCache(cacheName).get(key); return element == null ? null : element.getObjectValue(); } /** * 寫入緩存 * @param cacheName * @param key * @param value */ public static void put(String cacheName, String key, Object value) { Element element = new Element(key, value); getCache(cacheName).put(element); } /** * 從緩存中移除 s * @param cacheName * @param key */ public static void remove(String cacheName, String key) { getCache(cacheName).remove(key); } public static void removeAll(String cacheName) { getCache(cacheName).removeAll(); } /** * @param cacheName * @return */ public static Cache getCache(String cacheName) { if (cacheManager == null) { synchronized (locks) { if (cacheManager == null) {
System.setProperty("net.sf.ehcache.enableShutdownHook","true"); cacheManager = CacheManager.create(); } } } Cache cache = cacheManager.getCache(cacheName); if (cache == null) { cacheManager.addCache(cacheName); cache = cacheManager.getCache(cacheName); //sava all the day cache.getCacheConfiguration().setEternal(true); } return cache; } public static CacheManager getCacheManager() { return cacheManager; } }
Test:
@Test public void testCache() throws Exception { System.setProperty("net.sf.ehcache.enableShutdownHook","true"); EhCacheUtils ehCacheUtils = new EhCacheUtils(); Cache cache = ehCacheUtils.getCache(EhCacheUtils.SYS_CACHE); CacheManager cacheManager = ehCacheUtils.getCacheManager(); ehCacheUtils.put("sea", "sea test "); Object object = EhCacheUtils.get("sea"); System.err.println(object); }
Spring :
@Configuration public class EhcacheConfig { @Bean(name = "ehCacheManager") @Qualifier("ehCacheManager") public static CacheManager ehCacheManager() { System.setProperty("net.sf.ehcache.enableShutdownHook","true"); CacheManager cacheManager = CacheManager.create(); return cacheManager; } }
EhCacheUtils:
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Component; import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; @Component public class EhCacheUtils { public static final String SYS_CACHE = "sysCache"; public static final String MIN10 = "MIN10"; public static final String HOUR2 = "HOUR2"; @Autowired @Qualifier("ehCacheManager") private CacheManager cacheManager; /** * 獲取SYS_CACHE緩存 * @param key * @return */ public Object get(String key) { return get(SYS_CACHE, key); } /** * 寫入SYS_CACHE緩存 * @param key * @return */ public void put(String key, Object value) { put(SYS_CACHE, key, value); } /** * @param key * @return */ public void remove(String key) { remove(SYS_CACHE, key); } /** * 獲取緩存 * @param cacheName * @param key * @return */ public Object get(String cacheName, String key) { Element element = getCache(cacheName).get(key); return element == null ? null : element.getObjectValue(); } /** * 寫入緩存 * @param cacheName * @param key * @param value */ public void put(String cacheName, String key, Object value) { Element element = new Element(key, value); getCache(cacheName).put(element); } /** * 從緩存中移除 s * @param cacheName * @param key */ public void remove(String cacheName, String key) { getCache(cacheName).remove(key); } public void removeAll(String cacheName) { getCache(cacheName).removeAll(); } /** * @param cacheName * @return */ public Cache getCache(String cacheName) { Cache cache = cacheManager.getCache(cacheName); if (cache == null) { cacheManager.addCache(cacheName); cache = cacheManager.getCache(cacheName); //sava all the day cache.getCacheConfiguration().setEternal(true); } return cache; } }
