Picon
Favicon

[jira] [Created] (VELOCITY-817) Dynamic Templates with Caching

Dynamic Templates with Caching
------------------------------

                 Key: VELOCITY-817
                 URL: https://issues.apache.org/jira/browse/VELOCITY-817
             Project: Velocity
          Issue Type: Improvement
          Components: Engine
    Affects Versions: 1.7
            Reporter: Mark S

I thought I would share the following code with the Apache Velocity community.

Problem Description:  I wanted to use Dynamic Templates though I found the following had massive
performance implications.

-- Velocity Call Snippet --
{code}
String template = "message.to:  ${message.to} ";
StringWriter velocityWriter = new StringWriter();
velocityEngine.evaluate( velocityContext, velocityWriter, "LOG", template );
return velocityWriter.toString();
{code}
-- Velocity Call Snippet --

As such, I needed an alternate solution and guessed that if I could cache the Velocity templates used I could
see big performance gains.

I initially looked to the org.apache.velocity.runtime.resource.loader.StringResourceLoader class
to solve my problems.   The StringResourceLoader class requires an instance of the
(Continue reading)

Picon
Favicon

[jira] [Updated] (VELOCITY-817) Dynamic Templates with Caching


     [
https://issues.apache.org/jira/browse/VELOCITY-817?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Mark S updated VELOCITY-817:
----------------------------

    Description: 
I thought I would share the following code with the Apache Velocity community.

Problem Description:  I wanted to use Dynamic Templates though I found the following had massive
performance implications.

-- Velocity Call Snippet --
String template = "message.to:  ${message.to} ";
StringWriter velocityWriter = new StringWriter();
velocityEngine.evaluate( velocityContext, velocityWriter, "LOG", template );
return velocityWriter.toString();
-- Velocity Call Snippet --

As such, I needed an alternate solution and guessed that if I could cache the Velocity templates used I could
see big performance gains.

I initially looked to the org.apache.velocity.runtime.resource.loader.StringResourceLoader class
to solve my problems.   The StringResourceLoader class requires an instance of the
StringResourceRepository class, and the only implementation of StringResourceRepository is
StringResourceRepositoryImpl.  Unfortunately, since the number of unique templates will grow over
time, in time the StringResourceRepositoryImpl.resources Map will cause an out of memory error.

Why doesn't StringResourceRepositoryImpl use a cache with a maximum size?  Is a cache even needed as the
(Continue reading)


Gmane