参考: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; } }