|
|
@ -2,8 +2,11 @@ package com.alibaba.excel.cache; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.File; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
|
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.collections4.CollectionUtils; |
|
|
|
|
|
|
|
import org.apache.commons.collections4.MapUtils; |
|
|
|
import org.ehcache.CacheManager; |
|
|
|
import org.ehcache.CacheManager; |
|
|
|
import org.ehcache.config.CacheConfiguration; |
|
|
|
import org.ehcache.config.CacheConfiguration; |
|
|
|
import org.ehcache.config.builders.CacheConfigurationBuilder; |
|
|
|
import org.ehcache.config.builders.CacheConfigurationBuilder; |
|
|
@ -14,7 +17,6 @@ import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
|
|
|
import com.alibaba.excel.context.AnalysisContext; |
|
|
|
import com.alibaba.excel.context.AnalysisContext; |
|
|
|
import com.alibaba.excel.util.CollectionUtils; |
|
|
|
|
|
|
|
import com.alibaba.excel.util.FileUtils; |
|
|
|
import com.alibaba.excel.util.FileUtils; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -32,11 +34,11 @@ public class Ehcache implements ReadCache { |
|
|
|
* Key index |
|
|
|
* Key index |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private int index = 0; |
|
|
|
private int index = 0; |
|
|
|
private HashMap<Integer, String> dataMap = new HashMap<Integer, String>(BATCH_COUNT * 4 / 3 + 1); |
|
|
|
private HashMap<Integer, String> dataMap = new HashMap<>(BATCH_COUNT * 4 / 3 + 1); |
|
|
|
private static CacheManager fileCacheManager; |
|
|
|
private static final CacheManager FILE_CACHE_MANAGER; |
|
|
|
private static CacheConfiguration<Integer, HashMap> fileCacheConfiguration; |
|
|
|
private static final CacheConfiguration<Integer, HashMap> FILE_CACHE_CONFIGURATION; |
|
|
|
private static CacheManager activeCacheManager; |
|
|
|
private static final CacheManager ACTIVE_CACHE_MANAGER; |
|
|
|
private CacheConfiguration<Integer, HashMap> activeCacheConfiguration; |
|
|
|
private final CacheConfiguration<Integer, HashMap> activeCacheConfiguration; |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Bulk storage data |
|
|
|
* Bulk storage data |
|
|
|
*/ |
|
|
|
*/ |
|
|
@ -61,10 +63,10 @@ public class Ehcache implements ReadCache { |
|
|
|
|
|
|
|
|
|
|
|
static { |
|
|
|
static { |
|
|
|
File cacheFile = FileUtils.createCacheTmpFile(); |
|
|
|
File cacheFile = FileUtils.createCacheTmpFile(); |
|
|
|
fileCacheManager = |
|
|
|
FILE_CACHE_MANAGER = |
|
|
|
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true); |
|
|
|
CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true); |
|
|
|
activeCacheManager = CacheManagerBuilder.newCacheManagerBuilder().build(true); |
|
|
|
ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); |
|
|
|
fileCacheConfiguration = CacheConfigurationBuilder |
|
|
|
FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder |
|
|
|
.newCacheConfigurationBuilder(Integer.class, HashMap.class, |
|
|
|
.newCacheConfigurationBuilder(Integer.class, HashMap.class, |
|
|
|
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) |
|
|
|
ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) |
|
|
|
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build(); |
|
|
|
.withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build(); |
|
|
@ -73,8 +75,8 @@ public class Ehcache implements ReadCache { |
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void init(AnalysisContext analysisContext) { |
|
|
|
public void init(AnalysisContext analysisContext) { |
|
|
|
cacheAlias = UUID.randomUUID().toString(); |
|
|
|
cacheAlias = UUID.randomUUID().toString(); |
|
|
|
fileCache = fileCacheManager.createCache(cacheAlias, fileCacheConfiguration); |
|
|
|
fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION); |
|
|
|
activeCache = activeCacheManager.createCache(cacheAlias, activeCacheConfiguration); |
|
|
|
activeCache = ACTIVE_CACHE_MANAGER.createCache(cacheAlias, activeCacheConfiguration); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -113,7 +115,7 @@ public class Ehcache implements ReadCache { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void putFinished() { |
|
|
|
public void putFinished() { |
|
|
|
if (CollectionUtils.isEmpty(dataMap)) { |
|
|
|
if (MapUtils.isEmpty(dataMap)) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
fileCache.put(index / BATCH_COUNT, dataMap); |
|
|
|
fileCache.put(index / BATCH_COUNT, dataMap); |
|
|
@ -121,8 +123,8 @@ public class Ehcache implements ReadCache { |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void destroy() { |
|
|
|
public void destroy() { |
|
|
|
fileCacheManager.removeCache(cacheAlias); |
|
|
|
FILE_CACHE_MANAGER.removeCache(cacheAlias); |
|
|
|
activeCacheManager.removeCache(cacheAlias); |
|
|
|
ACTIVE_CACHE_MANAGER.removeCache(cacheAlias); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|