package com.ynxbd.common.config; import org.ehcache.Cache; import org.ehcache.CacheManager; import org.ehcache.config.ResourcePools; import org.ehcache.config.ResourceType; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ExpiryPolicyBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.MemoryUnit; import org.ehcache.xml.XmlConfiguration; import java.net.URL; import java.time.Duration; /** * @Author wsq * @Date 2020/12/7 16:06 * @Copyright @ 2020 云南新八达科技有限公司 All rights reserved. */ public class EhCacheConfig { private EhCacheConfig() { } private static CacheManager cacheManager = null; private static Cache cache = null; static { initCacheManager(); } private synchronized static void initCacheManager() { if (cacheManager == null) { URL url = EhCacheConfig.class.getResource("/ehcache.xml"); if (url != null) { XmlConfiguration xmlConfiguration = new XmlConfiguration(url); cacheManager = CacheManagerBuilder.newCacheManager(xmlConfiguration); cacheManager.init(); cache = cacheManager.getCache("default_cache", String.class, String.class); } else { System.out.println("配置文件路径为空"); } } } public static Cache getDefaultCache() { if (cache == null) { if (cacheManager == null) { initCacheManager(); } cache = cacheManager.getCache("default_cache", String.class, String.class); } return cache; } /** * 创建一个缓存 * * @param cacheName 缓存名 * @param timeToLiveExpiration 最大过期时间(S) * @return 缓存 */ public static Cache createCacheTTL(Class K, Class V, String cacheName, Long timeToLiveExpiration) { return createCache(K, V, cacheName, null, null, null, true, timeToLiveExpiration, null); } /** * 创建一个缓存 * * @param cacheName 缓存名 * @param timeToIdleExpiration 最大空闲时间(S) * @return 缓存 */ public static Cache createCacheTTI(Class K, Class V, String cacheName, Long timeToIdleExpiration) { return createCache(K, V, cacheName, null, null, null, true, null, timeToIdleExpiration); } /** * 创建一个缓存 * * @param cacheName 缓存名 * @param heap 堆内缓存大小(MB) * @param offHeap 堆外缓存大小(MB) * @param disk 磁盘缓存大小(MB) * @param isPersistent 重启时是否从磁盘中读取 *

* 两者不可并存,方法中两者同时存在时以[ttl最大存活时间为主] * @param timeToLiveExpiration 最大存活时间(S) * @param timeToIdleExpiration 该时间内未访问则失效(S) * @return 缓存对象 */ public synchronized static Cache createCache(Class K, Class V, String cacheName, Long heap, Long offHeap, Long disk, boolean isPersistent, Long timeToLiveExpiration, Long timeToIdleExpiration) { if (timeToLiveExpiration != null && timeToIdleExpiration != null) { timeToIdleExpiration = null; } if (cacheManager == null) { initCacheManager(); } if (heap == null || offHeap == null || disk == null) { // 读取xml配置 ResourcePools resourcePools = cache.getRuntimeConfiguration().getResourcePools(); heap = resourcePools.getPoolForResource(ResourceType.Core.HEAP).getSize(); offHeap = resourcePools.getPoolForResource(ResourceType.Core.OFFHEAP).getSize(); disk = resourcePools.getPoolForResource(ResourceType.Core.DISK).getSize(); } Cache cache = cacheManager.getCache(cacheName, K, V); if (cache != null) { return cache; } cacheManager.removeCache(cacheName); return cacheManager.createCache(cacheName, CacheConfigurationBuilder.newCacheConfigurationBuilder(K, V, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(heap, MemoryUnit.MB) .offheap(offHeap, MemoryUnit.MB) .disk(disk, MemoryUnit.MB, isPersistent)).withExpiry( timeToLiveExpiration == null ? ExpiryPolicyBuilder.timeToIdleExpiration(Duration.ofSeconds(timeToIdleExpiration)) : ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(timeToLiveExpiration)) ) ); } public static void close() { if (cacheManager != null) cacheManager.close(); } // public static void main(String[] args) throws InterruptedException { // Cache testCache = EhcacheConfig.createCacheTTL(String.class, String.class, "test", 3600L); // // testCache.put("1", "123"); // System.out.println(testCache.get("1")); // cacheManager.close(); // } }