Disable template cache during development phase

122 views
Skip to first unread message

Liu Honghai

unread,
Jul 30, 2015, 3:48:14 PM7/30/15
to mustache.java
Hi,

when using mustache.java during development, I noticed that any changes to template won't be effective unless I restart the application. I assume the cache is enabled by default.

is there a good way to disable template cache during development phase? This could save quite some time during development as the templates may be changed often.

thank you!

BR
Honghai


Sam

unread,
Aug 4, 2015, 1:44:26 PM8/4/15
to mustache.java
For development just create a new MustacheFactory for each request rather than using the same one every time. You can't really turn off caching at the compilation level without breaking things like recursion.

Sam

Liu Honghai

unread,
Aug 5, 2015, 7:56:39 AM8/5/15
to mustache.java
Hi Sam,

actually that is what did for each request. I use mustache.java together with vertx 3. in the handler, I create the factory like:

MustacheFactory mf = new DefaultMustacheFactory();
try {
            // Compile the template
            Mustache m = mf.compile(TEMPLATE_ROOT + page.getTemplate());
           
            StringWriter outputWriter = new StringWriter();
           
            m.execute(outputWriter, page).flush();
            context.response().end(outputWriter.toString());
           
            // Set the cookie
} catch (Exception e) {
....
}
but it seems still the templates are cached. when I make changes to templates, I have to restart the server to make the changes effective.

BR
Honghai

Michael Hixson

unread,
Aug 5, 2015, 6:04:12 PM8/5/15
to mustac...@googlegroups.com
Hi,

Maybe the template files are being loaded from your classpath (e.g.
target/classes) instead from the source (e.g. src/main/resources).
What's the value of TEMPLATE_ROOT in that code? If you change it to
an absolute file name like
"/path/to/your/project/src/main/resources/mustache_templates/" do you
get the expected behavior?

-Michael
> --
> You received this message because you are subscribed to the Google Groups
> "mustache.java" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mustachejava...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Sam Pullara

unread,
Aug 5, 2015, 7:28:31 PM8/5/15
to mustac...@googlegroups.com
I think you might have a different issue which is that you are loading from the classpath. If you are using a tool to build, you have to run that tool to copy the changed files into the right place. What is the whole setup that you are using?

Sam

On Wed, Aug 5, 2015 at 4:56 AM, Liu Honghai <liu.h...@gmail.com> wrote:

Liu Honghai

unread,
Aug 7, 2015, 9:33:20 AM8/7/15
to mustache.java
Hi Sam,

thanks! indeed, the templates are loaded from classpath. I am using eclipse as IDE. I think it might be the changed templates are not copied to target/classes automatically. If I restart the server/vertx, elipse did copy the templates.

I will try to figure it out. good to have the additional information from you.

BR
Honghai

Liu Honghai

unread,
Aug 7, 2015, 9:35:07 AM8/7/15
to mustache.java
Thanks Michael,

the TEMPLATE_ROOT is just a prefix for the folder, it is "templates/". You are right that the templates are loaded from classes path.
I am checking the eclipse + maven setup to see how to manage this.

BR
Honghai

Sam

unread,
Aug 7, 2015, 1:36:03 PM8/7/15
to mustache.java
I would probably use the file system instead of the class path and point directly at the src folder during development and use the class path for production. As an aside, you really should only compile each template once in production and keep a reference to it. Recompiling is pretty expensive though you might not notice it until you are under a production load.

Sam
Reply all
Reply to author
Forward
0 new messages