spring-projects / spring-boot

Spring Boot
https://spring.io/projects/spring-boot
Apache License 2.0
73.69k stars 40.34k forks source link

Support mustache template caching #20706

Open hookumsnivy opened 4 years ago

hookumsnivy commented 4 years ago

According to the JMustache docs, all the necessary classes are thread safe with the caveat that if you customize any classes they also have to be thread safe.

Currently instances of the MustacheView class can be cached, but not the result of compilation. Every time the view is rendered in renderMergedTemplateModel, the resource gets loaded, parsed and compiled into a Template before rendering. If there was an option to cache the compiled form, perhaps with a pluggable caching implementation, we wouldn't need to load and compile on every request. Admittedly I didn't do any performance testing, but it seems like it should be beneficial.

While I'm on the subject of the MustacheView class, it strikes me as odd that it uses a getApplicationContext().getResource(...) to get the resource instead of a MustacheResourceTemplateLoader instance. After all, when using partials, it will load the partial using the MustacheResourceTemplateLoader.

philwebb commented 4 years ago

it strikes me as odd that it uses a getApplicationContext().getResource(...) to get the resource instead of a MustacheResourceTemplateLoader instance.

Can you expand on this a bit more? I'm not sure we can use the TemplateLoader since we need to resolve a URL in MustacheView.

hookumsnivy commented 4 years ago

Internally the mustache library uses a TemplateLoader instance that returns a Reader object. Since the mustache library uses that to load partials, it seems odd to me that the spring doesn't use that same object to load the main template as well. To think of it another way, the main template for your view is going to be pulled via spring's ResourceLoader and any partials are going to be pulled by via JMustache's TemplateLoader. It's just not consistent. The auto configuration for Mustache does however set up a TemplateLoader that uses the Resource loader. My thought was why not use the TemplateLoader for both.