I haven't found such a possibility in thymeleaf (yet?), so i did something which just matches my needs.
I use it currently in two places:
- The application menu which is based on user role(s) and active application modules. So i cache it based on the roles and evict the cache on the page submit of module activation (post rendering with a tag containing cache:evict="name").
- Some "select input" where the content is dynamic but only depending on active entity classes (based on active modules). This here is working as we dont care about the "selected" value at this stage.
The cache:ttl is just an addon and not used yet by myself as i evict the cache explicitly atm.
A dialect for Thymeleaf that allows you to do partial page caching.
Some parts of our webpage will not change often during the lifetime of the application or is dependent only on a usersession.
This dialect will cache the resulting output of the element it is declared on and will replace the element on a cache hit.
Also: If you wish to include this into thymeleaf itself, please feel free to do so.
Key differences in the BLC approach -
--> Replaces substituteBy
--> Adds a cacheKey parameter.
--> The cacheKey can be further resolved to allow for custom interpretations of the value. For example, you could say cacheKey="guests,products=1" and write a resolver where that meant something to your application. By default, the string value is used as the cache key.
--> The BLC solution currently depends on ehCache.
I took a look at your implementation and I think we are going in a very similar direction. I'm always happy to accept inputs to improve implementations such as the cache dialect, so we can surely discuss about any ideas you might have.
I think that my implementation has the advantage that it is less "intrusive" than your approach of a specific tag, as you can apply the cache key on any element in the DOM tree to activate caching.
As far as i can tell it should also work for your purpose with the cacheKey attribute.
Have you already tried to replace your cacheKey attribute with the cache dialect and compared the results?