Alex Chacha
Alex Chacha

Reputation: 423

FreeMarker get list of cached templates in TemplateCache

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

I am trying to write a small admin UI to the templates and display what is already cached and call configuration.removeTemplateFromCache(String) on a specific template and not everything (clearTemplateCache will clear too much and cause too much file IO). My alternative is to keep a parallel list of template names in a Set and use that as a list of keys, but would be nice if FreeMarker provided this for consistency.

Also, it there a reason for hiding the TemplateCache contents behind Configuration (while allowing removal by key via public method, but not a list of keys). Seems like an anti-pattern.

Upvotes: 4

Views: 930

Answers (1)

ddekany
ddekany

Reputation: 31152

The TemplateCache is kind of implementation detail (I guess it's public because Java doesn't allow it to be visible for FreeMarker packages only). The better question is why you can't get the CacheStorage from the Configuration. Anyway, as of your problem, not even FreeMarker knows the list of templates, because the CacheStorage interface doesn't provide any method for querying that. However, you can plug your own CacheStorage implementation in via Configuration.setCacheStorage, that provides such method, and keep the reference to it somewhere outside FreeMarker. Yeah, it's kind of awkward that you can't just get it from Configuration... But on the bright side you can monitor the cache activity however you like.

Update: FreeMarker 2.3.20 has Configuration.getCacheStorage(), so it's not hidden anymore.

Upvotes: 1

Related Questions