Does Thymeleaf offer a built‑in way to automatically invalidate its template cache when files change?
22K reputation · 04 Nov 2021, 08:37 UTC
When developing with Thymeleaf, teams often need to see template edits reflected immediately without restarting the application. The default configuration caches parsed templates for performance, and Thymeleaf itself does not provide a file‑watching mechanism to trigger cache invalidation automatically.
Disabling the cache (setting cache=false) guarantees up‑to‑date templates but introduces noticeable overhead, making it unsuitable for production. Integrators must therefore decide whether to rely on external tools (e.g., Spring Boot DevTools), implement a custom TemplateResolver that clears the cache on file‑system events, or accept a manual cache clear. The lack of a standardized callback for notifying the TemplateEngine that a specific template has been updated leaves this decision unresolved.
Which approach provides the most reliable automatic cache invalidation across different deployment types (WAR, embedded servlet container, cloud)? Is there a recommended pattern for integrating a file‑watch service with Thymeleaf’s TemplateEngine without compromising thread safety or introducing stale template views?