Table of contents16 Template Cache
Thymeleaf works thanks to a set of
parsers for markup and text — which analyzes patterns in a sequence (open tag, text, close tag, comment, etc.). And a series of
processors , one for each type of behavior, which apply and modify the sequence of pattern parsing events to create the expected results by combining the original pattern with our data.
It also includes — by default — the
cache in which the analyzed templates are stored; sequence of events resulting from reading and analyzing template files before processing them. This is especially useful when working in a web application and is based on the following concepts:
- Input / output is almost always the slowest part of any application.
- Cloning an existing sequence of events in memory is always much faster than reading a template file, parsing it and creating a new sequence of events for it.
- Web applications usually have only a few dozen templates.
- Template files are small and medium in size and they do not change while the application is running.
All this leads to the idea that caching the most used templates in a web application is possible without losing large amounts of memory, and also that it will save a lot of time spent on input-output operations on a small set of files that, in fact, never change.
')
And how can we control this cache? First, we learned that we can enable or disable the cache in the Resolver Template, even acting only on certain templates:
In addition, we could change the configuration by setting our own Cache Manager object, which can be an instance of the default CacheManager default implementation:
Refer to the javadoc API org.thymeleaf.cache.
StandardCacheManager for more information on configuring caches.
Entries can be removed manually from the template cache: