Search code examples
javatemplatesfreemarker

Freemarker: reload template using custom function


i want to use freemarker with templates from database. templates will change sometimes. i would like to create a Function<String, String> that takes a template name and return current template from database.

How can i make freemarker considers templates stale after x minutes and just calls my function every time it needs new version of template? or maybe some other templating provides such functionality out of the box?


Solution

  • There's a configuration setting called template_update_delay, or with Java API Configuration.setTemplateUpdateDelayMilliseconds(long). This decides how often FreeMarker checks if a template cache entry is stale. (It only does that when you require the template, and its cache entry is older than that.) I guess you should simply rely on that.

    There's also Configuration.removeTemplateFromCache, in case you are notified about template changes.