You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Thomas Åhlen <th...@obidobi.net> on 2001/11/25 11:12:13 UTC

template caching

I have just been browsing throu the source trying to understand the Cache
implementation.
Guess one needs more than 30 minutes to exactly understand what is
happening. ;)
I just wanted to check one thing.

Since I store my templates in a database and have implementet an object
cache myself so I don't have any need for Velocitys cache. But then I
realised that you might cache the NodeTree so that a template only need to
be parsed one time. So that when I call evaluate next time with the same
template the only thing velocity should need to to is create an
InternalContextAdapterImpl and call nodeTree.render( ica, writer ); and thus
saving an rather expensive parse. I suppose that the nodeTree isn't
destoryed by the rendering of the template?

But looking through the code I can't find that you cache the NodeTree
produced by the parsing of the template or have I missed something? I guess
there can be a big performance improvment if it is cached.

Since I call velocitys evaluate with the template as a string there is no
unique identifier so it can't be cached. The way I see it I could extend
Velocity.java and use the logtag as a unique templatename and make my own
NodeTree cache :)

Is there any sence in what I am thinking or have I missunderstood something?

Thomas

Comment: I can't find that this attribute is used anywhere in Velocity?

[RuntimeInstance.java]
    /**
     * The caching system used by the Velocity Runtime
     */
    private  Hashtable globalCache;

    /**
     * Initialize the global cache use by the Velocity
     * runtime. Cached templates will be stored here,
     * as well as cached content pulled in by the #include
     * directive. Who knows what else we'll find to
     * cache.
     */
    private void initializeGlobalCache()
    {
        globalCache = new Hashtable();
    }




--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: template caching

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 11/25/01 5:12 AM, "Thomas Åhlen" <th...@obidobi.net> wrote:

> I have just been browsing throu the source trying to understand the Cache
> implementation.
> Guess one needs more than 30 minutes to exactly understand what is
> happening. ;)
> I just wanted to check one thing.
> 
> Since I store my templates in a database and have implementet an object
> cache myself so I don't have any need for Velocitys cache. But then I
> realised that you might cache the NodeTree so that a template only need to
> be parsed one time. So that when I call evaluate next time with the same
> template the only thing velocity should need to to is create an
> InternalContextAdapterImpl and call nodeTree.render( ica, writer ); and thus
> saving an rather expensive parse. I suppose that the nodeTree isn't
> destoryed by the rendering of the template?

Correct.

> 
> But looking through the code I can't find that you cache the NodeTree
> produced by the parsing of the template or have I missed something? I guess
> there can be a big performance improvment if it is cached.

Believe it or not, we did think of that :)  The ResourceManager takes care
of that (and in 1.3, the cache is pluggable...)
 
> Since I call velocitys evaluate with the template as a string there is no
> unique identifier so it can't be cached. The way I see it I could extend
> Velocity.java and use the logtag as a unique templatename and make my own
> NodeTree cache :)

If you are loading templates from a database and are using Velocity's
evaluate(), then yes, you don't get the advantage of the cache. I have
wanted to extend Velocity.java and VelocityApp.java to return a cacheable
Template that you can cache and reuse.

However, this functionality in Velocity/VelocityApp is meant for dynamic
"construct once and throw away" type of stuff.  You would be better served
by writing a resource loader (use or model after the current
o.a.v..runtime.resource.loader.DatasourceResourceLoader....)

> Is there any sence in what I am thinking or have I missunderstood something?
> 
> Thomas
> 
> Comment: I can't find that this attribute is used anywhere in Velocity?

No, and it should be out of the 1.3 tree...I mean to nip that out at the RI
refactoring...  It's now gone... Thx.

> 
> [RuntimeInstance.java]
>   /**
>    * The caching system used by the Velocity Runtime
>    */
>   private  Hashtable globalCache;
> 
>   /**
>    * Initialize the global cache use by the Velocity
>    * runtime. Cached templates will be stored here,
>    * as well as cached content pulled in by the #include
>    * directive. Who knows what else we'll find to
>    * cache.
>    */
>   private void initializeGlobalCache()
>   {
>       globalCache = new Hashtable();
>   }
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:
> <ma...@jakarta.apache.org>
> For additional commands, e-mail:
> <ma...@jakarta.apache.org>
> 

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
"They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety." - Benjamin Franklin



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>