Re: Is it possible to avoid template reloading?
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Wednesday, September 2, 2009, 6:03:35 PM, JJoe wrote:
> Hi FreemarkerUsers!
>
> I've got a peace of code that is often called while my app runs:
>
> Configuration cfg = new Configuration();
> cfg.setDirectoryForTemplateLoading(new File ("/home/joe/templates/"));
> cfg.getTemplate("myTemplate.ftl","UTF-8").process(freemarkerDataModel,
> osWriter);
> Is it possible to tell freemarker that the template
> "/home/joe/templates/myTemplate.ftl"
> is only read once?
If it is only one (or a few) special templates, you can just store the
Template object that you get on the first occasion, and then reuse it.
However, when those template #include or #import other templates, they
will use the Configuration that created the Environment... so those
template may be re-loaded.
If you don't want any template to be reloaded, you should
cfg.setTemplateUpdateDelay(Integer.MAX_VALUE), so it will never check
the file dates. In additionally, you will have to ensure that the
template cache doesn't throw templates to free memory, by
cfg.setCacheStorage(new
freemarker.cache.MruCacheStorage(Integer.MAX_VALUE, 0)). Now it should
keep the templates that in once loaded forever. Well, assuming you
don't explicitly clear the cache.
The third possibility is to write a custom TemplateLoader that itself
caches the raw (not parsed) templates... those that you don't want to
reload at least. Assuming you don't have a ton of templates, the
memory overhead hardly matters. (Actually, it's enough if the
TemplateLoader fakes the last modification dates, telling that they
didn't change, and the MruCacheStorage shown above then keeps the
parsed templates forever.)
> I know that freemarker caches the template but it will be reloade if the
> template files is changed on the file system.
>
> I want to avoid that the running application will be affected by a changed
> template which could contain errors.
>
> Thanks in advance
> Joe
--
Best regards,
Daniel Dekany
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july