Discussion:
[FreeMarker-user] Template Caching doesn't work
chintan4181
2013-04-19 18:59:51 UTC
Permalink
Hi,

I am using Freemarker for email template with spring. I want to cache my
template, but somehow it's not working. Below is my configuration

Spring configuration:
<bean id="freemarkerMailConfiguration"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="freemarkerSettings">
<props>
<prop key="cache_storage">strong:50, soft:500</prop>
<prop key="template_update_delay">40</prop>
</props>
</property>
</bean>

private String getContent(String templateFileName, String templatedir,
java.util.Map<String, Object> dynamicParams) throws Exception {
if(ftl == null)
ftl = new FileTemplateLoader(new File(templatedir));

configuration.setTemplateLoader(ftl); //configuration object is injected
by spring which has //MruStorage cache with 50,500

template = configuration.getTemplate(templateFileName); //all the time
parse same template again.
String result =
FreeMarkerTemplateUtils.processTemplateIntoString(template, dynamicParams);

return result;
}

I am calling getContent method for 3 times before sending email. but all
times it prints below message in the log
Could not find template in cache, creating new one;
id=[mail-template.txt[en_US,Cp1252,parsed] ]
Compiling FreeMarker template mail-template.txt[en_US,Cp1252,parsed]

Can somebody tell me, how do i cache the template so it retrieve from Cache.

Thanks
Chintan



--
View this message in context: http://freemarker.624813.n4.nabble.com/Template-Caching-doesn-t-work-tp4654527.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-04-20 21:12:05 UTC
Permalink
Hello,

The problem is that setting the TemplateLoader drops the cache. Also,
it's not allowed to modify the Configuration after have started using
it from multiple threads (unless you do the necessary
synchronizations).

Note that the template name can be a path like
"myemailtemplates/whatever/thistemplate.ftl". So probably you
shouldn't have a templatedir parameter at all.
--
Thanks,
Daniel Dekany
Post by chintan4181
Hi,
I am using Freemarker for email template with spring. I want to cache my
template, but somehow it's not working. Below is my configuration
<bean id="freemarkerMailConfiguration"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="freemarkerSettings">
<props>
<prop
key="cache_storage">strong:50, soft:500</prop>
<prop
key="template_update_delay">40</prop>
</props>
</property>
</bean>
private String getContent(String templateFileName, String templatedir,
java.util.Map<String, Object> dynamicParams) throws Exception {
if(ftl == null)
ftl = new FileTemplateLoader(new File(templatedir));
configuration.setTemplateLoader(ftl);
//configuration object is injected
by spring which has //MruStorage cache with 50,500
template =
configuration.getTemplate(templateFileName); //all the time
parse same template again.
String result =
FreeMarkerTemplateUtils.processTemplateIntoString(template, dynamicParams);
return result;
}
I am calling getContent method for 3 times before sending email. but all
times it prints below message in the log
Could not find template in cache, creating new one;
id=[mail-template.txt[en_US,Cp1252,parsed] ]
Compiling FreeMarker template mail-template.txt[en_US,Cp1252,parsed]
Can somebody tell me, how do i cache the template so it retrieve from Cache.
Thanks
Chintan
--
http://freemarker.624813.n4.nabble.com/Template-Caching-doesn-t-work-tp4654527.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
--
Thanks,
Daniel Dekany
chintan4181
2013-04-22 13:34:39 UTC
Permalink
If i don't set configuration.setTemplateLoader(ftl); then it gives me an
exception saying "Template not found".

I am loading template from physical location.

String templateDir = "C:\\workspace\\static-artifacts\\email-templates";
String templateFileName = "mail-template.ftl";

This is how i am using above to variables

private String getContent(String templateFileName, String templatedir,
java.util.Map<String, Object> dynamicParams) throws Exception {

if(ftl == null)
ftl = new FileTemplateLoader(new File(templatedir));
//if i comment below line, then i am getting "template not found"
exception
configuration.setTemplateLoader(ftl);

template = configuration.getTemplate(templateFileName);

String result =
FreeMarkerTemplateUtils.processTemplateIntoString(template, dynamicParams);

return result;





--
View this message in context: http://freemarker.624813.n4.nabble.com/Template-Caching-doesn-t-work-tp4654527p4654529.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Daniel Dekany
2013-04-23 20:22:05 UTC
Permalink
But the problem is that you are supposed to set the TempateLoader only
*once* in the life-cycle of the app, earlier than you start processing
any templates. Normally, you do that when the application is
initialized. You set up the Configuration object there, and then you
don't change anything in it anymore.
--
Thanks,
Daniel Dekany
Post by chintan4181
If i don't set configuration.setTemplateLoader(ftl); then it gives me an
exception saying "Template not found".
I am loading template from physical location.
String templateDir =
"C:\\workspace\\static-artifacts\\email-templates";
String templateFileName = "mail-template.ftl";
This is how i am using above to variables
private String getContent(String templateFileName, String templatedir,
java.util.Map<String, Object> dynamicParams) throws Exception {
if(ftl == null)
ftl = new FileTemplateLoader(new File(templatedir));
//if i comment below line, then i am getting "template not found"
exception
configuration.setTemplateLoader(ftl);
template =
configuration.getTemplate(templateFileName);
String result =
FreeMarkerTemplateUtils.processTemplateIntoString(template, dynamicParams);
return result;
--
http://freemarker.624813.n4.nabble.com/Template-Caching-doesn-t-work-tp4654527p4654529.html
Sent from the freemarker-user mailing list archive at Nabble.com.
------------------------------------------------------------------------------
Precog is a next-generation analytics platform capable of advanced
analytics on semi-structured data. The platform includes APIs for building
apps and a phenomenal toolset for data science. Developers can use
our toolset for easy data analysis & visualization. Get a free account!
http://www2.precog.com/precogplatform/slashdotnewsletter
_______________________________________________
FreeMarker-user mailing list
https://lists.sourceforge.net/lists/listinfo/freemarker-user
chintan4181
2013-04-23 20:23:49 UTC
Permalink
Thanks Daniel.

I removed template loader code from my method and it started working. Now it
caches template.

Thank you very much for the help!



--
View this message in context: http://freemarker.624813.n4.nabble.com/Template-Caching-doesn-t-work-tp4654527p4654531.html
Sent from the freemarker-user mailing list archive at Nabble.com.
Loading...