여기에 우리의 유스 케이스가 있습니다. 데이터베이스에서 freemarker 구문을로드하고 처리하는 중입니다. 우리는 백만 건에 가까운 기록을 처리하고 있습니다. 모든 것이 잘 작동합니다. 그러나 응용 프로그램의 프로필을 작성할 때 freemarker 처리 방법이 병목 현상이며 시간이 많이 걸리는 것으로 나타났습니다. freemarker 문서를 읽었을 때, 필자는 내 문제에 대해 약간의 조언을 받았다. 매번 처리를 할 때마다 새로운 freemarker.template.Template 객체 (생성이 비싸다)를 만듭니다. 나는 이것을하는 올바른 /보다 효율적인 방법이 무엇인지를 알 수 없었다. Java에서 String Freemarker 구문을 처리하는 가장 효율적인 방법은 무엇입니까
는이 프리 마커 구문 분석해야 할 때마다 호출되는 처리 방법을 찾아 보게한다. 이 메소드에서는 매번 새로운 Template 객체를 생성합니다. 이것을 피할 수있는 방법이 있습니까?public FTLTemplateEngine() { cfg = new Configuration(); } public String process(String template, Map<String, Object> input) throws IOException, TemplateException { String rc = null; final Writer out = new StringWriter(); try { final Template temp =new Template("TemporaryTemplate", new StringReader(template), cfg); temp.process(input, out); } catch (InvalidReferenceException e) { log.error("Unable to process FTL - " + template); throw new InvalidReferenceException("FTL expression has evaluated to null or it refers to something that doesn't exist. - " + template, Environment.getCurrentEnvironment()); } catch (TemplateException e) { log.error("Unable to process FTL - " + template); throw new TemplateException("Unable to process FTL - " + template, e, Environment.getCurrentEnvironment()); } catch (IOException e) { log.error("Unable to process FTL - " + template); throw new IOException("Unable to process FTL - " + template); } rc = out.toString(); out.close(); return rc.trim(); }
'Template.process'는 템플릿을 실행하고,'new Template'는 구문 분석을 수행합니다. 아니면 당신의 * 프로세스 방법이 시간을 필요로한다는 것을 의미합니까? 어쨌든, 당신은 이것에 관해 무엇인가 말하기 위해 좀 더 자세한 분석을해야합니다. – ddekany
@ddekany, 새 템플리트 ("TemporaryTemplate", 새 StringReader (템플리트), cfg)가 대부분 소요됩니다. 처리 속도는 매우 빠릅니다. – Anil
그럼, 결과로'Template'-s를 캐쉬해야 할 것 같습니다. 실제로 애플리케이션에 너무 느린 경우입니다. – ddekany