From 0afb6aa81e33608fc53d657b489155a9fa2b63a7 Mon Sep 17 00:00:00 2001 From: Jiaju Zhuang Date: Fri, 10 Feb 2023 17:16:10 +0800 Subject: [PATCH] =?UTF-8?q?*=20=E5=9C=A8=E4=B8=B4=E6=97=B6=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E8=A2=AB=E5=88=A0=E9=99=A4=E7=9A=84=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E8=83=BD=E6=AD=A3=E5=B8=B8=E7=9A=84=E8=AF=BB=E5=8F=96?= =?UTF-8?q?=20[Issue=20#2693](https://github.com/alibaba/easyexcel/issues/?= =?UTF-8?q?2693)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/alibaba/excel/cache/Ehcache.java | 13 ++++-- .../core/compatibility/CompatibilityTest.java | 41 +++++++++++++++++++ .../test/core/large/LargeDataTest.java | 13 +----- update.md | 1 + 4 files changed, 52 insertions(+), 16 deletions(-) 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 ce506792..6387d2a1 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 @@ -2,6 +2,7 @@ package com.alibaba.excel.cache; import java.io.File; import java.util.ArrayList; +import java.util.Optional; import java.util.UUID; import com.alibaba.excel.context.AnalysisContext; @@ -79,7 +80,8 @@ public class Ehcache implements ReadCache { static { CACHE_PATH_FILE = FileUtils.createCacheTmpFile(); FILE_CACHE_MANAGER = - CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build(true); + CacheManagerBuilder.newCacheManagerBuilder().with(CacheManagerBuilder.persistence(CACHE_PATH_FILE)).build( + true); ACTIVE_CACHE_MANAGER = CacheManagerBuilder.newCacheManagerBuilder().build(true); FILE_CACHE_CONFIGURATION = CacheConfigurationBuilder .newCacheConfigurationBuilder(Integer.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder() @@ -92,13 +94,16 @@ public class Ehcache implements ReadCache { try { fileCache = FILE_CACHE_MANAGER.createCache(cacheAlias, FILE_CACHE_CONFIGURATION); } catch (IllegalStateException e) { - //fix Issue #2693,重建缓存文件夹 + //fix Issue #2693,Temporary files may be deleted if there is no operation for a long time, so they need + // to be recreated. if (CACHE_PATH_FILE.exists()) { throw e; } synchronized (Ehcache.class) { - if (!CACHE_PATH_FILE.exists()){ - log.debug("cache file dir is not exist retry create"); + if (!CACHE_PATH_FILE.exists()) { + if (log.isDebugEnabled()) { + log.debug("cache file dir is not exist retry create"); + } FileUtils.createDirectory(CACHE_PATH_FILE); } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java index f4aae4a8..368f91b8 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/compatibility/CompatibilityTest.java @@ -1,16 +1,24 @@ package com.alibaba.easyexcel.test.core.compatibility; +import java.io.File; import java.math.BigDecimal; +import java.util.ArrayList; import java.util.List; import java.util.Map; +import com.alibaba.easyexcel.test.core.large.LargeData; +import com.alibaba.easyexcel.test.core.large.LargeDataListener; +import com.alibaba.easyexcel.test.core.simple.SimpleData; import com.alibaba.easyexcel.test.util.TestFileUtil; import com.alibaba.excel.EasyExcel; +import com.alibaba.excel.cache.Ehcache; import com.alibaba.excel.constant.EasyExcelConstants; import com.alibaba.excel.enums.ReadDefaultReturnEnum; +import com.alibaba.excel.util.FileUtils; import com.alibaba.fastjson.JSON; import lombok.extern.slf4j.Slf4j; +import org.apache.poi.util.TempFile; import org.junit.Assert; import org.junit.FixMethodOrder; import org.junit.Test; @@ -116,4 +124,37 @@ public class CompatibilityTest { Assert.assertEquals("24.20", list.get(0).get(11)); } + @Test + public void t08() { + // https://github.com/alibaba/easyexcel/issues/2693 + // Temporary files may be deleted if there is no operation for a long time, so they need to be recreated. + File file = TestFileUtil.createNewFile("compatibility/t08.xlsx"); + EasyExcel.write(file, SimpleData.class) + .sheet() + .doWrite(data()); + + List> list = EasyExcel.read(file) + .readCache(new Ehcache(null, 20)) + .sheet() + .doReadSync(); + Assert.assertEquals(10L, list.size()); + + FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR))); + + list = EasyExcel.read(file) + .readCache(new Ehcache(null, 20)) + .sheet() + .doReadSync(); + Assert.assertEquals(10L, list.size()); + } + + private List data() { + List list = new ArrayList(); + for (int i = 0; i < 10; i++) { + SimpleData simpleData = new SimpleData(); + simpleData.setName("姓名" + i); + list.add(simpleData); + } + return list; + } } diff --git a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java index aeebdde8..8862fe18 100644 --- a/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java +++ b/easyexcel-test/src/test/java/com/alibaba/easyexcel/test/core/large/LargeDataTest.java @@ -133,18 +133,7 @@ public class LargeDataTest { LOGGER.info("{} vs {}", cost, costPoi); Assert.assertTrue(costPoi * 2 > cost); } - @Test - public void t05Read() throws Exception { - long start = System.currentTimeMillis(); - EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, - new LargeDataListener()).headRowNumber(2).sheet().doRead(); - LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); - FileUtils.delete(new File(System.getProperty(TempFile.JAVA_IO_TMPDIR))); - start = System.currentTimeMillis(); - EasyExcel.read(TestFileUtil.getPath() + "large" + File.separator + "large07.xlsx", LargeData.class, - new LargeDataListener()).headRowNumber(2).sheet().doRead(); - LOGGER.info("Large data total time spent:{}", System.currentTimeMillis() - start); - } + private List data() { List list = new ArrayList<>(); int size = i + 100; diff --git a/update.md b/update.md index 3383244d..f118eeec 100644 --- a/update.md +++ b/update.md @@ -2,6 +2,7 @@ * 兼容`LocalDate` [Issue #2908](https://github.com/alibaba/easyexcel/issues/2908) * 优化大文件内存存储,减少内存占用 [Issue #2657](https://github.com/alibaba/easyexcel/issues/2657) +* 在临时文件被删除的情况下能正常的读取 [Issue #2693](https://github.com/alibaba/easyexcel/issues/2693) # 3.2.0