Open Gohla opened 4 years ago
However, TemplateLoader.getTemplate returns a Reader instead of a Template, meaning that every time a partial is used, it is recompiled into a Template, which incurs extra overhead. I don't see a nice way to cache these partial templates with the current code (but I could be wrong)
You should test it to verify that claim:
@Test
public void testPartials()
throws Exception {
AtomicInteger count = new AtomicInteger();
TemplateLoader tl = new TemplateLoader() {
@Override
public Reader getTemplate(
String name)
throws Exception {
count.incrementAndGet();
return new StringReader(name);
}
};
Template t = Mustache.compiler().withLoader(tl)
.compile("{{> crap }}");
t.execute(new Object());
t.execute(new Object(){});
t.execute(new Object());
//If the sub template was not cached we would expect 3
assertEquals(1, count.get());
}
Thus the only thing you need to cache are the root templates. Most of the frameworks that use JMustache do this like Spring MVC.
@samskivert
I think the above issue could be remedied for now with better documentation.
The javadoc does answer how to cache templates but its discoverability is not easy:
Furthermore we are missing Javadoc on the package-info.java
and module-info.java
. I plan on submitting a PR with basically a good portion the README.md in the module-info.java
as javadoc later this week.
Partial templates can be provided with a
TemplateLoader
. However,TemplateLoader.getTemplate
returns aReader
instead of aTemplate
, meaning that every time a partial is used, it is recompiled into aTemplate
, which incurs extra overhead. I don't see a nice way to cache these partial templates with the current code (but I could be wrong).As a workaround, I currently subclass
Mustache.Compiler
and add caching behavior. However, becauseMustache.Delims
isprotected
, this subclass must live under thecom.samskivert.mustache
package, and split packages are not allowed in Java 9 or higher.Is there a nicer way to implement this caching behavior?