API / Apache FreeMarker Interview questions
Explain the internal working of FreeMarker's template caching?
Every Configuration owns a template cache keyed by template name, locale, and character encoding. On a cache hit, FreeMarker still needs to decide whether the cached entry is still trustworthy before handing it back, and that decision is governed by two settings working together:
| Setting | Role |
| templateUpdateDelayMilliseconds | How long a cached entry is trusted before FreeMarker re-checks the source at all |
| localizedLookup | When true, the cache also tries locale-specific filename variants (e.g. home_en_US.ftl, then home_en.ftl, then home.ftl) before falling back |
Once the update-delay window has elapsed, the next request for that name causes FreeMarker to ask the TemplateLoader for the source's last-modified marker (or equivalent); only if that marker has actually changed does FreeMarker re-parse the source and replace the cached Template - otherwise the same compiled AST is reused, and the cache entry's freshness window simply resets.
The cache is thread-safe, so concurrent requests hitting a stale-but-unchanged entry do not each trigger their own redundant reload. Calling configuration.clearTemplateCache() forces an unconditional purge, which is normally only needed in development or after a bulk template deployment.
More Related questions...