FreemarkerUtil缓存优化

master
wangbing 5 years ago
parent e11efec26e
commit df5a25472e

@ -149,8 +149,15 @@ public class FreeMarkerUtil {
*/ */
public static boolean renderByFile(File file, Object model, Writer writer) { public static boolean renderByFile(File file, Object model, Writer writer) {
try { 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); return render(template, model, writer);
} catch (IOException e) { } catch (IOException e) {
@ -182,8 +189,15 @@ public class FreeMarkerUtil {
*/ */
public static boolean renderByString(String source, Object model, Writer writer) { public static boolean renderByString(String source, Object model, Writer writer) {
try { 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); return render(template, model, writer);
} catch (IOException e) { } catch (IOException e) {

Loading…
Cancel
Save

Powered by TurnKey Linux.