|
|
@ -2,6 +2,10 @@ package com.alibaba.excel.cache.selector; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import lombok.Data; |
|
|
|
|
|
|
|
import lombok.EqualsAndHashCode; |
|
|
|
|
|
|
|
import lombok.Getter; |
|
|
|
|
|
|
|
import lombok.Setter; |
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
|
import org.apache.poi.openxml4j.opc.PackagePart; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.Logger; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
import org.slf4j.LoggerFactory; |
|
|
@ -15,6 +19,9 @@ import com.alibaba.excel.cache.ReadCache; |
|
|
|
* |
|
|
|
* |
|
|
|
* @author Jiaju Zhuang |
|
|
|
* @author Jiaju Zhuang |
|
|
|
**/ |
|
|
|
**/ |
|
|
|
|
|
|
|
@Getter |
|
|
|
|
|
|
|
@Setter |
|
|
|
|
|
|
|
@EqualsAndHashCode |
|
|
|
public class SimpleReadCacheSelector implements ReadCacheSelector { |
|
|
|
public class SimpleReadCacheSelector implements ReadCacheSelector { |
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class); |
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(SimpleReadCacheSelector.class); |
|
|
|
/** |
|
|
|
/** |
|
|
@ -24,37 +31,46 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* If it's less than 5M, use map cache, or use ehcache.unit MB. |
|
|
|
* If it's less than 5M, use map cache, or use ehcache.unit MB. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static final int DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5; |
|
|
|
private static final long DEFAULT_MAX_USE_MAP_CACHE_SIZE = 5; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Maximum size of cache activation.unit MB. |
|
|
|
* Maximum batch of `SharedStrings` stored in memory. |
|
|
|
|
|
|
|
* The batch size is 100.{@link Ehcache#BATCH_COUNT} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE = 20; |
|
|
|
private static final int DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT = 20; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB. |
|
|
|
* Shared strings exceeding this value will use {@link Ehcache},or use {@link MapCache}.unit MB. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final long maxUseMapCacheSize; |
|
|
|
private Long maxUseMapCacheSize; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Maximum size of cache activation.unit MB. |
|
|
|
* Maximum size of cache activation.unit MB. |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @deprecated Please use maxCacheActivateBatchCount to control the size of the occupied memory |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
@Deprecated |
|
|
|
|
|
|
|
private Integer maxCacheActivateSize; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Maximum batch of `SharedStrings` stored in memory. |
|
|
|
|
|
|
|
* The batch size is 100.{@link Ehcache#BATCH_COUNT} |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private final int maxCacheActivateSize; |
|
|
|
private Integer maxCacheActivateBatchCount; |
|
|
|
|
|
|
|
|
|
|
|
public SimpleReadCacheSelector() { |
|
|
|
public SimpleReadCacheSelector() { |
|
|
|
this(DEFAULT_MAX_USE_MAP_CACHE_SIZE, DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public SimpleReadCacheSelector(long maxUseMapCacheSize, int maxCacheActivateSize) { |
|
|
|
/** |
|
|
|
if (maxUseMapCacheSize <= 0) { |
|
|
|
* Parameter maxCacheActivateSize has already been abandoned |
|
|
|
this.maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE; |
|
|
|
* |
|
|
|
} else { |
|
|
|
* @param maxUseMapCacheSize |
|
|
|
this.maxUseMapCacheSize = maxUseMapCacheSize; |
|
|
|
* @param maxCacheActivateSize |
|
|
|
} |
|
|
|
*/ |
|
|
|
if (maxCacheActivateSize <= 0) { |
|
|
|
@Deprecated |
|
|
|
this.maxCacheActivateSize = DEFAULT_MAX_EHCACHE_ACTIVATE_SIZE; |
|
|
|
public SimpleReadCacheSelector(Long maxUseMapCacheSize, Integer maxCacheActivateSize) { |
|
|
|
} else { |
|
|
|
this.maxUseMapCacheSize = maxUseMapCacheSize; |
|
|
|
this.maxCacheActivateSize = maxCacheActivateSize; |
|
|
|
this.maxCacheActivateSize = maxCacheActivateSize; |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
@ -68,6 +84,9 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { |
|
|
|
return new MapCache(); |
|
|
|
return new MapCache(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (maxUseMapCacheSize == null) { |
|
|
|
|
|
|
|
maxUseMapCacheSize = DEFAULT_MAX_USE_MAP_CACHE_SIZE; |
|
|
|
|
|
|
|
} |
|
|
|
if (size < maxUseMapCacheSize * B2M) { |
|
|
|
if (size < maxUseMapCacheSize * B2M) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
LOGGER.debug("Use map cache.size:{}", size); |
|
|
|
LOGGER.debug("Use map cache.size:{}", size); |
|
|
@ -77,6 +96,17 @@ public class SimpleReadCacheSelector implements ReadCacheSelector { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
if (LOGGER.isDebugEnabled()) { |
|
|
|
LOGGER.debug("Use ehcache.size:{}", size); |
|
|
|
LOGGER.debug("Use ehcache.size:{}", size); |
|
|
|
} |
|
|
|
} |
|
|
|
return new Ehcache(maxCacheActivateSize); |
|
|
|
|
|
|
|
|
|
|
|
// In order to be compatible with the code
|
|
|
|
|
|
|
|
// If the user set up `maxCacheActivateSize`, then continue using it
|
|
|
|
|
|
|
|
if (maxCacheActivateSize != null) { |
|
|
|
|
|
|
|
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
if (maxCacheActivateBatchCount == null) { |
|
|
|
|
|
|
|
maxCacheActivateBatchCount = DEFAULT_MAX_EHCACHE_ACTIVATE_BATCH_COUNT; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return new Ehcache(maxCacheActivateSize, maxCacheActivateBatchCount); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|