From df5a25472ea88bb8daa7b2012ce35ed092add4b1 Mon Sep 17 00:00:00 2001 From: wangbing Date: Thu, 6 Aug 2020 17:16:45 +0800 Subject: [PATCH] =?UTF-8?q?FreemarkerUtil=E7=BC=93=E5=AD=98=E4=BC=98?= =?UTF-8?q?=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/frame/utils/FreeMarkerUtil.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/resources/modules/SpringBoot/java/frame/utils/FreeMarkerUtil.java b/src/main/resources/modules/SpringBoot/java/frame/utils/FreeMarkerUtil.java index bef49dec..1bc3ae6b 100644 --- a/src/main/resources/modules/SpringBoot/java/frame/utils/FreeMarkerUtil.java +++ b/src/main/resources/modules/SpringBoot/java/frame/utils/FreeMarkerUtil.java @@ -149,8 +149,15 @@ public class FreeMarkerUtil { */ public static boolean renderByFile(File file, Object model, Writer writer) { try { - // 构建模板 - Template template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration()); + // 模板名称 + String name = MD5Util.encode(file.getAbsolutePath()); + // 获取缓存模板 + Template template = (Template) getDefaultConfiguration().getCacheStorage().get(name); + if (template == null) { + // 构建模板,此处构建的模板需手动缓存 + template = new Template(file.getName(), FileUtil.readFileToString(file), getDefaultConfiguration()); + getDefaultConfiguration().getCacheStorage().put(name, template); + } // 渲染模板 return render(template, model, writer); } catch (IOException e) { @@ -182,8 +189,15 @@ public class FreeMarkerUtil { */ public static boolean renderByString(String source, Object model, Writer writer) { try { - // 构建模板 - Template template = new Template(MD5Util.encode(source), source, getDefaultConfiguration()); + // 模板名称 + String name = MD5Util.encode(source); + // 获取缓存模板 + Template template = (Template) getDefaultConfiguration().getCacheStorage().get(name); + if (template == null) { + // 构建模板,此处构建的模板需手动缓存 + template = new Template(name, source, getDefaultConfiguration()); + getDefaultConfiguration().getCacheStorage().put(name, template); + } // 渲染模板 return render(template, model, writer); } catch (IOException e) {