Pre-caching of templates using server startup

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view

Pre-caching of templates using server startup


We're migrating our webapp to Thymeleaf (liking it so far!), but I'd like to know if it is possible to pre-cache templates.  Our server is generally under relatively high continuous load, so I'd like to avoid the small initial template parsing overhead for the first request (about 200ms on my machine).  We used to use JSPs, and precompiled them during server startup, so would like to do something similar with Thymeleaf.

I've hacked a workaround for now (by processing each template with a null model and then catching any resulting exceptions), but I think that a 'nice' solution wouldn't be too hard either, for example:

* Add a 'parse' method to TemplateManager, which would be the same as the existing parseAndProcess method, except that it would not make the final process call (and parseAndProcess could be refactored to call the new parse method).  This seems to be enough to cache the template (might still need an IContext, but callers could always provide a default implementation).
* (Optionally) Add a 'parse' method to TemplateEngine that would delegate to TemplateManager.parse - convenience method, since TemplateEngine would seem to be more easily accessible than TemplateManager.

Would that make sense?  I can create a PR if that would help.