From 213938332b40e18ec7893e297d79566b72f5d65b Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 10 Feb 2023 10:25:48 +0800 Subject: [PATCH] stringcache --- .../java/com/alibaba/excel/cache/Ehcache.java | 20 ++++---- .../com/alibaba/excel/cache/StringArray.java | 51 +++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) create mode 100644 easyexcel-core/src/main/java/com/alibaba/excel/cache/StringArray.java diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java index b7b542ee..f3d100ac 100644 --- a/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/Ehcache.java @@ -31,19 +31,19 @@ public class Ehcache implements ReadCache { private int activeIndex = 0; public static final int DEBUG_CACHE_MISS_SIZE = 1000; public static final int DEBUG_WRITE_SIZE = 100 * 10000; - private ArrayList dataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + private StringArray dataList = new StringArray(BATCH_COUNT); private static final CacheManager FILE_CACHE_MANAGER; - private static final CacheConfiguration FILE_CACHE_CONFIGURATION; + private static final CacheConfiguration FILE_CACHE_CONFIGURATION; private static final CacheManager ACTIVE_CACHE_MANAGER; - private final CacheConfiguration activeCacheConfiguration; + private final CacheConfiguration activeCacheConfiguration; /** * Bulk storage data */ - private org.ehcache.Cache fileCache; + private org.ehcache.Cache fileCache; /** * Currently active cache */ - private org.ehcache.Cache activeCache; + private org.ehcache.Cache activeCache; private String cacheAlias; /** * Count the number of cache misses @@ -52,7 +52,7 @@ public class Ehcache implements ReadCache { public Ehcache(int maxCacheActivateSize) { activeCacheConfiguration = CacheConfigurationBuilder - .newCacheConfigurationBuilder(Integer.class, ArrayList.class, + .newCacheConfigurationBuilder(Integer.class, StringArray.class, ResourcePoolsBuilder.newResourcePoolsBuilder().heap(maxCacheActivateSize, MemoryUnit.MB)) .withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(maxCacheActivateSize, MemoryUnit.MB) .build(); @@ -64,7 +64,7 @@ public class Ehcache implements ReadCache { CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(cacheFile)).build(true); ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder - .newCacheConfigurationBuilder(Integer.class, ArrayList.class, + .newCacheConfigurationBuilder(Integer.class, StringArray.class, ResourcePoolsBuilder.newResourcePoolsBuilder().disk(10, MemoryUnit.GB)) .withSizeOfMaxObjectGraph(1000 * 1000L).withSizeOfMaxObjectSize(10, MemoryUnit.GB).build(); } @@ -82,7 +82,7 @@ public class Ehcache implements ReadCache { if (dataList.size() >= BATCH_COUNT) { fileCache.put(activeIndex, dataList); activeIndex++; - dataList = ListUtils.newArrayListWithExpectedSize(BATCH_COUNT); + dataList = new StringArray(BATCH_COUNT); } if (log.isDebugEnabled()) { int alreadyPut = activeIndex * BATCH_COUNT + dataList.size(); @@ -98,7 +98,7 @@ public class Ehcache implements ReadCache { return null; } int route = key / BATCH_COUNT; - ArrayList dataList = activeCache.get(route); + StringArray dataList = activeCache.get(route); if (dataList == null) { dataList = fileCache.get(route); activeCache.put(route, dataList); @@ -113,7 +113,7 @@ public class Ehcache implements ReadCache { @Override public void putFinished() { - if (CollectionUtils.isEmpty(dataList)) { + if (dataList.size() == 0) { return; } fileCache.put(activeIndex, dataList); diff --git a/easyexcel-core/src/main/java/com/alibaba/excel/cache/StringArray.java b/easyexcel-core/src/main/java/com/alibaba/excel/cache/StringArray.java new file mode 100644 index 00000000..24c964d0 --- /dev/null +++ b/easyexcel-core/src/main/java/com/alibaba/excel/cache/StringArray.java @@ -0,0 +1,51 @@ +package com.alibaba.excel.cache; + +import java.io.Serializable; +import java.util.ArrayList; + +/** + * Specially used to store a string array + * + * The reason for not using {@link ArrayList} is: when the `elementData` field of `ArrayList` is serialized, `ehcache` + * There will be a warning. In fact, there is no problem with the `elementData` warning, but there will be a + * `warn` prompt, so I wrote a warning to prevent the `warn` prompt. + * + * @author Jiaju Zhuang + */ +public class StringArray implements Serializable { + private static final long serialVersionUID = 1L; + + /** + * element Data + */ + private String[] elementData; + + /** + * The size of the StringArray (the number of elements it contains). + * + * @serial + */ + private int size; + + public StringArray(int capacity) { + this.elementData = new String[capacity]; + } + + public void add(String e) { + elementData[size++] = e; + } + + public String get(int index) { + return elementData[index]; + } + + /** + * Returns the number of elements in this list. + * + * @return the number of elements in this list + */ + public int size() { + return size; + } + +}