FreeMarker gets list of cached templates in TemplateCache

I am trying to get a list of templates that are already in TemplateCache (which is private and hidden behind the configuration).

I am trying to write a small admin user interface for templates and display what is already cached and call configuration.removeTemplateFromCache(String) on a specific template, and not all ( clearTemplateCache will clean up too much and cause too many IO files). My alternative is to keep a parallel list of template names in the set and use this as a list of keys, but it would be nice if FreeMarker provided this for consistency.

In addition, there is reason to hide the contents of the TemplateCache behind the configuration (allowing you to delete the key using a public method, but not a list of keys). It looks like an anti-pattern.

+4
source share
1 answer

TemplateCache is detailed implementation information (I assume it is publicly available because Java does not allow it to be seen only for FreeMarker packages). The best question is why you cannot get CacheStorage from Configuration . In any case, even FreeMarker does not know the list of templates with your problem, because the CacheStorage interface CacheStorage not provide any method for requesting. However, you can connect your own implementation of CacheStorage through Configuration.setCacheStorage , which provides such a method and stores a link to it somewhere outside of FreeMarker. Yes, it’s awkward that you cannot just get it from Configuration ... But on the bright side, you can control the cache activity as you like.

Update: FreeMarker 2.3.20 has Configuration.getCacheStorage() , so it no longer hides.

+1
source

All Articles