-
Notifications
You must be signed in to change notification settings - Fork 129
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Caching partial templates #116
Comments
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. |
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 |
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?
The text was updated successfully, but these errors were encountered: