I am learning Thymeleaf and don't understand what are cacheable templates.
Can you sb explain me what they are and usage?
Thanx
By default, cache is enabled for thymeleaf, therefore all pages are cached.
Thymeleaf template engine will store parsed templates before processing them, so that if there are changes, it is possible to see those only after re-reading/parsing the files again (e.g. redeploy, restart).
This is actually very convenient, specially in heavy web application(many pages with different sizes), as otherwise for every single change it will re-read all pages (which is simple input/output operations - costing time), though the most of the pages would never be changed.
You can disable the cache explicitly when configuring template engine
templateResolver.setCacheable(false);
With disabled cache, after modifying thymeleaf pages you can just reload the page in the web browser and see the changes, so for every single change thymeleaf will parse the template and load into the application immediately. Again, it will re-load all pages from your application, not the single one which has been changed.
Also it is possible to clear cache for single template by
templateEngine.clearTemplateCacheFor("/test");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With