handlebars.java icon indicating copy to clipboard operation
handlebars.java copied to clipboard

springmvc HandlebarsViewResolver: Setting Cache to `true` cause cache to be ignored

Open msangel opened this issue 3 years ago • 2 comments

HandlebarsViewResolver has two caches:

  1. private TemplateCache templateCache; in HandlebarsViewResolver directly
  2. private final Map<Object, View> viewAccessCache in 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 templateCache is forced to NullTemplateCache.INSTANCE no cache is actually called.
  • true
    • then it buggy: HandlebarsViewResolver#resolveViewName is accessing viewAccessCache map and get view directly from that. The view already created and so configured TemplateCache templateCache is simply bypassed.

Proposed fix: override AbstractCachingViewResolver#resolveViewName in HandlebarsViewResolver and if caching is enabled - to delegate it to templateCache rather then to viewAccessCache

msangel avatar Jun 05 '22 23:06 msangel

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);
  }

msangel avatar Jun 05 '22 23:06 msangel

Proposed fix: override AbstractCachingViewResolver#resolveViewName in HandlebarsViewResolver and if caching is enabled - to delegate it to templateCache rather then to viewAccessCache

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

msangel avatar Jun 05 '22 23:06 msangel