springmvc HandlebarsViewResolver: Setting Cache to `true` cause cache to be ignored
HandlebarsViewResolver has two caches:
private TemplateCache templateCache;in HandlebarsViewResolver directlyprivate final Map<Object, View> viewAccessCachein its parent AbstractCachingViewResolver
Having this property:
handlebars:
cache: true
cause the HandlebarsViewResolver#setCache is called, and set:
HandlebarsViewResolver.cache = true
During lifecycle AbstractCachingViewResolver#resolveViewName it check for cache property. If it is:
- false
- then it behaves as expected: the view is created from scratch and since
templateCacheis forced toNullTemplateCache.INSTANCEno cache is actually called.
- then it behaves as expected: the view is created from scratch and since
- true
- then it buggy:
HandlebarsViewResolver#resolveViewNameis accessingviewAccessCachemap and get view directly from that. The view already created and so configuredTemplateCache templateCacheis simply bypassed.
- then it buggy:
Proposed fix: override AbstractCachingViewResolver#resolveViewName in HandlebarsViewResolver and if caching is enabled - to delegate it to templateCache rather then to viewAccessCache
Related issue: https://github.com/jknack/handlebars.java/issues/780 since old cache instance is wiped out without reverting possibility:
@Override
public void setCache(final boolean cache) {
if (!cache) {
templateCache = NullTemplateCache.INSTANCE;
}
super.setCache(cache);
}
Proposed fix: override
AbstractCachingViewResolver#resolveViewNamein HandlebarsViewResolver and if caching is enabled - to delegate it totemplateCacherather then toviewAccessCache
Or distinguish these caches by different keys:
handlebars:
cache: false # say not to use viewAccessCache
template-cache: true # but use templateCache
template-cache-reloadable: true # a nice to have feature because of which I went so deep and because if this bug is simply not accessible