在学习EasyExcel
时会发现针对使用类模型配置表头相关属性时,EasyExcel
会使用到缓存技术以提升表头的解析速度如下代码:
这些参数再何时设置的哪?
在easyExcel
基础参数设置中会有这个参数filedCacheLocation
。默认采用的使用线程级别的缓存。
默认支持3种方式:
THREAD_LOCAL
:数据缓存在线程级别,当前线程使用完成时清除数据MEMORY
:数据缓存在内存级别,只有当程序停止时才会清除数据NONE
:不适应缓存
场景分析:
- 前向同一个excel和同一个sheet中重复写入数据时,并且写入属性非常多时:
如下有个用户信息:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class User {@ExcelProperty("用户名")private String username;@ExcelProperty("年龄")private String age;@ExcelProperty("attribute01")private String attribute01;@ExcelProperty("attribute02")private String attribute02;@ExcelProperty("attribute03")private String attribute03;@ExcelProperty("attribute04")private String attribute04;@ExcelProperty("attribute05")private String attribute05;@ExcelProperty("attribute06")private String attribute06;@ExcelProperty("attribute07")private String attribute07;@ExcelProperty("attribute08")private String attribute08;@ExcelProperty("attribute09")private String attribute09;@ExcelProperty("attribute10")private String attribute10;@ExcelProperty("attribute11")private String attribute11;@ExcelProperty("attribute12")private String attribute12;@ExcelProperty("attribute13")private String attribute13;@ExcelProperty("attribute14")private String attribute14;@ExcelProperty("attribute15")private String attribute15;@ExcelProperty("attribute16")private String attribute16;@ExcelProperty("attribute17")private String attribute17;@ExcelProperty("attribute18")private String attribute18;@ExcelProperty("attribute19")private String attribute19;
}
主程序:
public void test01() {List<User> userList = new ArrayList<>();// 写入1000条数据for (int i = 0; i < 1000; i++) {userList.add(new User("lisi" + i, String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i),String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i),String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i),String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i), String.valueOf(i),String.valueOf(i)));}ExcelWriterBuilder excelWriterBuilder = new ExcelWriterBuilder();excelWriterBuilder.file("F:\\excel\\a.xls");excelWriterBuilder.filedCacheLocation(CacheLocationEnum.NONE);long startTime01 = System.currentTimeMillis();ExcelWriterSheetBuilder excelWriterSheetBuilder01 = new ExcelWriterSheetBuilder(excelWriterBuilder.build());excelWriterSheetBuilder01.sheetName("test01");excelWriterSheetBuilder01.doWrite(userList);long endTime01 = System.currentTimeMillis();System.out.println("总耗时 = " + (endTime01 - startTime01) + "ms");long startTime02 = System.currentTimeMillis();ExcelWriterSheetBuilder excelWriterSheetBuilder002 = new ExcelWriterSheetBuilder(excelWriterBuilder.build());excelWriterSheetBuilder002.sheetName("test02");excelWriterSheetBuilder002.doWrite(userList);long endTime02 = System.currentTimeMillis();System.out.println("总耗时 = " + (endTime02 - startTime02) + "ms");long startTime03 = System.currentTimeMillis();ExcelWriterSheetBuilder excelWriterSheetBuilder03 = new ExcelWriterSheetBuilder(excelWriterBuilder.build());excelWriterSheetBuilder03.sheetName("test03");excelWriterSheetBuilder03.doWrite(userList);long endTime03 = System.currentTimeMillis();System.out.println("总耗时 = " + (endTime03 - startTime03) + "ms");}
采用NONE
时调用3次耗时:
第一次:
总耗时 = 596ms
总耗时 = 83ms
总耗时 = 60ms
第二次:
总耗时 = 580ms
总耗时 = 100ms
总耗时 = 54ms
第三次:
总耗时 = 578ms
总耗时 = 91ms
总耗时 = 53ms
采用MEMORY
时调用3次耗时:
第一次:
总耗时 = 490ms
总耗时 = 32ms
总耗时 = 24ms
第二次:
总耗时 = 467ms
总耗时 = 35ms
总耗时 = 22ms
第三次:
总耗时 = 470ms
总耗时 = 34ms
总耗时 = 22ms
采用THREAD_LOCAL
时调用3次耗时:
第一次:
总耗时 = 468ms
总耗时 = 38ms
总耗时 = 23ms
第二次:
总耗时 = 460ms
总耗时 = 32ms
总耗时 = 22ms
第三次:
总耗时 = 460ms
总耗时 = 33ms
总耗时 = 24ms