Posts Tagged java apache-velocity
Speed up the Apache-Velocity-Engine significantly
The Apache-Velocity-Engine is a nice template engine. I’m using it for producing static html pages in my mini CMS poormans. In common cases the configuration and rendering of Velocity is based on file system resources (templates/macros). Because poormans holds all templates and macros in a database, I was looking for a new way to configure and use velocity without file system resources. By the way I’ve got an increasing of performance up to 15%.
To make a merge without taking a template from file system is very simple. You just have to use:
VelocityEngine.evaluate(veloCtx, writer, logTag, stringTemplate) (see here)
A tricky bit is how to configure Velocity to take marcos from a string and not from a file system resource. Therefore you need to configure the StringResourceLoader. In your properties file the following entries are necessary:
resource.loader = string
string.resource.loader.description = Velocity StringResourceLoader
string.resource.loader.class = org.apache.velocity.runtime.resource.loader.StringResourceLoader
Now you can easily add macros to your VelocityEngine:
StringResourceRepository repo = StringResourceLoader.getRepository();
This kind of configuration and the internal cashing of the template strings results in an increasing of performance up to 15%.