Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Thymeleaf cacheable template?

Tags:

thymeleaf


I am learning Thymeleaf and don't understand what are cacheable templates.
Can you sb explain me what they are and usage?
Thanx

like image 782
Mladen Uzelac Avatar asked Feb 15 '15 19:02

Mladen Uzelac


1 Answers

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");

like image 161
vtor Avatar answered Oct 18 '22 06:10

vtor