You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
			
				
					145 lines
				
				5.5 KiB
			
		
		
			
		
	
	
					145 lines
				
				5.5 KiB
			| 
								 
											3 years ago
										 
									 | 
							
								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<String, String> 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<String, String> 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 <K, V> Cache<K, V> createCacheTTL(Class<K> K, Class<V> V, String cacheName, Long timeToLiveExpiration) {
							 | 
						||
| 
								 | 
							
								        return createCache(K, V, cacheName, null, null, null, true, timeToLiveExpiration, null);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * 创建一个缓存
							 | 
						||
| 
								 | 
							
								     *
							 | 
						||
| 
								 | 
							
								     * @param cacheName            缓存名
							 | 
						||
| 
								 | 
							
								     * @param timeToIdleExpiration 最大空闲时间(S)
							 | 
						||
| 
								 | 
							
								     * @return 缓存
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public static <K, V> Cache<K, V> createCacheTTI(Class<K> K, Class<V> 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         重启时是否从磁盘中读取
							 | 
						||
| 
								 | 
							
								     *                             <p>
							 | 
						||
| 
								 | 
							
								     *                             两者不可并存,方法中两者同时存在时以[ttl最大存活时间为主]
							 | 
						||
| 
								 | 
							
								     * @param timeToLiveExpiration 最大存活时间(S)
							 | 
						||
| 
								 | 
							
								     * @param timeToIdleExpiration 该时间内未访问则失效(S)
							 | 
						||
| 
								 | 
							
								     * @return 缓存对象
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public synchronized static <K, V> Cache<K, V> createCache(Class<K> K, Class<V> 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<K, V> 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<String, String> testCache = EhcacheConfig.createCacheTTL(String.class, String.class, "test", 3600L);
							 | 
						||
| 
								 | 
							
								//
							 | 
						||
| 
								 | 
							
								//        testCache.put("1", "123");
							 | 
						||
| 
								 | 
							
								//        System.out.println(testCache.get("1"));
							 | 
						||
| 
								 | 
							
								//        cacheManager.close();
							 | 
						||
| 
								 | 
							
								//    }
							 | 
						||
| 
								 | 
							
								}
							 |