Java Mailing List Archive

Home » FreeMarker-user »

Re: [FreeMarker-user] Removing templates manually from a cache.

Daniel Dekany


Replies: Find Java Web Hosting

Author LoginPost Reply
Wednesday, December 14, 2011, 11:52:35 PM, Frank Zeyda wrote:

> Dear FreeMarker Developers,
> Here's a small problem I encountered when using a MultiTemplateLoader that
> first loads templates from the file store, and via a second
> StringTemplateLoader allows for creation and use of on-the-fly templates while
> the program executes. The reason for using a common loader is
> intentional so that the on-the-fly templates can include those from the
> file store where needed.
> My problem is that I reuse the same name for the on-the-fly templates when
> inserting them i.e. using the StringTemplateLoader method (putTemplate(...)).
> This does not seem to cause the template to be reloaded unless one clears the
> entire cache using clearTemplateCache() of the TemplateCache object. This,
> however, appears to be inefficient as I suppose it would cause all templates
> previously cached to be reloaded.

It would.

> setDelay() is not much use since I need the one template to be
> reloaded immediately.

BTW the whole delay business has a design issue... it should be
per-template-loader. After all, what the ideal delay depends on how
resource consuming checking if the source was changed is. So, I just
wonder of a 0 delay for the StringTemplateLoader and non-0 for the
FileTemplateLoader would solve your problem. Anyway, you can't do

> To solve the problem, I tried to create and
> set my own CacheStore, however, the methods get(...), put(...) and
> remove(...) are of not much use here since one has no public access
> to the object that is used as the key in the underlying map
> (TemplateCache.TemplateKey is private).

You are supposed use the CacheStorage API only indirectly, trough the
Configuration API. Or at least I suppose that was the intent of the
authors. Problem is, there's nothing that calls CacheStorage.remove in
FreeMarker, as far as I see at least. (Consequently, that method of
CacheStorage implementations isn't field tested... But I don't know
the author of them as sloppy, so let's hope for the best.)

> So, to summarise, the solution to the above problem via clearTemplateCache()
> seems to be not entirely satisfactory. Instead one may wish to have finer
> control about the caches, for instance, in order to remove specific entries
> manually; possibly via a method in TemplateCache, or by way of exposing
> TemplateCache.TemplateKey so that keys can be created by the user.

Can you get the TemplateCache instance at all? There's no
Configuration.getTemplateCache, nor setTemplateCache. I think the only
reason TemplateCache is public is that access control in Java is
overly simplistic for any product that has sub-packages.

Anyway... I have added Configuration.removeTemlateFromCache to the 2.3
branch. Tell me if it works. Here's the build:

> Many thanks for any comments, in particular in case I am missing a simpler
> solution.
> Best wishes
> Frank

Best regards,
Daniel Dekany

10 Tips for Better Server Consolidation
Server virtualization is being driven by many needs.
But none more important than the need to reduce IT complexity
while improving strategic productivity. Learn More!
FreeMarker-user mailing list
©2008 - Jax Systems, LLC, U.S.A.