Template Caching doesn't work

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Template Caching doesn't work

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

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
Reply | Threaded
Open this post in threaded view
|

Re: Template Caching doesn't work

Daniel Dekany
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



Friday, April 19, 2013, 8:59:51 PM, chintan4181 wrote:

> 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.
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>

--
Thanks,
 Daniel Dekany


------------------------------------------------------------------------------
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
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Template Caching doesn't work

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;

Reply | Threaded
Open this post in threaded view
|

Re: Template Caching doesn't work

Daniel Dekany
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


Monday, April 22, 2013, 3:34:39 PM, chintan4181 wrote:

> 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.
>
> ------------------------------------------------------------------------------
> 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
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/freemarker-user
>


------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Template Caching doesn't work

chintan4181
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!